Skip to contentSkip to conversation

visitor+BER migration format

RFC PATCH In review Dr. David Alan Gilbert (git) 3 participants 24 messages Mar 25, 2014 – Jun 6, 2014

lore
Share & tools
lore
#0 lore raw

cover visitor+BER migration format

From: "Dr. David Alan Gilbert" <[email protected]>

This is a work in progress cut of my visitor+BER format migration world; there's lots to do but it's starting to get there. I'd appreciate if anyone who has more experience with either the visitor code, or ASN.1/BER in general has a look to see if I'm doing anything particularly odd.

I've cherry picked bits of Michael Roth's visitor set from 2011 and Stefan Berger's BER code from this time last year, but not necessarily used things in the same way.

The good:
It can perform a migration using a BER format file that's described
and verifiable against a schema.

It can also perform a migration using the old binary format.

A lot of the nasty detail of the old format is hidden in the compatibility visitor (e.g. the subsection end detection).

The ugly:
1) The encoding for zero pages is bloated, it can be simplified a lot (see previous discussion with Michael from a week or two back that I've not yet implemented)
2) There are a few shims that get passed visitors but need files or the other way that should be removable. The shim to get from a qemu file back to a visitor is patch 3 and marked as 'tmp' and the main uses are in piix4, pci.c, spapr_vscsi.c.
3) There are places where the visitor interface is too tied to the old file format, but most of those can be removed with effort.
4) I've made extensions to the Visitor type, many of these are very migration specific; and use things like get_next_type and the list visitors in quite a different way from the current uses. Should I really have a separate type?
(see patch 1)
5) At the moment you select BER output format by setting an environment variable ( export QEMUMIGFORMAT=BER ) , I need to put more thought in to the right way to do this, there are some harder questions like what happens to devices that are still using pre-vmstate encodings (that are currently sent as blobs) when they eventually convert over and thus how to keep compatibility with earlier BER output versions where they were blobs.
6) At the moment for blobs (i.e. pre-vmstate) the whole blob gets loaded into memory and then processed; that's not necessary and can be done with a QEMUFile shim.
7) I need to sort out my Error** handling (probably in a lot of places)
8) The visitors should be able to share much more code.

The not-yet-done:
a) XBZRLE
b) block-migration
c) SPAPR (it's an iterative migration like RAM and block)
d) RDMA (I'm guessing I've broken it, but haven't tried)
e) Floats are currently sent as blobs.

What I've done:
There are separate output visitors (binary compatible, debug and BER) and
they're selected by setting QEMUMIGFORMAT to one of 'debug', 'BER' or just leaving it unset.

The Input visitor is selected automatically from the first 4 bytes of the
file.

In general most types are BER sequences of indefinite length, with some
types that I've allocated an Application specific type tag to. There is a hook to give any VMState it's own type tag (which I've shown using one example for cpu-common). Integers and strings are standard 'universal' types.

Objects with .get/.put methods or register_savevm are saved as an 'octet string'.
There are a few places where a device registered with .get/.put calls back into vmstate_save_state for part of their state (typically for devices built on PCI) and when they do that, even when in BER mode, those components get stored inside the octet string in the old format.

I've used the 'asn1c' tool to validate the schema (which is
in docs/specs/migration.schema) and also to verify streams that I've produced match the schema.

I've tested it with virt-test (hacked to have different source/dest qemu's)
and tried bin-bin, ber-ber, and pre-visitor-qemu -> this and
this -> pre-visitor-qemu just on the standard migration.default.tcp migration test, but I've not tried a large combination of configurations.

My fix for qemu_peek_buffer that's on the list is needed to stabilise
the binary format input visitor.

I'll keep chipping away at that list, and would expect to pop another
version out in a month or so.

Dave

Dr. David Alan Gilbert (16):
Visitor: Add methods for migration format use
QEMUSizedBuffer/QEMUFile
qemu-file: Add set/get tmp_visitor
Header/constant/types fixes for visitors
Visitor: Binary compatible output visitor
Visitor: Debug output visitor
Visitor: Binary compatible input visitor
Visitor: Output path
Visitor: Load path
Visitor: Common types to use visitors
Choose output visitor based on env variable
BER Visitor: Create output visitor
BER Visitor: Create input visitor
Start some BER format docs
ASN.1 schema for new migration format
Wire in BER visitors

 arch_init.c                                    |  231 +++--
 block-migration.c                              |   13 +-
 docs/migration.txt                             |   34 +-
 docs/specs/migration.schema                    |  113 +++
 exec.c                                         |    2 +
 hw/acpi/piix4.c                                |    5 +-
 hw/pci/pci.c                                   |    5 +-
 hw/ppc/spapr.c                                 |    9 +-
 hw/scsi/spapr_vscsi.c                          |    6 +-
 include/migration/migration.h                  |   17 +
 include/migration/qemu-file.h                  |   31 +
 include/migration/vmstate.h                    |   28 +-
 include/qapi/ber.h                             |  108 +++
 include/qapi/qemu-file-ber-input-visitor.h     |   26 +
 include/qapi/qemu-file-ber-output-visitor.h    |   26 +
 include/qapi/qemu-file-binary-input-visitor.h  |   27 +
 include/qapi/qemu-file-binary-output-visitor.h |   26 +
 include/qapi/qemu-file-debug-output-visitor.h  |   26 +
 include/qapi/visitor-impl.h                    |   23 +
 include/qapi/visitor.h                         |   51 ++
 include/qemu/typedefs.h                        |    4 +-
 qapi/Makefile.objs                             |    4 +-
 qapi/qapi-visit-core.c                         |   80 ++
 qapi/qemu-file-ber-input-visitor.c             | 1163 ++++++++++++++++++++++++
 qapi/qemu-file-ber-output-visitor.c            |  916 +++++++++++++++++++
 qapi/qemu-file-binary-input-visitor.c          |  688 ++++++++++++++
 qapi/qemu-file-binary-output-visitor.c         |  564 ++++++++++++
 qapi/qemu-file-debug-output-visitor.c          |  471 ++++++++++
 qemu-file.c                                    |  423 +++++++++
 savevm.c                                       |  412 +++++++--
 vmstate.c                                      |  619 ++++++-------
 31 files changed, 5593 insertions(+), 558 deletions(-)
 create mode 100644 docs/specs/migration.schema
 create mode 100644 include/qapi/ber.h
 create mode 100644 include/qapi/qemu-file-ber-input-visitor.h
 create mode 100644 include/qapi/qemu-file-ber-output-visitor.h
 create mode 100644 include/qapi/qemu-file-binary-input-visitor.h
 create mode 100644 include/qapi/qemu-file-binary-output-visitor.h
 create mode 100644 include/qapi/qemu-file-debug-output-visitor.h
 create mode 100644 qapi/qemu-file-ber-input-visitor.c
 create mode 100644 qapi/qemu-file-ber-output-visitor.c
 create mode 100644 qapi/qemu-file-binary-input-visitor.c
 create mode 100644 qapi/qemu-file-binary-output-visitor.c
 create mode 100644 qapi/qemu-file-debug-output-visitor.c
Signature · 1 line
1.8.5.3
#1 lore raw

RFC PATCH 1/16 Visitor: Add methods for migration format use

From: "Dr. David Alan Gilbert" <[email protected]>

array types

    From  https://lists.gnu.org/archive/html/qemu-devel/2011-09/msg02465.html

str256 type
For the upto 256byte strings QEMU commonly uses for IDs

buffer type
For a blob of data that the caller wants to deliver whole (e.g. a page of RAM or block of disk)

Load/save flags to let a user perform pre-save/post-load checking

An accessor to get the underlying QEMUFile* (for compatibility)

compat-sequences
Provide enough information for a visitor providing compatibility
with the old format to generate it's byte stream, while allowing a new visitor to do something sensible.

destroy
Allows the caller to destroy a visitor without knowing what type of visitor it is.

---
 include/qapi/visitor-impl.h | 23 +++++++++++++
 include/qapi/visitor.h      | 51 +++++++++++++++++++++++++++++
 qapi/qapi-visit-core.c      | 80 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 154 insertions(+)
include/qapi/visitor-impl.h +23 -0
index f3fa420..10cdbf7 100644
@@ -15,6 +15,9 @@
 #include "qapi/error.h"
 #include "qapi/visitor.h"
 
+#define VISITOR_SAVING (1<<0)
+#define VISITOR_LOADING (1<<1)
+
 struct Visitor
 {
     /* Must be set */
@@ -29,6 +32,10 @@ struct Visitor
     void (*start_list)(Visitor *v, const char *name, Error **errp);
     GenericList *(*next_list)(Visitor *v, GenericList **list, Error **errp);
     void (*end_list)(Visitor *v, Error **errp);
+    void (*start_array)(Visitor *v, void **obj, const char *name,
+                        size_t elem_count, size_t elem_size, Error **errp);
+    void (*next_array)(Visitor *v, Error **errp);
+    void (*end_array)(Visitor *v, Error **errp);
 
     void (*type_enum)(Visitor *v, int *obj, const char *strings[],
                       const char *kind, const char *name, Error **errp);
@@ -38,6 +45,7 @@ struct Visitor
     void (*type_int)(Visitor *v, int64_t *obj, const char *name, Error **errp);
     void (*type_bool)(Visitor *v, bool *obj, const char *name, Error **errp);
     void (*type_str)(Visitor *v, char **obj, const char *name, Error **errp);
+    void (*type_str256)(Visitor *v, char *obj, const char *name, Error **errp);
     void (*type_number)(Visitor *v, double *obj, const char *name,
                         Error **errp);
 
@@ -49,6 +57,8 @@ struct Visitor
     void (*start_handle)(Visitor *v, void **obj, const char *kind,
                          const char *name, Error **errp);
     void (*end_handle)(Visitor *v, Error **errp);
+    void (*type_buffer)(Visitor *v, void *data, size_t len, bool async,
+                        const char *name, Error **errp);
     void (*type_uint8)(Visitor *v, uint8_t *obj, const char *name, Error **errp);
     void (*type_uint16)(Visitor *v, uint16_t *obj, const char *name, Error **errp);
     void (*type_uint32)(Visitor *v, uint32_t *obj, const char *name, Error **errp);
@@ -59,6 +69,19 @@ struct Visitor
     void (*type_int64)(Visitor *v, int64_t *obj, const char *name, Error **errp);
     /* visit_type_size() falls back to (*type_uint64)() if type_size is unset */
     void (*type_size)(Visitor *v, uint64_t *obj, const char *name, Error **errp);
+
+    void (*destroy)(Visitor *v, Error **errp);
+
+    void (*start_sequence_compat)(Visitor *v, const char *name,
+                                  Visit_seq_compat_mode compat_mode,
+                                  void *opaque, Error **errp);
+    void (*end_sequence_compat)(Visitor *v, const char *name,
+                                Visit_seq_compat_mode compat_mode,
+                                Error **errp);
+
+    QEMUFile* (*get_qemufile)(Visitor *v);
+
+    uint64_t flags;
 };
 
 void input_type_enum(Visitor *v, int *obj, const char *strings[],
include/qapi/visitor.h +51 -0

68 lines not on this page · raw message

qapi/qapi-visit-core.c +80 -0

101 lines not on this page · raw message

Signature · 1 line
1.8.5.3
#2 lore raw

Re: [Qemu-devel] [RFC PATCH 01/16] Visitor: Add methods for migration format use

Quoted 7 lines · Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

array types

    From  https://lists.gnu.org/archive/html/qemu-devel/2011-09/msg02465.html

str256 type
For the upto 256byte strings QEMU commonly uses for IDs

Naive question: why do you need this when you have arrays?

Quoted 3 lines

buffer type
For a blob of data that the caller wants to deliver whole (e.g. a page of RAM or block of disk)

This smells like an array, too.

Quoted 1 line

Load/save flags to let a user perform pre-save/post-load checking

Odd. I'd expect separate visitors, one for save, one for load.

Quoted 10 lines

An accessor to get the underlying QEMUFile* (for compatibility)

compat-sequences
Provide enough information for a visitor providing compatibility
with the old format to generate it's byte stream, while allowing a new visitor to do something sensible.

destroy
Allows the caller to destroy a visitor without knowing what type of visitor it is.

When the commit message is basically a list, splitting it into one commit per list item often makes sense.

Some of them (the one introducing destroy, perhaps?) can then be put to use in the same or the next patch. Makes review much easier.

But on to the actual code.

Quoted 12 lines
---
 include/qapi/visitor-impl.h | 23 +++++++++++++
 include/qapi/visitor.h      | 51 +++++++++++++++++++++++++++++
 qapi/qapi-visit-core.c      | 80 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 154 insertions(+)
include/qapi/visitor-impl.h +0 -0
index f3fa420..10cdbf7 100644

include/qapi/visitor-impl.h line 20

Quoted 6 lines
include/qapi/visitor-impl.h +3 -0
@@ -15,6 +15,9 @@
 #include "qapi/error.h"
 #include "qapi/visitor.h"
 
+#define VISITOR_SAVING (1<<0)
+#define VISITOR_LOADING (1<<1)
+

Comment explaining their meaning and the connection to Visitor member flags needed.

Are the flags independent, i.e. all four combinations meaningful?

Quoted 4 lines
include/qapi/visitor-impl.h +0 -0
 struct Visitor
 {
     /* Must be set */

include/qapi/visitor-impl.h struct Visitor · line 41

Quoted 10 lines
include/qapi/visitor-impl.h +4 -0
@@ -29,6 +32,10 @@ struct Visitor
     void (*start_list)(Visitor *v, const char *name, Error **errp);
     GenericList *(*next_list)(Visitor *v, GenericList **list, Error **errp);
     void (*end_list)(Visitor *v, Error **errp);
+    void (*start_array)(Visitor *v, void **obj, const char *name,
+                        size_t elem_count, size_t elem_size, Error **errp);
+    void (*next_array)(Visitor *v, Error **errp);
+    void (*end_array)(Visitor *v, Error **errp);
 
     void (*type_enum)(Visitor *v, int *obj, const char *strings[],
                       const char *kind, const char *name, Error **errp);

Since these "must be set", you need to update all existing visitors to actually set them, in the same patch, don't you?

I'm not sure the existing visitors completely obey "must be set" vs. "may be null". But let's not make it worse.

include/qapi/visitor-impl.h struct Visitor · line 51

Quoted 8 lines
include/qapi/visitor-impl.h +1 -0
@@ -38,6 +45,7 @@ struct Visitor
     void (*type_int)(Visitor *v, int64_t *obj, const char *name, Error **errp);
     void (*type_bool)(Visitor *v, bool *obj, const char *name, Error **errp);
     void (*type_str)(Visitor *v, char **obj, const char *name, Error **errp);
+    void (*type_str256)(Visitor *v, char *obj, const char *name, Error **errp);
     void (*type_number)(Visitor *v, double *obj, const char *name,
                         Error **errp);
 
       /* May be NULL */

More context ^^^

Members above must be set, members below may be null.

include/qapi/visitor-impl.h struct Visitor · line 59

Quoted 4 lines
include/qapi/visitor-impl.h +0 -0
@@ -49,6 +57,8 @@ struct Visitor
     void (*start_handle)(Visitor *v, void **obj, const char *kind,
                          const char *name, Error **errp);
     void (*end_handle)(Visitor *v, Error **errp);

No longer applies (I killed end_handle() in commit cbc955).

Quoted 10 lines
include/qapi/visitor-impl.h +3 -0
+    void (*type_buffer)(Visitor *v, void *data, size_t len, bool async,
+                        const char *name, Error **errp);
     void (*type_uint8)(Visitor *v, uint8_t *obj, const char *name, Error **errp);
     void (*type_uint16)(Visitor *v, uint16_t *obj, const char *name, Error **errp);
     void (*type_uint32)(Visitor *v, uint32_t *obj, const char *name, Error **errp);
@@ -59,6 +69,19 @@ struct Visitor
     void (*type_int64)(Visitor *v, int64_t *obj, const char *name, Error **errp);
     /* visit_type_size() falls back to (*type_uint64)() if type_size is unset */
     void (*type_size)(Visitor *v, uint64_t *obj, const char *name, Error **errp);
+

include/qapi/visitor-impl.h struct Visitor · line 82

Quoted 10 lines
include/qapi/visitor-impl.h +10 -0
+    void (*destroy)(Visitor *v, Error **errp);
+
+    void (*start_sequence_compat)(Visitor *v, const char *name,
+                                  Visit_seq_compat_mode compat_mode,
+                                  void *opaque, Error **errp);
+    void (*end_sequence_compat)(Visitor *v, const char *name,
+                                Visit_seq_compat_mode compat_mode,
+                                Error **errp);
+
+    QEMUFile* (*get_qemufile)(Visitor *v);

Style nit: QEMUFile *(*get_qemufile)(Visitor *v);

The empty line before destroy makes me wonder whether these still belong to the "May be NULL" heading.

include/qapi/visitor-impl.h struct Visitor · line 84

Quoted 2 lines
include/qapi/visitor-impl.h +2 -0
+
+    uint64_t flags;

This one can't, because it can't be null :)

include/qapi/visitor-impl.h struct Visitor · line 87

Quoted 3 lines
include/qapi/visitor-impl.h +0 -0
 };
 
 void input_type_enum(Visitor *v, int *obj, const char *strings[],

Your patch drags migration-specific stuff into the until now perfectly generic struct Visitor:

* get_qemufile()

Looks temporary, thus tolerable.

* Compat sequences
* Load/save flags

These look permanent :(

I'd have to review how they're used to convince myself we actually need them.

Quoted 14 lines
include/qapi/visitor.h +5 -0
index 29da211..70c20df 100644
@@ -39,11 +39,22 @@ void visit_end_implicit_struct(Visitor *v, Error **errp);
 void visit_start_list(Visitor *v, const char *name, Error **errp);
 GenericList *visit_next_list(Visitor *v, GenericList **list, Error **errp);
 void visit_end_list(Visitor *v, Error **errp);
+void visit_start_array(Visitor *v, void **obj, const char *name,
+                       size_t elem_count, size_t elem_size, Error **errp);
+void visit_next_array(Visitor *v, Error **errp);
+void visit_end_array(Visitor *v, Error **errp);
+
 void visit_start_optional(Visitor *v, bool *present, const char *name,

include/qapi/visitor.h void visit_end_implicit_struct(Visitor *v, Error **errp); · line 57

Quoted 10 lines
include/qapi/visitor.h +6 -0
                           Error **errp);
 void visit_end_optional(Visitor *v, Error **errp);
 void visit_get_next_type(Visitor *v, int *obj, const int *qtypes,
                          const char *name, Error **errp);
+/* Blocks of guest memory,disk or otherwise opaque data that there is a lot
+ * of and must be handled efficiently. 'async' true if the write can happen
+ * 'later'
+ */
+void visit_type_buffer(Visitor *v, void *data, size_t len, bool async,
+                       const char *name, Error **errp);

I'm afraid your specification of async is tied to a specific kind of
visitor: one that "writes". Many don't.

Quoted 32 lines
include/qapi/visitor.h +24 -0
 void visit_type_enum(Visitor *v, int *obj, const char *strings[],
                      const char *kind, const char *name, Error **errp);
 void visit_type_int(Visitor *v, int64_t *obj, const char *name, Error **errp);
@@ -58,6 +69,46 @@ void visit_type_int64(Visitor *v, int64_t *obj, const char *name, Error **errp);
 void visit_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp);
 void visit_type_bool(Visitor *v, bool *obj, const char *name, Error **errp);
 void visit_type_str(Visitor *v, char **obj, const char *name, Error **errp);
+/* A string no more than 256 (including term) characters in length */
+void visit_type_str256(Visitor *v, char *obj, const char *name, Error **errp);
 void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp);
+void visit_destroy(Visitor *v, Error **errp);
+
+/* Type of visitor, used where actions have to be taken when (de)serializing */
+bool visitor_loading(Visitor *v);
+bool visitor_saving(Visitor *v);
+
+typedef enum {
+    VISIT_SEQ_COMPAT_BYTE0TERM,      /* list terminated with a 0 byte */
+    VISIT_SEQ_COMPAT_FILE,           /* The top level file object */
+    VISIT_SEQ_COMPAT_SUBSECLIST,     /* list terminated by
+                                        historical complexity */
+    VISIT_SEQ_COMPAT_SUBSECTION,     /* One subsection */
+    VISIT_SEQ_COMPAT_SECTION_HEADER, /* SECTION_FULL/START, header + name */
+    VISIT_SEQ_COMPAT_SECTION_MIN,    /* SECTION_PART/END - minimal header */
+    VISIT_SEQ_COMPAT_RAMSECLIST,     /* list terminated by int64 bit
+                                        RAM_SAVE_FLAG_EOS */
+    VISIT_SEQ_COMPAT_RAMSECENTRY,    /* Entry in RAM Sec list */
+    VISIT_SEQ_COMPAT_VMSTATE,        /* One VMState */
+    VISIT_SEQ_COMPAT_BLOB            /* A binary old-format blob */
+} Visit_seq_compat_mode;
+
+/* Start a sequence of items (which may be of unknown length and unknown

include/qapi/visitor.h void visit_type_int64(Visitor *v, int64_t *obj, const char *name, Error **errp); · line 106

Quoted 10 lines
include/qapi/visitor.h +10 -0
+ * mix of some subset of types), specify a compatibility mode that's only
+ * used by an implementation trying to match the existing binary migration
+ * format.
+ * opaque is compat_mode specific
+ */
+void visit_start_sequence_compat(Visitor *v, const char *name,
+                                 Visit_seq_compat_mode compat_mode,
+                                 void *opaque,
+                                 Error **errp);
+/* Use visit_get_next_type for each entry including the first */

I'm afraid I don't get this comment.

include/qapi/visitor.h void visit_type_int64(Visitor *v, int64_t *obj, const char *name, Error **errp); · line 112

Quoted 6 lines
include/qapi/visitor.h +6 -0
+void visit_end_sequence_compat(Visitor *v, const char *name,
+                                 Visit_seq_compat_mode compat_mode,
+                                 Error **errp);
+
+/* Don't Use! - lets us move forward until we can get rid of all file uses */
+QEMUFile *visitor_get_qemufile(Visitor *v);

Likewise.

Quoted 37 lines
include/qapi/visitor.h +0 -0
 #endif
qapi/qapi-visit-core.c +22 -0
index 6451a21..2d20fde 100644
@@ -84,6 +84,28 @@ void visit_end_list(Visitor *v, Error **errp)
     v->end_list(v, errp);
 }
 
+void visit_start_array(Visitor *v, void **obj, const char *name,
+                       size_t elem_count, size_t elem_size, Error **errp)
+{
+    if (!error_is_set(errp)) {
+        v->start_array(v, obj, name, elem_count, elem_size, errp);
+    }
+}
+
+void visit_next_array(Visitor *v, Error **errp)
+{
+    if (!error_is_set(errp)) {
+        v->next_array(v, errp);
+    }
+}
+
+void visit_end_array(Visitor *v, Error **errp)
+{
+    if (!error_is_set(errp)) {
+        v->end_array(v, errp);
+    }
+}
+
 void visit_start_optional(Visitor *v, bool *present, const char *name,
                           Error **errp)
 {
@@ -107,6 +129,14 @@ void visit_get_next_type(Visitor *v, int *obj, const int *qtypes,
     }

qapi/qapi-visit-core.c void visit_get_next_type(Visitor *v, int *obj, const int *qtypes, · line 139

Quoted 10 lines
qapi/qapi-visit-core.c +8 -0
 }
 
+void visit_type_buffer(Visitor *v, void *data, size_t len, bool async,
+                       const char *name, Error **errp)
+{
+    if (!error_is_set(errp)) {
+        v->type_buffer(v, data, len, async, name, errp);
+    }
+}
+

Looks like Visitor member type_buffer must be set. However, you put it under the "may be null" heading in visitor-impl.h.

Quoted 18 lines
qapi/qapi-visit-core.c +7 -0
 void visit_type_enum(Visitor *v, int *obj, const char *strings[],
                      const char *kind, const char *name, Error **errp)
 {
@@ -291,6 +321,13 @@ void visit_type_str(Visitor *v, char **obj, const char *name, Error **errp)
     }
 }
 
+void visit_type_str256(Visitor *v, char *obj, const char *name, Error **errp)
+{
+    if (!error_is_set(errp)) {
+        v->type_str256(v, obj, name, errp);
+    }
+}
+
 void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp)
 {
     if (!error_is_set(errp)) {

qapi/qapi-visit-core.c void input_type_enum(Visitor *v, int *obj, const char *strings[], · line 391

Quoted 8 lines
qapi/qapi-visit-core.c +5 -0
@@ -347,3 +384,46 @@ void input_type_enum(Visitor *v, int *obj, const char *strings[],
     g_free(enum_str);
     *obj = value;
 }
+
+void visit_destroy(Visitor *v, Error **errp)
+{
+    v->destroy(v, errp);
+}

Likewise.

qapi/qapi-visit-core.c void input_type_enum(Visitor *v, int *obj, const char *strings[], · line 402

Quoted 11 lines
qapi/qapi-visit-core.c +11 -0
+
+void visit_start_sequence_compat(Visitor *v, const char *name,
+                                 Visit_seq_compat_mode compat_mode,
+                                 void *opaque, Error **errp)
+{
+    if (error_is_set(errp)) {
+        return;
+    }
+
+    v->start_sequence_compat(v, name, compat_mode, opaque, errp);
+}

Likewise.

qapi/qapi-visit-core.c void input_type_enum(Visitor *v, int *obj, const char *strings[], · line 413

Quoted 11 lines
qapi/qapi-visit-core.c +11 -0
+
+void visit_end_sequence_compat(Visitor *v, const char *name,
+                                 Visit_seq_compat_mode compat_mode,
+                               Error **errp)
+{
+    if (error_is_set(errp)) {
+        return;
+    }
+
+    v->end_sequence_compat(v, name, compat_mode, errp);
+}

Likewise.

qapi/qapi-visit-core.c void input_type_enum(Visitor *v, int *obj, const char *strings[], · line 418

Quoted 5 lines
qapi/qapi-visit-core.c +5 -0
+
+QEMUFile *visitor_get_qemufile(Visitor *v)
+{
+    return v->get_qemufile(v);
+}

Likewise.

Quoted 11 lines
qapi/qapi-visit-core.c +11 -0
+
+bool visitor_loading(Visitor *v)
+{
+    return v->flags & VISITOR_LOADING;
+}
+
+bool visitor_saving(Visitor *v)
+{
+    return v->flags & VISITOR_SAVING;
+}
+
#3 lore raw

Re: [Qemu-devel] [RFC PATCH 01/16] Visitor: Add methods for migration format use

Quoted 2 lines · * Markus Armbruster ([email protected]) wrote:

"Dr. David Alan Gilbert (git)" <[email protected]> writes:

Hi Markus,
Thanks for the review. I've reordered your comments to group some of the
replies together.

Quoted 9 lines
Quoted 1 line

From: "Dr. David Alan Gilbert" <[email protected]>

Your patch drags migration-specific stuff into the until now perfectly generic struct Visitor:

* get_qemufile()

Looks temporary, thus tolerable.

Quoted 7 lines

* Compat sequences
* Load/save flags

These look permanent :(

I'd have to review how they're used to convince myself we actually need them.

Right - and this is the one I actually wanted you to look at this series for; I know it feels weird, and I'm up for better suggestions
I've toyed with the idea of having a MigrationVisitor subclass - ie just like the visitor except the few additions; but how would I cleanly make it a subclass in this setup?

Quoted 6 lines
Quoted 5 lines

array types

    From  https://lists.gnu.org/archive/html/qemu-devel/2011-09/msg02465.html

str256 type
For the upto 256byte strings QEMU commonly uses for IDs

Quoted 7 lines

Naive question: why do you need this when you have arrays?

Quoted 3 lines

buffer type
For a blob of data that the caller wants to deliver whole (e.g. a page of RAM or block of disk)

This smells like an array, too.

I'd assumed that the point of the 'array' was something where you started an array and then visited each member (with the appropriate visitor type for the type of the member).

I use arrays for things that are declared as such in vmstate and which I know the internal types of;

BER has various string types and it makes sense to me to use that for readable strings. The other reason in this case is that the binary-visitor knows to format this in the way compatible with the existing length+text string formatting.

Where there is a blob of data which the visitor code isn't going to get to know the inside of, that's a buffer.

Quoted 3 lines
Quoted 1 line

Load/save flags to let a user perform pre-save/post-load checking

Odd. I'd expect separate visitors, one for save, one for load.

Indeed they are - these flags are to allow a caller to know whether it's being visited for the purpose of saving or loading;
In a non-visitor world you typically have a:
load_state
save_state
function; in the visit world you more commonly have a:
visit_state
function; for a lot of the simpler types this works fine - but when you want to do some reformatting or checking of your data then you really want to know if you're saving or loading.
The other way of doing it would still be to have a load_state/save_state that just happen to call the same function in most cases.

Quoted 8 lines

include/qapi/visitor-impl.h line 20

Quoted 3 lines
include/qapi/visitor-impl.h +3 -0
+#define VISITOR_SAVING (1<<0)
+#define VISITOR_LOADING (1<<1)
+

Comment explaining their meaning and the connection to Visitor member flags needed.

Are the flags independent, i.e. all four combinations meaningful?

They're only two states, and I could have encoded it as one bit in the flag.

Quoted 11 lines
Quoted 10 lines

An accessor to get the underlying QEMUFile* (for compatibility)

compat-sequences
Provide enough information for a visitor providing compatibility
with the old format to generate it's byte stream, while allowing a new visitor to do something sensible.

destroy
Allows the caller to destroy a visitor without knowing what type of visitor it is.

Quoted 5 lines

When the commit message is basically a list, splitting it into one commit per list item often makes sense.

Some of them (the one introducing destroy, perhaps?) can then be put to use in the same or the next patch. Makes review much easier.

Yep, happy to split.

Quoted 30 lines

But on to the actual code.

Quoted 28 lines
---
 include/qapi/visitor-impl.h | 23 +++++++++++++
 include/qapi/visitor.h      | 51 +++++++++++++++++++++++++++++
 qapi/qapi-visit-core.c      | 80 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 154 insertions(+)
include/qapi/visitor-impl.h +4 -0
index f3fa420..10cdbf7 100644
@@ -15,6 +15,9 @@
 #include "qapi/error.h"
 #include "qapi/visitor.h"
 
 
 struct Visitor
 {
     /* Must be set */
@@ -29,6 +32,10 @@ struct Visitor
     void (*start_list)(Visitor *v, const char *name, Error **errp);
     GenericList *(*next_list)(Visitor *v, GenericList **list, Error **errp);
     void (*end_list)(Visitor *v, Error **errp);
+    void (*start_array)(Visitor *v, void **obj, const char *name,
+                        size_t elem_count, size_t elem_size, Error **errp);
+    void (*next_array)(Visitor *v, Error **errp);
+    void (*end_array)(Visitor *v, Error **errp);
 
Quoted 8 lines

include/qapi/visitor-impl.h struct Visitor · line 41

Quoted 2 lines
include/qapi/visitor-impl.h +0 -0
     void (*type_enum)(Visitor *v, int *obj, const char *strings[],
                       const char *kind, const char *name, Error **errp);

Since these "must be set", you need to update all existing visitors to actually set them, in the same patch, don't you?

I'm not sure the existing visitors completely obey "must be set" vs. "may be null". But let's not make it worse.

Yep; you've picked me up on a whole bunch of the 'must be set'/'may be null'
- I'll treat those all as one and go through and tidy them up.

Quoted 8 lines

Members above must be set, members below may be null.

include/qapi/visitor-impl.h struct Visitor · line 59

Quoted 4 lines
include/qapi/visitor-impl.h +0 -0
@@ -49,6 +57,8 @@ struct Visitor
     void (*start_handle)(Visitor *v, void **obj, const char *kind,
                          const char *name, Error **errp);
     void (*end_handle)(Visitor *v, Error **errp);

No longer applies (I killed end_handle() in commit cbc955).

Yep; it's dead in my working tree.

Quoted 14 lines
Quoted 14 lines
include/qapi/visitor-impl.h +7 -0
+    void (*type_buffer)(Visitor *v, void *data, size_t len, bool async,
+                        const char *name, Error **errp);
     void (*type_uint8)(Visitor *v, uint8_t *obj, const char *name, Error **errp);
     void (*type_uint16)(Visitor *v, uint16_t *obj, const char *name, Error **errp);
     void (*type_uint32)(Visitor *v, uint32_t *obj, const char *name, Error **errp);
@@ -59,6 +69,19 @@ struct Visitor
     void (*type_int64)(Visitor *v, int64_t *obj, const char *name, Error **errp);
     /* visit_type_size() falls back to (*type_uint64)() if type_size is unset */
     void (*type_size)(Visitor *v, uint64_t *obj, const char *name, Error **errp);
+
+    void (*destroy)(Visitor *v, Error **errp);
+
+    void (*start_sequence_compat)(Visitor *v, const char *name,
+                                  Visit_seq_compat_mode compat_mode,
Quoted 8 lines

include/qapi/visitor-impl.h struct Visitor · line 82

Quoted 6 lines
include/qapi/visitor-impl.h +6 -0
+                                  void *opaque, Error **errp);
+    void (*end_sequence_compat)(Visitor *v, const char *name,
+                                Visit_seq_compat_mode compat_mode,
+                                Error **errp);
+
+    QEMUFile* (*get_qemufile)(Visitor *v);

Style nit: QEMUFile *(*get_qemufile)(Visitor *v);

Fixed.

Quoted 9 lines

The empty line before destroy makes me wonder whether these still belong to the "May be NULL" heading.

include/qapi/visitor-impl.h struct Visitor · line 84

Quoted 2 lines
include/qapi/visitor-impl.h +2 -0
+
+    uint64_t flags;

This one can't, because it can't be null :)

Quoted 9 lines

include/qapi/visitor-impl.h struct Visitor

Quoted 6 lines
include/qapi/visitor-impl.h +6 -0
+/* Blocks of guest memory,disk or otherwise opaque data that there is a lot
+ * of and must be handled efficiently. 'async' true if the write can happen
+ * 'later'
+ */
+void visit_type_buffer(Visitor *v, void *data, size_t len, bool async,
+                       const char *name, Error **errp);

I'm afraid your specification of async is tied to a specific kind of
visitor: one that "writes". Many don't.

Do you have a suggestion on how to do this better? Async accesses were put in some time ago apparently for a useful speed increase, so I can't remove them, and anyway they make sense for the writes; I guess you could argue that they could be used for read as well, if the time at which the 'async' had to have completed was defined, but that's needed for the write anyway.

Quoted 40 lines
Quoted 40 lines

void visit_type_enum(Visitor *v, int *obj, const char *strings[],

                      const char *kind, const char *name, Error **errp);

void visit_type_int(Visitor *v, int64_t *obj, const char *name, Error **errp);

include/qapi/visitor-impl.h +32 -0
@@ -58,6 +69,46 @@ void visit_type_int64(Visitor *v, int64_t *obj, const char *name, Error **errp);
 void visit_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp);
 void visit_type_bool(Visitor *v, bool *obj, const char *name, Error **errp);
 void visit_type_str(Visitor *v, char **obj, const char *name, Error **errp);
+/* A string no more than 256 (including term) characters in length */
+void visit_type_str256(Visitor *v, char *obj, const char *name, Error **errp);
 void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp);
+void visit_destroy(Visitor *v, Error **errp);
+
+/* Type of visitor, used where actions have to be taken when (de)serializing */
+bool visitor_loading(Visitor *v);
+bool visitor_saving(Visitor *v);
+
+typedef enum {
+    VISIT_SEQ_COMPAT_BYTE0TERM,      /* list terminated with a 0 byte */
+    VISIT_SEQ_COMPAT_FILE,           /* The top level file object */
+    VISIT_SEQ_COMPAT_SUBSECLIST,     /* list terminated by
+                                        historical complexity */
+    VISIT_SEQ_COMPAT_SUBSECTION,     /* One subsection */
+    VISIT_SEQ_COMPAT_SECTION_HEADER, /* SECTION_FULL/START, header + name */
+    VISIT_SEQ_COMPAT_SECTION_MIN,    /* SECTION_PART/END - minimal header */
+    VISIT_SEQ_COMPAT_RAMSECLIST,     /* list terminated by int64 bit
+                                        RAM_SAVE_FLAG_EOS */
+    VISIT_SEQ_COMPAT_RAMSECENTRY,    /* Entry in RAM Sec list */
+    VISIT_SEQ_COMPAT_VMSTATE,        /* One VMState */
+    VISIT_SEQ_COMPAT_BLOB            /* A binary old-format blob */
+} Visit_seq_compat_mode;
+
+/* Start a sequence of items (which may be of unknown length and unknown
+ * mix of some subset of types), specify a compatibility mode that's only
+ * used by an implementation trying to match the existing binary migration
+ * format.
+ * opaque is compat_mode specific
+ */
+void visit_start_sequence_compat(Visitor *v, const char *name,
+                                 Visit_seq_compat_mode compat_mode,
+                                 void *opaque,
Quoted 8 lines

include/qapi/visitor-impl.h void visit_type_int64(Visitor *v, int64_t *obj, const char *name, Error **errp); · line 110

Quoted 6 lines
include/qapi/visitor-impl.h +6 -0
+                                 Error **errp);
+/* Use visit_get_next_type for each entry including the first */
+void visit_end_sequence_compat(Visitor *v, const char *name,
+                                 Visit_seq_compat_mode compat_mode,
+                                 Error **errp);
+

I'm afraid I don't get this comment.

Quite a lot of the structures I need to keep compatibility with are lists of some mixture of different things with a terminator on the end (there's not much consistency in the binary format about how you tell what's next or what the terminator is).
So the typical use is:

visit_start_sequence_compat(...)
while (visit_get_next_type( &flag ), flag != end_flag) {
switch (flag) {
case A:
do whatever you need to do now that we've found an 'A' in the list (typically another visit_start_sequence_compat for that type)
case B:
do whatever you need to do now that we've found an 'B' in the list
....

       }
    }
    visit_end_sequence_compat(...)

An example is a RAM migration section; it's a list of initialisation data, normal pages, all-zero pages, XBZRLE pages

Quoted 4 lines

include/qapi/visitor-impl.h void visit_type_int64(Visitor *v, int64_t *obj, const char *name, Error **errp); · line 112

Quoted 2 lines
include/qapi/visitor-impl.h +2 -0
+/* Don't Use! - lets us move forward until we can get rid of all file uses */
+QEMUFile *visitor_get_qemufile(Visitor *v);

Likewise.

I'm hoping this is a temporary shim - my hope eventually is that we won't need to use this and everywhere that uses Visitors won't also need to get a QEMUFile except internally to the visitor.

Dave

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#4 lore raw

Re: [Qemu-devel] [RFC PATCH 01/16] Visitor: Add methods for migration format use

Quoted 2 lines · * Dr. David Alan Gilbert ([email protected]) wrote:
Quoted 1 line · * Markus Armbruster ([email protected]) wrote:

* Dr. David Alan Gilbert ([email protected]) wrote:

<snip>

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Load/save flags to let a user perform pre-save/post-load checking

Odd. I'd expect separate visitors, one for save, one for load.

Indeed they are - these flags are to allow a caller to know whether

Oops that's a typo - 'Indeed there are separate load and save visitors'

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#5 lore raw

Re: [Qemu-devel] [RFC PATCH 01/16] Visitor: Add methods for migration format use

In reply to Dr. David Alan Gilbert
Quoted 22 lines · Dr. David Alan Gilbert
Quoted 2 lines · * Markus Armbruster ([email protected]) wrote:

"Dr. David Alan Gilbert (git)" <[email protected]> writes:

Hi Markus,
Thanks for the review. I've reordered your comments to group some of the
replies together.

Quoted 14 lines
Quoted 1 line

From: "Dr. David Alan Gilbert" <[email protected]>

Your patch drags migration-specific stuff into the until now perfectly generic struct Visitor:

* get_qemufile()

Looks temporary, thus tolerable.

* Compat sequences
* Load/save flags

These look permanent :(

Quoted 8 lines
Quoted 2 lines

I'd have to review how they're used to convince myself we actually need them.

Right - and this is the one I actually wanted you to look at this series for; I know it feels weird, and I'm up for better suggestions
I've toyed with the idea of having a MigrationVisitor subclass - ie just like the visitor except the few additions; but how would I cleanly make it a subclass in this setup?

To help with that, I need to understand the problems your additional functionality solves. I could try to find out by studying the rest of this series, but it'll take time. Perhaps I can find some next week.

Quoted 19 lines
Quoted 7 lines
Quoted 6 lines

array types
From
https://lists.gnu.org/archive/html/qemu-devel/2011-09/msg02465.html

str256 type
For the upto 256byte strings QEMU commonly uses for IDs

Quoted 7 lines

Naive question: why do you need this when you have arrays?

Quoted 3 lines

buffer type
For a blob of data that the caller wants to deliver whole (e.g. a page of RAM or block of disk)

This smells like an array, too.

I'd assumed that the point of the 'array' was something where you started an array and then visited each member (with the appropriate visitor type for the type of the member).

Quoted 7 lines

I use arrays for things that are declared as such in vmstate and which I know the internal types of;

BER has various string types and it makes sense to me to use that for readable strings. The other reason in this case is that the binary-visitor knows to format this in the way compatible with the existing length+text string formatting.

Point taken: while (unboxed) strings really are just special arrays, it can make sense to treat them specially.

But limiting it to arrays of exactly 256 characters is lame :) Would generalizing to arbitrary size be hard?

This one's not specific to migration.

Quoted 2 lines

Where there is a blob of data which the visitor code isn't going to get to know the inside of, that's a buffer.

Similar argument as for strings: blobs are just special arrays, but it can make sense to treat them specially. Not sure I find it convincing, but examining actual users tends to fix that.

Again, not necessarily specific to migration.

Quoted 4 lines
Quoted 3 lines
Quoted 1 line

Load/save flags to let a user perform pre-save/post-load checking

Odd. I'd expect separate visitors, one for save, one for load.

Quoted 12 lines

Indeed they are - these flags are to allow a caller to know whether it's being visited for the purpose of saving or loading;
In a non-visitor world you typically have a:
load_state
save_state
function; in the visit world you more commonly have a:
visit_state
function; for a lot of the simpler types this works fine - but when you want to do some reformatting or checking of your data then you really want to know if you're saving or loading.
The other way of doing it would still be to have a load_state/save_state that just happen to call the same function in most cases.

The purpose of visitors is to separate data structure traversal and what to do with the data.

An implementation of the Visitor type encapsulates "what to do". The code passing a Visitor instance v to the visit_FOO(v, ...) does the traversal.

In other words, the abstract Visitor type hides "what to do".

Your flags leak "what to do" bits into the abstract Visitor type interface. Unclean.

The orthodox way to provide additional data to a concrete visitor is extending the abstract Visitor type:

    struct FooVisitor {
        Visitor visitor;
        ... Foo-specific stuff ...
    }

This is how the existing visitors work. Could this work for you, too?

Quoted 10 lines
Quoted 8 lines

include/qapi/visitor-impl.h line 20

Quoted 3 lines
include/qapi/visitor-impl.h +3 -0
+#define VISITOR_SAVING (1<<0)
+#define VISITOR_LOADING (1<<1)
+

Comment explaining their meaning and the connection to Visitor member flags needed.

Are the flags independent, i.e. all four combinations meaningful?

They're only two states, and I could have encoded it as one bit in the flag.

I prefer to avoid invalid states.

Quoted 64 lines
Quoted 11 lines
Quoted 10 lines

An accessor to get the underlying QEMUFile* (for compatibility)

compat-sequences
Provide enough information for a visitor providing compatibility
with the old format to generate it's byte stream, while allowing a new visitor to do something sensible.

destroy
Allows the caller to destroy a visitor without knowing what type of visitor it is.

Quoted 5 lines

When the commit message is basically a list, splitting it into one commit per list item often makes sense.

Some of them (the one introducing destroy, perhaps?) can then be put to use in the same or the next patch. Makes review much easier.

Yep, happy to split.

Quoted 31 lines

But on to the actual code.

Quoted 29 lines
---
 include/qapi/visitor-impl.h | 23 +++++++++++++
 include/qapi/visitor.h      | 51 +++++++++++++++++++++++++++++
 qapi/qapi-visit-core.c | 80
+++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 154 insertions(+)
include/qapi/visitor-impl.h +4 -0
index f3fa420..10cdbf7 100644
@@ -15,6 +15,9 @@
 #include "qapi/error.h"
 #include "qapi/visitor.h"
 
 
 struct Visitor
 {
     /* Must be set */
@@ -29,6 +32,10 @@ struct Visitor
     void (*start_list)(Visitor *v, const char *name, Error **errp);
     GenericList *(*next_list)(Visitor *v, GenericList **list, Error **errp);
     void (*end_list)(Visitor *v, Error **errp);
+    void (*start_array)(Visitor *v, void **obj, const char *name,
+                        size_t elem_count, size_t elem_size, Error **errp);
+    void (*next_array)(Visitor *v, Error **errp);
+    void (*end_array)(Visitor *v, Error **errp);
 
Quoted 8 lines

include/qapi/visitor-impl.h struct Visitor · line 41

Quoted 2 lines
include/qapi/visitor-impl.h +0 -0
     void (*type_enum)(Visitor *v, int *obj, const char *strings[],
                       const char *kind, const char *name, Error **errp);

Since these "must be set", you need to update all existing visitors to actually set them, in the same patch, don't you?

I'm not sure the existing visitors completely obey "must be set" vs. "may be null". But let's not make it worse.

Yep; you've picked me up on a whole bunch of the 'must be set'/'may be null'
- I'll treat those all as one and go through and tidy them up.

Quoted 2 lines

Members above must be set, members below may be null.

Quoted 8 lines
Quoted 6 lines

include/qapi/visitor-impl.h struct Visitor · line 59

Quoted 4 lines
include/qapi/visitor-impl.h +0 -0
@@ -49,6 +57,8 @@ struct Visitor
     void (*start_handle)(Visitor *v, void **obj, const char *kind,
                          const char *name, Error **errp);
     void (*end_handle)(Visitor *v, Error **errp);

No longer applies (I killed end_handle() in commit cbc955).

Yep; it's dead in my working tree.

When I get to studying how you put this stuff to use, I'll want a current version. Happy to pull, just tell me where from.

Quoted 50 lines
Quoted 20 lines
Quoted 20 lines
include/qapi/visitor-impl.h +2 -0
+    void (*type_buffer)(Visitor *v, void *data, size_t len, bool async,
+                        const char *name, Error **errp);
     void (*type_uint8)(Visitor *v, uint8_t *obj, const char
 *name, Error **errp);
     void (*type_uint16)(Visitor *v, uint16_t *obj, const char

*name, Error **errp);
void (*type_uint32)(Visitor *v, uint32_t *obj, const char
*name, Error **errp);

include/qapi/visitor-impl.h +0 -0
@@ -59,6 +69,19 @@ struct Visitor
     void (*type_int64)(Visitor *v, int64_t *obj, const char
 *name, Error **errp);
     /* visit_type_size() falls back to (*type_uint64)() if

type_size is unset */
void (*type_size)(Visitor *v, uint64_t *obj, const char
*name, Error **errp);

include/qapi/visitor-impl.h +5 -0
+
+    void (*destroy)(Visitor *v, Error **errp);
+
+    void (*start_sequence_compat)(Visitor *v, const char *name,
+                                  Visit_seq_compat_mode compat_mode,
Quoted 8 lines

include/qapi/visitor-impl.h struct Visitor · line 82

Quoted 6 lines
include/qapi/visitor-impl.h +6 -0
+                                  void *opaque, Error **errp);
+    void (*end_sequence_compat)(Visitor *v, const char *name,
+                                Visit_seq_compat_mode compat_mode,
+                                Error **errp);
+
+    QEMUFile* (*get_qemufile)(Visitor *v);

Style nit: QEMUFile *(*get_qemufile)(Visitor *v);

Fixed.

Quoted 9 lines

The empty line before destroy makes me wonder whether these still belong to the "May be NULL" heading.

include/qapi/visitor-impl.h

Quoted 2 lines
include/qapi/visitor-impl.h +2 -0
+
+    uint64_t flags;

This one can't, because it can't be null :)

Quoted 9 lines

include/qapi/visitor-impl.h

Quoted 6 lines
include/qapi/visitor-impl.h +6 -0
+/* Blocks of guest memory,disk or otherwise opaque data that there is a lot
+ * of and must be handled efficiently. 'async' true if the write can happen
+ * 'later'
+ */
+void visit_type_buffer(Visitor *v, void *data, size_t len, bool async,
+                       const char *name, Error **errp);

I'm afraid your specification of async is tied to a specific kind of
visitor: one that "writes". Many don't.

Quoted 6 lines

Do you have a suggestion on how to do this better? Async accesses were put in some time ago apparently for a useful speed increase, so I can't remove them, and anyway they make sense for the writes; I guess you could argue that they could be used for read as well, if the time at which the 'async' had to have completed was defined, but that's needed for the write anyway.

How do we decide async / sync?

Quoted 64 lines
Quoted 40 lines
Quoted 40 lines

void visit_type_enum(Visitor *v, int *obj, const char *strings[],

                      const char *kind, const char *name, Error **errp);

void visit_type_int(Visitor *v, int64_t *obj, const char *name, Error **errp);

include/qapi/visitor-impl.h +32 -0
@@ -58,6 +69,46 @@ void visit_type_int64(Visitor *v, int64_t *obj, const char *name, Error **errp);
 void visit_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp);
 void visit_type_bool(Visitor *v, bool *obj, const char *name, Error **errp);
 void visit_type_str(Visitor *v, char **obj, const char *name, Error **errp);
+/* A string no more than 256 (including term) characters in length */
+void visit_type_str256(Visitor *v, char *obj, const char *name, Error **errp);
 void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp);
+void visit_destroy(Visitor *v, Error **errp);
+
+/* Type of visitor, used where actions have to be taken when (de)serializing */
+bool visitor_loading(Visitor *v);
+bool visitor_saving(Visitor *v);
+
+typedef enum {
+    VISIT_SEQ_COMPAT_BYTE0TERM,      /* list terminated with a 0 byte */
+    VISIT_SEQ_COMPAT_FILE,           /* The top level file object */
+    VISIT_SEQ_COMPAT_SUBSECLIST,     /* list terminated by
+                                        historical complexity */
+    VISIT_SEQ_COMPAT_SUBSECTION,     /* One subsection */
+    VISIT_SEQ_COMPAT_SECTION_HEADER, /* SECTION_FULL/START, header + name */
+    VISIT_SEQ_COMPAT_SECTION_MIN,    /* SECTION_PART/END - minimal header */
+    VISIT_SEQ_COMPAT_RAMSECLIST,     /* list terminated by int64 bit
+                                        RAM_SAVE_FLAG_EOS */
+    VISIT_SEQ_COMPAT_RAMSECENTRY,    /* Entry in RAM Sec list */
+    VISIT_SEQ_COMPAT_VMSTATE,        /* One VMState */
+    VISIT_SEQ_COMPAT_BLOB            /* A binary old-format blob */
+} Visit_seq_compat_mode;
+
+/* Start a sequence of items (which may be of unknown length and unknown
+ * mix of some subset of types), specify a compatibility mode that's only
+ * used by an implementation trying to match the existing binary migration
+ * format.
+ * opaque is compat_mode specific
+ */
+void visit_start_sequence_compat(Visitor *v, const char *name,
+                                 Visit_seq_compat_mode compat_mode,
+                                 void *opaque,
Quoted 8 lines

include/qapi/visitor-impl.h void visit_type_int64(Visitor *v, int64_t *obj, const char *name, Error **errp); · line 110

Quoted 6 lines
include/qapi/visitor-impl.h +6 -0
+                                 Error **errp);
+/* Use visit_get_next_type for each entry including the first */
+void visit_end_sequence_compat(Visitor *v, const char *name,
+                                 Visit_seq_compat_mode compat_mode,
+                                 Error **errp);
+

I'm afraid I don't get this comment.

Quite a lot of the structures I need to keep compatibility with are lists of some mixture of different things with a terminator on the end (there's not much consistency in the binary format about how you tell what's next or what the terminator is).
So the typical use is:

visit_start_sequence_compat(...)
while (visit_get_next_type( &flag ), flag != end_flag) {
switch (flag) {
case A:
do whatever you need to do now that we've found an 'A' in the list (typically another visit_start_sequence_compat for that type)
case B:
do whatever you need to do now that we've found an 'B' in the list
....

Quoted 6 lines
       }
    }
    visit_end_sequence_compat(...)

An example is a RAM migration section; it's a list of initialisation data, normal pages, all-zero pages, XBZRLE pages

Doesn't match the existing visit_get_next_type(). I guess I need more context. One way to get it is study your patches.

Quoted 10 lines
Quoted 5 lines
Quoted 3 lines
include/qapi/visitor-impl.h +1 -0
+/* Don't Use! - lets us move forward until we can get rid of all

file uses */
+QEMUFile *visitor_get_qemufile(Visitor *v);

Likewise.

I'm hoping this is a temporary shim - my hope eventually is that we won't need to
use this and everywhere that uses Visitors won't also need to get a QEMUFile except internally to the visitor.

Suggest to put that in the comment.

#6 lore raw

Re: [Qemu-devel] [RFC PATCH 01/16] Visitor: Add methods for migration format use

Quoted 33 lines · * Markus Armbruster ([email protected]) wrote:
Quoted 22 lines · Dr. David Alan Gilbert
Quoted 2 lines · * Markus Armbruster ([email protected]) wrote:

"Dr. David Alan Gilbert (git)" <[email protected]> writes:

Hi Markus,
Thanks for the review. I've reordered your comments to group some of the
replies together.

Quoted 14 lines
Quoted 1 line

From: "Dr. David Alan Gilbert" <[email protected]>

Your patch drags migration-specific stuff into the until now perfectly generic struct Visitor:

* get_qemufile()

Looks temporary, thus tolerable.

* Compat sequences
* Load/save flags

These look permanent :(

Quoted 8 lines
Quoted 2 lines

I'd have to review how they're used to convince myself we actually need them.

Right - and this is the one I actually wanted you to look at this series for; I know it feels weird, and I'm up for better suggestions
I've toyed with the idea of having a MigrationVisitor subclass - ie just like the visitor except the few additions; but how would I cleanly make it a subclass in this setup?

Quoted 3 lines

To help with that, I need to understand the problems your additional functionality solves. I could try to find out by studying the rest of this series, but it'll take time. Perhaps I can find some next week.

Hmm, that means I need to beef up my comments/commit messages to make that more obvious.

OK, the quick overview is that I want to abstract out of the devices/ram/etc code any notion of the format of the migration file, and then support two different formats, the current binary format and the new BER format. I end up with 4 visitors for each of (Binary, BER) x (Input, output).

The tricky bit for the visitor inteface is matching the existing binary format; ignoring the VMState (which is pretty easy) all the rest of the migration format is frankly pretty random in the way it deals with lists of items or special formatting, and each one needs treating slightly specially; although many are similar.

I didn't want to create a separate visitor method for each weird case in the binary format, and so I created 'visit_start_sequence_compat' which takes an enum telling it which of the special cases it's dealing with (and a pointer to a data structure for that case).

Quoted 27 lines
Quoted 19 lines
Quoted 7 lines
Quoted 6 lines

array types
From
https://lists.gnu.org/archive/html/qemu-devel/2011-09/msg02465.html

str256 type
For the upto 256byte strings QEMU commonly uses for IDs

Quoted 7 lines

Naive question: why do you need this when you have arrays?

Quoted 3 lines

buffer type
For a blob of data that the caller wants to deliver whole (e.g. a page of RAM or block of disk)

This smells like an array, too.

I'd assumed that the point of the 'array' was something where you started an array and then visited each member (with the appropriate visitor type for the type of the member).

Quoted 7 lines

I use arrays for things that are declared as such in vmstate and which I know the internal types of;

BER has various string types and it makes sense to me to use that for readable strings. The other reason in this case is that the binary-visitor knows to format this in the way compatible with the existing length+text string formatting.

Quoted 7 lines

Point taken: while (unboxed) strings really are just special arrays, it can make sense to treat them specially.

But limiting it to arrays of exactly 256 characters is lame :) Would generalizing to arbitrary size be hard?

This one's not specific to migration.

Probably not too hard; the problem I have here is that I know that the binary visitor must encode that as a single byte length followed by the string; now I could just take the '256' off the name and error in the case of someone passing a string longer than 255 bytes
to it for the binary visitor. The other thing I would have to
do is add a string copy (at the moment the input visitor here assumes it's passed a pointer to a nice roomy 256 byte buffer).

Quoted 8 lines
Quoted 2 lines

Where there is a blob of data which the visitor code isn't going to get to know the inside of, that's a buffer.

Similar argument as for strings: blobs are just special arrays, but it can make sense to treat them specially. Not sure I find it convincing, but examining actual users tends to fix that.

Again, not necessarily specific to migration.

Let me give some examples from a decoded BER stream (from the unber tool):

A string:
<P O="105" T="[UNIVERSAL 12]" TL="2" V="22" A="UTF8String">0000:00:03.0/e1000.rom</P>

A buffer:
<P O="289" T="[UNIVERSAL 4]" TL="4" V="4096" A="OCTET STRING"> (Now follows 4k of hex data)</P>

An array:

     <I O="1589207" T="[UNIVERSAL 16]" TL="2" V="Indefinite" A="SEQUENCE">
        <P O="1589209" T="[UNIVERSAL 2]" TL="2" V="2" A="INTEGER" F>4416</P>
        <P O="1589213" T="[UNIVERSAL 2]" TL="2" V="2" A="INTEGER" F>31053</P>
        <P O="1589217" T="[UNIVERSAL 2]" TL="2" V="2" A="INTEGER" F>321</P>
        <P O="1589221" T="[UNIVERSAL 2]" TL="2" V="2" A="INTEGER" F>3104</P>
        <P O="1589311" T="[UNIVERSAL 2]" TL="2" V="1" A="INTEGER" F>0</P>
        <P O="1589314" T="[UNIVERSAL 2]" TL="2" V="1" A="INTEGER" F>0</P>
     </I O="1589317" T="[UNIVERSAL 0]" TL="2" L="112">
Quoted 32 lines
Quoted 4 lines
Quoted 3 lines
Quoted 1 line

Load/save flags to let a user perform pre-save/post-load checking

Odd. I'd expect separate visitors, one for save, one for load.

Quoted 12 lines

Indeed they are - these flags are to allow a caller to know whether it's being visited for the purpose of saving or loading;
In a non-visitor world you typically have a:
load_state
save_state
function; in the visit world you more commonly have a:
visit_state
function; for a lot of the simpler types this works fine - but when you want to do some reformatting or checking of your data then you really want to know if you're saving or loading.
The other way of doing it would still be to have a load_state/save_state that just happen to call the same function in most cases.

The purpose of visitors is to separate data structure traversal and what to do with the data.

An implementation of the Visitor type encapsulates "what to do". The code passing a Visitor instance v to the visit_FOO(v, ...) does the traversal.

In other words, the abstract Visitor type hides "what to do".

Your flags leak "what to do" bits into the abstract Visitor type interface. Unclean.

The orthodox way to provide additional data to a concrete visitor is extending the abstract Visitor type:

Quoted 6 lines
    struct FooVisitor {
        Visitor visitor;
        ... Foo-specific stuff ...
    }

This is how the existing visitors work. Could this work for you, too?

Note that these flags are only accessed by the:

  bool visitor_loading(Visitor *v);
  bool visitor_saving(Visitor *v);

methods; those magic constants are never used by the callers.

an example user is traversing the int32_le types in vmstate:

static int visit_int32_le(Visitor *v, void *pdata, const char *name,
size_t size, Error **errp)

{
    if (visitor_loading(v)) {
        int32_t loaded;

        visit_type_int32(v, &loaded, name, errp);
        if (loaded >= 0 && loaded <= *(int32_t *)pdata) {
            *(int32_t *)pdata = loaded;
            return 0;
        }
        error_setg(errp, "Unexpected value for field '%s', expected a value"
                   " no larger than %d but got %d", name, *(int32_t *)pdata,
                   loaded);
        return -EINVAL;
    } else {
        visit_type_int32(v, (int32_t *)pdata, name, errp);
        return 0;
    }

}

all the more basic vmstate wrappers just directly call visit_type_whatever without careing if they're load/store.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

include/qapi/visitor-impl.h line 20

Quoted 3 lines
include/qapi/visitor-impl.h +3 -0
+#define VISITOR_SAVING (1<<0)
+#define VISITOR_LOADING (1<<1)
+

Comment explaining their meaning and the connection to Visitor member flags needed.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Are the flags independent, i.e. all four combinations meaningful?

They're only two states, and I could have encoded it as one bit in the flag.

I prefer to avoid invalid states.

Yep, fair enough.

Quoted 71 lines
Quoted 71 lines
Quoted 11 lines
Quoted 10 lines

An accessor to get the underlying QEMUFile* (for compatibility)

compat-sequences
Provide enough information for a visitor providing compatibility
with the old format to generate it's byte stream, while allowing a new visitor to do something sensible.

destroy
Allows the caller to destroy a visitor without knowing what type of visitor it is.

Quoted 5 lines

When the commit message is basically a list, splitting it into one commit per list item often makes sense.

Some of them (the one introducing destroy, perhaps?) can then be put to use in the same or the next patch. Makes review much easier.

Yep, happy to split.

Quoted 31 lines

But on to the actual code.

Quoted 29 lines
---
 include/qapi/visitor-impl.h | 23 +++++++++++++
 include/qapi/visitor.h      | 51 +++++++++++++++++++++++++++++
 qapi/qapi-visit-core.c | 80
+++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 154 insertions(+)
include/qapi/visitor-impl.h +4 -0
index f3fa420..10cdbf7 100644
@@ -15,6 +15,9 @@
 #include "qapi/error.h"
 #include "qapi/visitor.h"
 
 
 struct Visitor
 {
     /* Must be set */
@@ -29,6 +32,10 @@ struct Visitor
     void (*start_list)(Visitor *v, const char *name, Error **errp);
     GenericList *(*next_list)(Visitor *v, GenericList **list, Error **errp);
     void (*end_list)(Visitor *v, Error **errp);
+    void (*start_array)(Visitor *v, void **obj, const char *name,
+                        size_t elem_count, size_t elem_size, Error **errp);
+    void (*next_array)(Visitor *v, Error **errp);
+    void (*end_array)(Visitor *v, Error **errp);
 
Quoted 8 lines

include/qapi/visitor-impl.h struct Visitor · line 41

Quoted 2 lines
include/qapi/visitor-impl.h +0 -0
     void (*type_enum)(Visitor *v, int *obj, const char *strings[],
                       const char *kind, const char *name, Error **errp);

Since these "must be set", you need to update all existing visitors to actually set them, in the same patch, don't you?

I'm not sure the existing visitors completely obey "must be set" vs. "may be null". But let's not make it worse.

Yep; you've picked me up on a whole bunch of the 'must be set'/'may be null'
- I'll treat those all as one and go through and tidy them up.

Quoted 8 lines

Members above must be set, members below may be null.

include/qapi/visitor-impl.h struct Visitor · line 59

Quoted 4 lines
include/qapi/visitor-impl.h +0 -0
@@ -49,6 +57,8 @@ struct Visitor
     void (*start_handle)(Visitor *v, void **obj, const char *kind,
                          const char *name, Error **errp);
     void (*end_handle)(Visitor *v, Error **errp);

No longer applies (I killed end_handle() in commit cbc955).

Quoted 4 lines
Quoted 1 line

Yep; it's dead in my working tree.

When I get to studying how you put this stuff to use, I'll want a current version. Happy to pull, just tell me where from.

OK, will do.

Quoted 50 lines
Quoted 50 lines
Quoted 20 lines
Quoted 20 lines
include/qapi/visitor-impl.h +2 -0
+    void (*type_buffer)(Visitor *v, void *data, size_t len, bool async,
+                        const char *name, Error **errp);
     void (*type_uint8)(Visitor *v, uint8_t *obj, const char
 *name, Error **errp);
     void (*type_uint16)(Visitor *v, uint16_t *obj, const char

*name, Error **errp);
void (*type_uint32)(Visitor *v, uint32_t *obj, const char
*name, Error **errp);

include/qapi/visitor-impl.h +0 -0
@@ -59,6 +69,19 @@ struct Visitor
     void (*type_int64)(Visitor *v, int64_t *obj, const char
 *name, Error **errp);
     /* visit_type_size() falls back to (*type_uint64)() if

type_size is unset */
void (*type_size)(Visitor *v, uint64_t *obj, const char
*name, Error **errp);

include/qapi/visitor-impl.h +5 -0
+
+    void (*destroy)(Visitor *v, Error **errp);
+
+    void (*start_sequence_compat)(Visitor *v, const char *name,
+                                  Visit_seq_compat_mode compat_mode,
Quoted 8 lines

include/qapi/visitor-impl.h struct Visitor · line 82

Quoted 6 lines
include/qapi/visitor-impl.h +6 -0
+                                  void *opaque, Error **errp);
+    void (*end_sequence_compat)(Visitor *v, const char *name,
+                                Visit_seq_compat_mode compat_mode,
+                                Error **errp);
+
+    QEMUFile* (*get_qemufile)(Visitor *v);

Style nit: QEMUFile *(*get_qemufile)(Visitor *v);

Fixed.

Quoted 18 lines

The empty line before destroy makes me wonder whether these still belong to the "May be NULL" heading.

include/qapi/visitor-impl.h

Quoted 2 lines
include/qapi/visitor-impl.h +2 -0
+
+    uint64_t flags;

This one can't, because it can't be null :)

include/qapi/visitor-impl.h

Quoted 6 lines
include/qapi/visitor-impl.h +6 -0
+/* Blocks of guest memory,disk or otherwise opaque data that there is a lot
+ * of and must be handled efficiently. 'async' true if the write can happen
+ * 'later'
+ */
+void visit_type_buffer(Visitor *v, void *data, size_t len, bool async,
+                       const char *name, Error **errp);

I'm afraid your specification of async is tied to a specific kind of
visitor: one that "writes". Many don't.

Quoted 8 lines
Quoted 6 lines

Do you have a suggestion on how to do this better? Async accesses were put in some time ago apparently for a useful speed increase, so I can't remove them, and anyway they make sense for the writes; I guess you could argue that they could be used for read as well, if the time at which the 'async' had to have completed was defined, but that's needed for the write anyway.

How do we decide async / sync?

The one current use is for RAM migration; in most cases we just want to throw the 'current' ram data out on the wire and don't particularly care if we send a newer version out by the time the write() gets to it and hence set async; the gotcha is in the xbzrle case where the xbzrle cache has to match exactly what was sent to the destination and hence we send it sync. Note that the 'async' in this case is being used to deal with the version/validity of the data and not really caring about knowing whether it's actually hit the wire or not.

Quoted 68 lines
Quoted 68 lines
Quoted 40 lines
Quoted 40 lines

void visit_type_enum(Visitor *v, int *obj, const char *strings[],

                      const char *kind, const char *name, Error **errp);

void visit_type_int(Visitor *v, int64_t *obj, const char *name, Error **errp);

include/qapi/visitor-impl.h +32 -0
@@ -58,6 +69,46 @@ void visit_type_int64(Visitor *v, int64_t *obj, const char *name, Error **errp);
 void visit_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp);
 void visit_type_bool(Visitor *v, bool *obj, const char *name, Error **errp);
 void visit_type_str(Visitor *v, char **obj, const char *name, Error **errp);
+/* A string no more than 256 (including term) characters in length */
+void visit_type_str256(Visitor *v, char *obj, const char *name, Error **errp);
 void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp);
+void visit_destroy(Visitor *v, Error **errp);
+
+/* Type of visitor, used where actions have to be taken when (de)serializing */
+bool visitor_loading(Visitor *v);
+bool visitor_saving(Visitor *v);
+
+typedef enum {
+    VISIT_SEQ_COMPAT_BYTE0TERM,      /* list terminated with a 0 byte */
+    VISIT_SEQ_COMPAT_FILE,           /* The top level file object */
+    VISIT_SEQ_COMPAT_SUBSECLIST,     /* list terminated by
+                                        historical complexity */
+    VISIT_SEQ_COMPAT_SUBSECTION,     /* One subsection */
+    VISIT_SEQ_COMPAT_SECTION_HEADER, /* SECTION_FULL/START, header + name */
+    VISIT_SEQ_COMPAT_SECTION_MIN,    /* SECTION_PART/END - minimal header */
+    VISIT_SEQ_COMPAT_RAMSECLIST,     /* list terminated by int64 bit
+                                        RAM_SAVE_FLAG_EOS */
+    VISIT_SEQ_COMPAT_RAMSECENTRY,    /* Entry in RAM Sec list */
+    VISIT_SEQ_COMPAT_VMSTATE,        /* One VMState */
+    VISIT_SEQ_COMPAT_BLOB            /* A binary old-format blob */
+} Visit_seq_compat_mode;
+
+/* Start a sequence of items (which may be of unknown length and unknown
+ * mix of some subset of types), specify a compatibility mode that's only
+ * used by an implementation trying to match the existing binary migration
+ * format.
+ * opaque is compat_mode specific
+ */
+void visit_start_sequence_compat(Visitor *v, const char *name,
+                                 Visit_seq_compat_mode compat_mode,
+                                 void *opaque,
Quoted 8 lines

include/qapi/visitor-impl.h void visit_type_int64(Visitor *v, int64_t *obj, const char *name, Error **errp); · line 110

Quoted 6 lines
include/qapi/visitor-impl.h +6 -0
+                                 Error **errp);
+/* Use visit_get_next_type for each entry including the first */
+void visit_end_sequence_compat(Visitor *v, const char *name,
+                                 Visit_seq_compat_mode compat_mode,
+                                 Error **errp);
+

I'm afraid I don't get this comment.

Quite a lot of the structures I need to keep compatibility with are lists of some mixture of different things with a terminator on the end (there's not much consistency in the binary format about how you tell what's next or what the terminator is).
So the typical use is:

visit_start_sequence_compat(...)
while (visit_get_next_type( &flag ), flag != end_flag) {
switch (flag) {
case A:
do whatever you need to do now that we've found an 'A' in the list (typically another visit_start_sequence_compat for that type)
case B:
do whatever you need to do now that we've found an 'B' in the list
....

       }
    }
    visit_end_sequence_compat(...)
Quoted 5 lines
Quoted 2 lines

An example is a RAM migration section; it's a list of initialisation data, normal pages, all-zero pages, XBZRLE pages

Doesn't match the existing visit_get_next_type(). I guess I need more context. One way to get it is study your patches.

Probably a good one to look at is the load path change;
http://lists.gnu.org/archive/html/qemu-devel/2014-04/msg03583.html
in particular the changes to ram_load and qemu_loadvm_state that both have examples of that use.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines
include/qapi/visitor-impl.h +1 -0
+/* Don't Use! - lets us move forward until we can get rid of all

file uses */
+QEMUFile *visitor_get_qemufile(Visitor *v);

Likewise.

Quoted 6 lines
Quoted 4 lines

I'm hoping this is a temporary shim - my hope eventually is that we won't need to
use this and everywhere that uses Visitors won't also need to get a QEMUFile except internally to the visitor.

Suggest to put that in the comment.

Will do.

Dave
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#7 lore raw

RFC PATCH 2/16 QEMUSizedBuffer/QEMUFile

In reply to Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

Stefan Berger's to create a QEMUFile that goes to a memory buffer; from:

http://lists.gnu.org/archive/html/qemu-devel/2013-03/msg05036.html

Using the QEMUFile interface, this patch adds support functions for operating
on in-memory sized buffers that can be written to or read from.

---
 include/migration/qemu-file.h |  27 +++
 qemu-file.c                   | 411 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 438 insertions(+)
include/migration/qemu-file.h +27 -0
index a191fb6..f066801 100644
@@ -25,6 +25,8 @@
 #define QEMU_FILE_H 1
 #include "exec/cpu-common.h"
 
+#include <stdint.h>
+
 /* This function writes a chunk of data to a file at the given position.
  * The pos argument can be ignored if the file is only being used for
  * streaming.  The handler should try to write all of the data it can.
@@ -94,11 +96,31 @@ typedef struct QEMUFileOps {
     QEMURamSaveFunc *save_page;
 } QEMUFileOps;
 
+struct QEMUSizedBuffer {
+    struct iovec *iov;
+    size_t n_iov;
+    size_t size; /* total allocated size in all iov's */
+    size_t used; /* number of used bytes */
+};
+
+typedef struct QEMUSizedBuffer QEMUSizedBuffer;
+QEMUSizedBuffer *qsb_create(const uint8_t *buffer, size_t len);
+QEMUSizedBuffer *qsb_clone(const QEMUSizedBuffer *);
+void qsb_free(QEMUSizedBuffer *);
+size_t qsb_set_length(QEMUSizedBuffer *qsb, size_t length);
+size_t qsb_get_length(const QEMUSizedBuffer *qsb);
+ssize_t qsb_get_buffer(const QEMUSizedBuffer *, off_t start, size_t count,
+                       uint8_t **buf);
+ssize_t qsb_write_at(QEMUSizedBuffer *qsb, const uint8_t *buf,
+                     off_t pos, size_t count);
+
+
 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops);
 QEMUFile *qemu_fopen(const char *filename, const char *mode);
 QEMUFile *qemu_fdopen(int fd, const char *mode);
 QEMUFile *qemu_fopen_socket(int fd, const char *mode);
 QEMUFile *qemu_popen_cmd(const char *command, const char *mode);
+QEMUFile *qemu_bufopen(const char *mode, QEMUSizedBuffer *input);
 int qemu_get_fd(QEMUFile *f);
 int qemu_fclose(QEMUFile *f);
 int64_t qemu_ftell(QEMUFile *f);
@@ -111,6 +133,11 @@ void qemu_put_byte(QEMUFile *f, int v);
 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size);
 bool qemu_file_mode_is_not_valid(const char *mode);
 
+/*
+ * For use on files opened with qemu_bufopen
+ */
+const QEMUSizedBuffer *qemu_buf_get(QEMUFile *f);
+
 static inline void qemu_put_ubyte(QEMUFile *f, unsigned int v)
 {
     qemu_put_byte(f, (int)v);
qemu-file.c +411 -0

414 lines not on this page · raw message

Signature · 1 line
1.8.5.3
#8 lore raw

RFC PATCH 3/16 qemu-file: Add set/get tmp_visitor

In reply to Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

---
 include/migration/qemu-file.h |  4 ++++
 qemu-file.c                   | 12 ++++++++++++
 2 files changed, 16 insertions(+)
include/migration/qemu-file.h +4 -0
index f066801..e36c03f 100644
@@ -174,6 +174,10 @@ int qemu_file_get_error(QEMUFile *f);
 void qemu_file_set_error(QEMUFile *f, int ret);
 void qemu_fflush(QEMUFile *f);
 
+/* TRANSITION ONLY - DON'T USE! */
+Visitor *qemu_file_get_tmp_visitor(QEMUFile *f);
+void qemu_file_set_tmp_visitor(QEMUFile *f, Visitor *v);
+
 static inline void qemu_put_be64s(QEMUFile *f, const uint64_t *pv)
 {
     qemu_put_be64(f, *pv);
qemu-file.c +12 -0
index b1b1a5b..feb3e68 100644
@@ -25,6 +25,8 @@ struct QEMUFile {
     unsigned int iovcnt;
 
     int last_error;
+
+    Visitor *tmp_visitor;
 };
 
 typedef struct QEMUFileStdio {
@@ -37,6 +39,16 @@ typedef struct QEMUFileSocket {
     QEMUFile *file;
 } QEMUFileSocket;
 
+Visitor *qemu_file_get_tmp_visitor(QEMUFile *f)
+{
+    return f->tmp_visitor;
+}
+
+void qemu_file_set_tmp_visitor(QEMUFile *f, Visitor *v)
+{
+    f->tmp_visitor = v;
+}
+
 static ssize_t socket_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
                                     int64_t pos)
 {
Signature · 1 line
1.8.5.3
#9 lore raw

RFC PATCH 4/16 Header/constant/types fixes for visitors

In reply to Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

Move constants around and add types to allow file structure to move into visitors.

---
 arch_init.c                   | 12 ------------
 include/migration/migration.h | 15 +++++++++++++++
 include/migration/vmstate.h   | 20 +++++++++++++++++---
 include/qemu/typedefs.h       |  4 ++--
 4 files changed, 34 insertions(+), 17 deletions(-)
arch_init.c +0 -12
index 60c975d..73b9303 100644
@@ -110,18 +110,6 @@ static bool mig_throttle_on;
 static int dirty_rate_high_cnt;
 static void check_guest_throttling(void);
 
-/***********************************************************/
-/* ram save/restore */
-
-#define RAM_SAVE_FLAG_FULL     0x01 /* Obsolete, not used anymore */
-#define RAM_SAVE_FLAG_COMPRESS 0x02
-#define RAM_SAVE_FLAG_MEM_SIZE 0x04
-#define RAM_SAVE_FLAG_PAGE     0x08
-#define RAM_SAVE_FLAG_EOS      0x10
-#define RAM_SAVE_FLAG_CONTINUE 0x20
-#define RAM_SAVE_FLAG_XBZRLE   0x40
-/* 0x80 is reserved in migration.h start with 0x100 next */
-
 static struct defconfig_file {
     const char *filename;
     /* Indicates it is an user config file (disabled by -no-user-config) */
include/migration/migration.h +15 -0
index 3e1e6c7..1987e79 100644
@@ -165,7 +165,16 @@ void ram_control_load_hook(QEMUFile *f, uint64_t flags);
  * side. This lets before_ram_iterate/after_ram_iterate add
  * transport-specific sections to the RAM migration data.
  */
+/* ram save/restore */
+#define RAM_SAVE_FLAG_FULL     0x01 /* Obsolete, not used anymore */
+#define RAM_SAVE_FLAG_COMPRESS 0x02
+#define RAM_SAVE_FLAG_MEM_SIZE 0x04
+#define RAM_SAVE_FLAG_PAGE     0x08
+#define RAM_SAVE_FLAG_EOS      0x10
+#define RAM_SAVE_FLAG_CONTINUE 0x20
+#define RAM_SAVE_FLAG_XBZRLE   0x40
 #define RAM_SAVE_FLAG_HOOK     0x80
+#define RAM_SAVE_FLAG_MASK    0x1ff
 
 #define RAM_SAVE_CONTROL_NOT_SUPP -1000
 #define RAM_SAVE_CONTROL_DELAYED  -2000
@@ -174,4 +183,10 @@ size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
                              ram_addr_t offset, size_t size,
                              int *bytes_sent);
 
+typedef struct {
+    uint64_t addr;
+    uint16_t flags;
+    char     idstr[256];
+} ramsecentry_header;
+
 #endif
include/migration/vmstate.h +17 -3
index e7e1705..a5e4b0b 100644
@@ -26,6 +26,7 @@
 #ifndef QEMU_VMSTATE_H
 #define QEMU_VMSTATE_H 1
 
+#include "qemu/typedefs.h"
 #ifndef CONFIG_USER_ONLY
 #include <migration/qemu-file.h>
 #endif
@@ -49,15 +50,27 @@ typedef struct SaveVMHandlers {
      * use data that is local to the migration thread or protected
      * by other locks.
      */
-    int (*save_live_iterate)(QEMUFile *f, void *opaque);
+    int (*save_live_iterate)(Visitor *v, void *opaque);
 
     /* This runs outside the iothread lock!  */
-    int (*save_live_setup)(QEMUFile *f, void *opaque);
-    uint64_t (*save_live_pending)(QEMUFile *f, void *opaque, uint64_t max_size);
+    int (*save_live_setup)(Visitor *v, void *opaque);
+    uint64_t (*save_live_pending)(void *opaque, uint64_t max_size);
 
     LoadStateHandler *load_state;
 } SaveVMHandlers;
 
+/* This is the data used to identify a section as passed
+ * into the section version of the compat sequence visitor
+ * (TODO: Probably want to move the whole name lookup into there
+ *    and keep the section_id wrapped inside the binary visitor)
+ */
+typedef struct SectionHeader {
+    uint32_t section_id;
+    uint32_t instance_id; /* Below only used for full version */
+    uint32_t version_id;
+    char     idstr[256];
+} SectionHeader;
+
 int register_savevm(DeviceState *dev,
                     const char *idstr,
                     int instance_id,
@@ -134,6 +147,7 @@ struct VMStateDescription {
     void (*pre_save)(void *opaque);
     VMStateField *fields;
     const VMStateSubsection *subsections;
+    uint32_t ber_tag;
 };
 
 #ifdef CONFIG_USER_ONLY
include/qemu/typedefs.h +2 -2
index bf8daac..3fea88e 100644
@@ -10,8 +10,6 @@ typedef struct QEMUBH QEMUBH;
 
 typedef struct AioContext AioContext;
 
-typedef struct Visitor Visitor;
-
 struct Monitor;
 typedef struct Monitor Monitor;
 typedef struct MigrationParams MigrationParams;
@@ -39,6 +37,7 @@ typedef struct DriveInfo DriveInfo;
 typedef struct DisplayState DisplayState;
 typedef struct DisplayChangeListener DisplayChangeListener;
 typedef struct DisplaySurface DisplaySurface;
+typedef struct Error Error;
 typedef struct PixelFormat PixelFormat;
 typedef struct QemuConsole QemuConsole;
 typedef struct CharDriverState CharDriverState;
@@ -73,5 +72,6 @@ typedef struct SHPCDevice SHPCDevice;
 typedef struct FWCfgState FWCfgState;
 typedef struct PcGuestInfo PcGuestInfo;
 typedef struct Range Range;
+typedef struct Visitor Visitor;
 
 #endif /* QEMU_TYPEDEFS_H */
Signature · 1 line
1.8.5.3
#10 lore raw

RFC PATCH 5/16 Visitor: Binary compatible output visitor

In reply to Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

Binary compatible output visitor to write to a QEMUFile in current migration format.

This is based on Michael Roth's series :
https://lists.gnu.org/archive/html/qemu-devel/2011-09/msg02466.html

---
 include/qapi/qemu-file-binary-output-visitor.h |  26 ++
 qapi/Makefile.objs                             |   2 +-
 qapi/qemu-file-binary-output-visitor.c         | 564 +++++++++++++++++++++++++
 3 files changed, 591 insertions(+), 1 deletion(-)
 create mode 100644 include/qapi/qemu-file-binary-output-visitor.h
 create mode 100644 qapi/qemu-file-binary-output-visitor.c
include/qapi/qemu-file-binary-output-visitor.h new file +26 -0
new file mode 100644
index 0000000..f563407
@@ -0,0 +1,26 @@
+/*
+ * QEMUFile Visitor
+ *
+ * Copyright IBM, Corp. 2011
+ *
+ * Authors:
+ *  Michael Roth   <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ */
+
+#ifndef QEMU_FILE_BINARY_OUTPUT_VISITOR_H
+#define QEMU_FILE_BINARY_OUTPUT_VISITOR_H
+
+#include "visitor.h"
+
+typedef struct QemuFileBinOutputVisitor QemuFileBinOutputVisitor;
+
+QemuFileBinOutputVisitor *qemu_file_bin_output_visitor_new(QEMUFile *f);
+void qemu_file_bin_output_visitor_cleanup(QemuFileBinOutputVisitor *d);
+
+Visitor *qemu_file_bin_output_get_visitor(QemuFileBinOutputVisitor *v);
+
+#endif
qapi/Makefile.objs +1 -1
index 1f9c973..a054d52 100644
@@ -1,5 +1,5 @@
 util-obj-y = qapi-visit-core.o qapi-dealloc-visitor.o qmp-input-visitor.o
 util-obj-y += qmp-output-visitor.o qmp-registry.o qmp-dispatch.o
 util-obj-y += string-input-visitor.o string-output-visitor.o
-
+util-obj-y += qemu-file-binary-output-visitor.o
 util-obj-y += opts-visitor.o
qapi/qemu-file-binary-output-visitor.c new file +564 -0

564 lines not on this page · raw message

Signature · 1 line
1.8.5.3
#11 lore raw

RFC PATCH 6/16 Visitor: Debug output visitor

In reply to Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

A migration visitor whose output is textual and purely for human consumption for debug.

---
 include/qapi/qemu-file-debug-output-visitor.h |  26 ++
 qapi/Makefile.objs                            |   1 +
 qapi/qemu-file-debug-output-visitor.c         | 471 ++++++++++++++++++++++++++
 3 files changed, 498 insertions(+)
 create mode 100644 include/qapi/qemu-file-debug-output-visitor.h
 create mode 100644 qapi/qemu-file-debug-output-visitor.c
include/qapi/qemu-file-debug-output-visitor.h new file +26 -0
new file mode 100644
index 0000000..427d5b2
@@ -0,0 +1,26 @@
+/*
+ * QEMUFile Visitor
+ *
+ * Copyright Red Hat, Corp. 2014
+ *
+ * Authors:
+ *  David Gilbert <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ */
+
+#ifndef QEMU_FILE_DEBUG_OUTPUT_VISITOR_H
+#define QEMU_FILE_DEBUG_OUTPUT_VISITOR_H
+
+#include "visitor.h"
+
+typedef struct QemuFileDebugOutputVisitor QemuFileDebugOutputVisitor;
+
+QemuFileDebugOutputVisitor *qemu_file_debug_output_visitor_new(QEMUFile *f);
+void qemu_file_debug_output_visitor_cleanup(QemuFileDebugOutputVisitor *d);
+
+Visitor *qemu_file_debug_output_get_visitor(QemuFileDebugOutputVisitor *v);
+
+#endif
qapi/Makefile.objs +1 -0
index a054d52..06c69e6 100644
@@ -2,4 +2,5 @@ util-obj-y = qapi-visit-core.o qapi-dealloc-visitor.o qmp-input-visitor.o
 util-obj-y += qmp-output-visitor.o qmp-registry.o qmp-dispatch.o
 util-obj-y += string-input-visitor.o string-output-visitor.o
 util-obj-y += qemu-file-binary-output-visitor.o
+util-obj-y += qemu-file-debug-output-visitor.o
 util-obj-y += opts-visitor.o
qapi/qemu-file-debug-output-visitor.c new file +471 -0

471 lines not on this page · raw message

Signature · 1 line
1.8.5.3
#12 lore raw

RFC PATCH 7/16 Visitor: Binary compatible input visitor

In reply to Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

based on Michael Roth's
https://lists.gnu.org/archive/html/qemu-devel/2011-09/msg02470.html

---
 include/qapi/qemu-file-binary-input-visitor.h |  27 +
 qapi/Makefile.objs                            |   2 +-
 qapi/qemu-file-binary-input-visitor.c         | 688 ++++++++++++++++++++++++++
 3 files changed, 716 insertions(+), 1 deletion(-)
 create mode 100644 include/qapi/qemu-file-binary-input-visitor.h
 create mode 100644 qapi/qemu-file-binary-input-visitor.c
include/qapi/qemu-file-binary-input-visitor.h new file +27 -0
new file mode 100644
index 0000000..dd7e40e
@@ -0,0 +1,27 @@
+/*
+ * QEMUFile Visitor
+ *
+ * Copyright IBM, Corp. 2011
+ *
+ * Authors:
+ *  Michael Roth   <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ */
+
+#ifndef QEMU_FILE_BINARY_INPUT_VISITOR_H
+#define QEMU_FILE_BINARY_INPUT_VISITOR_H
+
+#include "visitor.h"
+
+typedef struct QemuFileBinInputVisitor QemuFileBinInputVisitor;
+
+QemuFileBinInputVisitor *qemu_file_bin_input_visitor_new(QEMUFile *f);
+void qemu_file_bin_input_visitor_cleanup(QemuFileBinInputVisitor *d);
+
+Visitor *qemu_file_bin_input_get_visitor(QemuFileBinInputVisitor *v);
+
+#endif
+
qapi/Makefile.objs +1 -1
index 06c69e6..3d9d47a 100644
@@ -1,6 +1,6 @@
 util-obj-y = qapi-visit-core.o qapi-dealloc-visitor.o qmp-input-visitor.o
 util-obj-y += qmp-output-visitor.o qmp-registry.o qmp-dispatch.o
 util-obj-y += string-input-visitor.o string-output-visitor.o
-util-obj-y += qemu-file-binary-output-visitor.o
+util-obj-y += qemu-file-binary-output-visitor.o qemu-file-binary-input-visitor.o
 util-obj-y += qemu-file-debug-output-visitor.o
 util-obj-y += opts-visitor.o
qapi/qemu-file-binary-input-visitor.c new file +688 -0

688 lines not on this page · raw message

Signature · 1 line
1.8.5.3
#13 lore raw

RFC PATCH 8/16 Visitor: Output path

In reply to Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

Replace QEMUFile by Visitor in most of the output path. There are still a few places that want a QEMUFile* and those are TODOs at the moment.

Hacks in pci.c, spapr, spapr_vscsi.c for now where new visitor/qemufile boundaries have been added.

---
 arch_init.c                 | 147 ++++++++++++++++++++++---------
 block-migration.c           |  13 ++-
 hw/pci/pci.c                |   2 +-
 hw/ppc/spapr.c              |   9 +-
 hw/scsi/spapr_vscsi.c       |   3 +-
 include/migration/vmstate.h |   6 +-
 savevm.c                    | 208 +++++++++++++++++++++++++++++++-------------
 vmstate.c                   | 102 ++++++++++++++++++----
 8 files changed, 365 insertions(+), 125 deletions(-)
arch_init.c +108 -39
index 73b9303..02bf78a 100644
@@ -31,6 +31,7 @@
 #include "config.h"
 #include "monitor/monitor.h"
 #include "sysemu/sysemu.h"
+#include "qapi/visitor.h"
 #include "qemu/bitops.h"
 #include "qemu/bitmap.h"
 #include "sysemu/arch_init.h"
@@ -60,6 +61,13 @@
     do { } while (0)
 #endif
 
+#define LOCAL_ERR_REPORT(fallout) \
+    if (local_err) { \
+        error_report("%s:%d %s", __func__, __LINE__, \
+                     error_get_pretty(local_err));   \
+        fallout \
+    }
+
 #ifdef TARGET_SPARC
 int graphic_width = 1024;
 int graphic_height = 768;
@@ -70,7 +78,6 @@ int graphic_height = 600;
 int graphic_depth = 32;
 #endif
 
-
 #if defined(TARGET_ALPHA)
 #define QEMU_ARCH QEMU_ARCH_ALPHA
 #elif defined(TARGET_ARM)
@@ -281,20 +288,22 @@ uint64_t xbzrle_mig_pages_overflow(void)
     return acct_info.xbzrle_overflows;
 }
 
-static size_t save_block_hdr(QEMUFile *f, RAMBlock *block, ram_addr_t offset,
+static size_t save_block_hdr(Visitor *v, RAMBlock *block, ram_addr_t offset,
                              int cont, int flag)
 {
-    size_t size;
-
-    qemu_put_be64(f, offset | cont | flag);
-    size = 8;
+    size_t size = 8; /* Note bogus with visitor */
+    Error *local_err = NULL;
+    ramsecentry_header rse_hdr;
 
+    rse_hdr.addr = offset;
+    rse_hdr.flags = cont | flag;
     if (!cont) {
-        qemu_put_byte(f, strlen(block->idstr));
-        qemu_put_buffer(f, (uint8_t *)block->idstr,
-                        strlen(block->idstr));
+        strcpy(rse_hdr.idstr, block->idstr);
         size += 1 + strlen(block->idstr);
     }
+    visit_start_sequence_compat(v, "ramsecentry", VISIT_SEQ_COMPAT_RAMSECENTRY,
+                                &rse_hdr, &local_err);
+
     return size;
 }
 
@@ -328,10 +337,13 @@ static void xbzrle_cache_zero_page(ram_addr_t current_addr)
 
 #define ENCODING_FLAG_XBZRLE 0x1
 
-static int save_xbzrle_page(QEMUFile *f, uint8_t *current_data,
+static int save_xbzrle_page(Visitor *v, uint8_t *current_data,
                             ram_addr_t current_addr, RAMBlock *block,
                             ram_addr_t offset, int cont, bool last_stage)
 {
+    QEMUFile *f = visitor_get_qemufile(v); // TODO
+    Error *local_err = NULL;
+
     int encoded_len = 0, bytes_sent = -1;
     uint8_t *prev_cached_page;
 
@@ -371,7 +383,7 @@ static int save_xbzrle_page(QEMUFile *f, uint8_t *current_data,
     }
 
     /* Send XBZRLE based compressed page */
-    bytes_sent = save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_XBZRLE);
+    bytes_sent = save_block_hdr(v, block, offset, cont, RAM_SAVE_FLAG_XBZRLE);
     qemu_put_byte(f, ENCODING_FLAG_XBZRLE);
     qemu_put_be16(f, encoded_len);
     qemu_put_buffer(f, XBZRLE.encoded_buf, encoded_len);
@@ -379,6 +391,9 @@ static int save_xbzrle_page(QEMUFile *f, uint8_t *current_data,
     acct_info.xbzrle_pages++;
     acct_info.xbzrle_bytes += bytes_sent;
 
+    visit_end_sequence_compat(v, "ramsecentry", VISIT_SEQ_COMPAT_RAMSECENTRY,
+                              &local_err);
+    LOCAL_ERR_REPORT(return -1;);
     return bytes_sent;
 }
 
@@ -523,8 +538,11 @@ static void migration_bitmap_sync(void)
  *           0 means no dirty pages
  */
 
-static int ram_save_block(QEMUFile *f, bool last_stage)
+static int ram_save_block(Visitor *v, bool last_stage)
 {
+    QEMUFile *f = visitor_get_qemufile(v); // TODO
+    Error *local_err = NULL;
+
     RAMBlock *block = last_seen_block;
     ram_addr_t offset = last_offset;
     bool complete_round = false;
@@ -576,17 +594,23 @@ static int ram_save_block(QEMUFile *f, bool last_stage)
                     }
                 }
             } else if (is_zero_range(p, TARGET_PAGE_SIZE)) {
+                uint8_t tmpbyte;
                 acct_info.dup_pages++;
-                bytes_sent = save_block_hdr(f, block, offset, cont,
+                bytes_sent = save_block_hdr(v, block, offset, cont,
                                             RAM_SAVE_FLAG_COMPRESS);
-                qemu_put_byte(f, 0);
+                tmpbyte = 0;
+                visit_type_uint8(v, &tmpbyte, "pagefill", &local_err);
+                visit_end_sequence_compat(v, "ramsecentry",
+                                          VISIT_SEQ_COMPAT_RAMSECENTRY,
+                                          &local_err);
+                LOCAL_ERR_REPORT(return -1;);
                 bytes_sent++;
                 /* Must let xbzrle know, otherwise a previous (now 0'd) cached
                  * page would be stale
                  */
                 xbzrle_cache_zero_page(current_addr);
             } else if (!ram_bulk_stage && migrate_use_xbzrle()) {
-                bytes_sent = save_xbzrle_page(f, p, current_addr, block,
+                bytes_sent = save_xbzrle_page(v, p, current_addr, block,
                                               offset, cont, last_stage);
                 if (!last_stage) {
                     /* We must send exactly what's in the xbzrle cache
@@ -604,12 +628,14 @@ static int ram_save_block(QEMUFile *f, bool last_stage)
 
             /* XBZRLE overflow or normal page */
             if (bytes_sent == -1) {
-                bytes_sent = save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_PAGE);
-                if (send_async) {
-                    qemu_put_buffer_async(f, p, TARGET_PAGE_SIZE);
-                } else {
-                    qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
-                }
+                bytes_sent = save_block_hdr(v, block, offset, cont,
+                                            RAM_SAVE_FLAG_PAGE);
+                visit_type_buffer(v, p, TARGET_PAGE_SIZE, send_async, "page",
+                                            &local_err);
+                visit_end_sequence_compat(v, "ramsecentry",
+                                          VISIT_SEQ_COMPAT_RAMSECENTRY,
+                                          &local_err);
+                LOCAL_ERR_REPORT(return -1;);
                 bytes_sent += TARGET_PAGE_SIZE;
                 acct_info.norm_pages++;
             }
@@ -711,10 +737,12 @@ static void reset_ram_globals(void)
 
 #define MAX_WAIT 50 /* ms, half buffered_file limit */
 
-static int ram_save_setup(QEMUFile *f, void *opaque)
+static int ram_save_setup(Visitor *v, void *opaque)
 {
     RAMBlock *block;
     int64_t ram_pages = last_ram_offset() >> TARGET_PAGE_BITS;
+    Error *local_err = NULL;
+    ramsecentry_header rse_hdr;
 
     migration_bitmap = bitmap_new(ram_pages);
     bitmap_set(migration_bitmap, 0, ram_pages);
@@ -762,31 +790,65 @@ static int ram_save_setup(QEMUFile *f, void *opaque)
     migration_bitmap_sync();
     qemu_mutex_unlock_iothread();
 
-    qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
+    visit_start_sequence_compat(v, "ramseclist", VISIT_SEQ_COMPAT_RAMSECLIST,
+                                NULL, &local_err);
+    rse_hdr.addr = ram_bytes_total();
+    rse_hdr.flags = RAM_SAVE_FLAG_MEM_SIZE;
+    visit_start_sequence_compat(v, "ramsecentry", VISIT_SEQ_COMPAT_RAMSECENTRY,
+                                &rse_hdr, &local_err);
+    LOCAL_ERR_REPORT(
+        qemu_mutex_unlock_ramlist();
+        return -1;
+    );
 
+    /* Note in the binary file the list is ended when it hits ram_bytes_total
+     * in length rather than any terminator
+     */
+    visit_start_list(v, "blocklist", &local_err);
     QTAILQ_FOREACH(block, &ram_list.blocks, next) {
-        qemu_put_byte(f, strlen(block->idstr));
-        qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr));
-        qemu_put_be64(f, block->length);
+        visit_next_list(v, NULL, &local_err);
+
+        visit_start_struct(v, NULL, "blockid", "blockid", 0, &local_err);
+        visit_type_str256(v, block->idstr, "blockname", &local_err);
+        visit_type_uint64(v, &block->length, "blocklen", &local_err);
+        LOCAL_ERR_REPORT(
+            qemu_mutex_unlock_ramlist();
+            return -1;
+        );
+        visit_end_struct(v, &local_err);
     }
+    LOCAL_ERR_REPORT(
+        qemu_mutex_unlock_ramlist();
+        return -1;
+    );
+
+    visit_end_list(v, &local_err);
 
     qemu_mutex_unlock_ramlist();
 
-    ram_control_before_iterate(f, RAM_CONTROL_SETUP);
-    ram_control_after_iterate(f, RAM_CONTROL_SETUP);
+    visit_end_sequence_compat(v, "ramsecentry", VISIT_SEQ_COMPAT_RAMSECENTRY,
+                              &local_err);
+    LOCAL_ERR_REPORT(return -1;);
+    ram_control_before_iterate(visitor_get_qemufile(v), RAM_CONTROL_SETUP);
+    ram_control_after_iterate(visitor_get_qemufile(v), RAM_CONTROL_SETUP);
 
-    qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
+    visit_end_sequence_compat(v, "ramseclist", VISIT_SEQ_COMPAT_RAMSECLIST,
+                              &local_err);
 
     return 0;
 }
 
-static int ram_save_iterate(QEMUFile *f, void *opaque)
+static int ram_save_iterate(Visitor *v, void *opaque)
 {
+    QEMUFile *f = visitor_get_qemufile(v);
+    Error *local_err = NULL;
     int ret;
     int i;
     int64_t t0;
     int total_sent = 0;
 
+    visit_start_sequence_compat(v, "ramseclist", VISIT_SEQ_COMPAT_RAMSECLIST,
+                                NULL, &local_err);
     qemu_mutex_lock_ramlist();
 
     if (ram_list.version != last_version) {
@@ -800,7 +862,7 @@ static int ram_save_iterate(QEMUFile *f, void *opaque)
     while ((ret = qemu_file_rate_limit(f)) == 0) {
         int bytes_sent;
 
-        bytes_sent = ram_save_block(f, false);
+        bytes_sent = ram_save_block(v, false);
         /* no more blocks to sent */
         if (bytes_sent == 0) {
             break;
@@ -838,7 +900,8 @@ static int ram_save_iterate(QEMUFile *f, void *opaque)
      * Do not count these 8 bytes into total_sent, so that we can
      * return 0 if no page had been dirtied.
      */
-    qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
+    visit_end_sequence_compat(v, "ramseclist", VISIT_SEQ_COMPAT_RAMSECLIST,
+                              &local_err);
     bytes_transferred += 8;
 
     ret = qemu_file_get_error(f);
@@ -846,15 +909,19 @@ static int ram_save_iterate(QEMUFile *f, void *opaque)
         return ret;
     }
 
+    LOCAL_ERR_REPORT(return -EINVAL;);
     return total_sent;
 }
 
-static int ram_save_complete(QEMUFile *f, void *opaque)
+static int ram_save_complete(Visitor *v, void *opaque)
 {
+    Error *local_err = NULL;
     qemu_mutex_lock_ramlist();
     migration_bitmap_sync();
 
-    ram_control_before_iterate(f, RAM_CONTROL_FINISH);
+    visit_start_sequence_compat(v, "ramseclist", VISIT_SEQ_COMPAT_RAMSECLIST,
+                                NULL, &local_err);
+    ram_control_before_iterate(visitor_get_qemufile(v), RAM_CONTROL_FINISH);
 
     /* try transferring iterative blocks of memory */
 
@@ -862,24 +929,26 @@ static int ram_save_complete(QEMUFile *f, void *opaque)
     while (true) {
         int bytes_sent;
 
-        bytes_sent = ram_save_block(f, true);
-        /* no more blocks to sent */
+        bytes_sent = ram_save_block(v, true);
+        /* no more blocks to send */
         if (bytes_sent == 0) {
             break;
         }
         bytes_transferred += bytes_sent;
     }
 
-    ram_control_after_iterate(f, RAM_CONTROL_FINISH);
+    ram_control_after_iterate(visitor_get_qemufile(v), RAM_CONTROL_FINISH);
+    visit_end_sequence_compat(v, "ramseclist", VISIT_SEQ_COMPAT_RAMSECLIST,
+                              &local_err);
     migration_end();
 
     qemu_mutex_unlock_ramlist();
-    qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
+    LOCAL_ERR_REPORT(return -EINVAL;);
 
     return 0;
 }
 
-static uint64_t ram_save_pending(QEMUFile *f, void *opaque, uint64_t max_size)
+static uint64_t ram_save_pending(void *opaque, uint64_t max_size)
 {
     uint64_t remaining_size;
 
block-migration.c +9 -4
index 897fdba..3475d76 100644
@@ -16,6 +16,7 @@
 #include "qemu-common.h"
 #include "block/block_int.h"
 #include "hw/hw.h"
+#include "qapi/visitor.h"
 #include "qemu/queue.h"
 #include "qemu/timer.h"
 #include "migration/block.h"
@@ -603,9 +604,10 @@ static void block_migration_cancel(void *opaque)
     blk_mig_cleanup();
 }
 
-static int block_save_setup(QEMUFile *f, void *opaque)
+static int block_save_setup(Visitor *v, void *opaque)
 {
     int ret;
+    QEMUFile *f = visitor_get_qemufile(v); // TODO
 
     DPRINTF("Enter save live setup submitted %d transferred %d\n",
             block_mig_state.submitted, block_mig_state.transferred);
@@ -624,8 +626,10 @@ static int block_save_setup(QEMUFile *f, void *opaque)
     return ret;
 }
 
-static int block_save_iterate(QEMUFile *f, void *opaque)
+static int block_save_iterate(Visitor *v, void *opaque)
 {
+    QEMUFile *f = visitor_get_qemufile(v); // TODO
+
     int ret;
     int64_t last_ftell = qemu_ftell(f);
 
@@ -682,8 +686,9 @@ static int block_save_iterate(QEMUFile *f, void *opaque)
 
 /* Called with iothread lock taken.  */
 
-static int block_save_complete(QEMUFile *f, void *opaque)
+static int block_save_complete(Visitor *v, void *opaque)
 {
+    QEMUFile *f = visitor_get_qemufile(v); // TODO
     int ret;
 
     DPRINTF("Enter save live complete submitted %d transferred %d\n",
@@ -720,7 +725,7 @@ static int block_save_complete(QEMUFile *f, void *opaque)
     return 0;
 }
 
-static uint64_t block_save_pending(QEMUFile *f, void *opaque, uint64_t max_size)
+static uint64_t block_save_pending(void *opaque, uint64_t max_size)
 {
     /* Estimate pending number of bytes to send */
     uint64_t pending;
hw/pci/pci.c +1 -1
index 8f722dd..e91f729 100644
@@ -515,7 +515,7 @@ void pci_device_save(PCIDevice *s, QEMUFile *f)
      * This makes us compatible with old devices
      * which never set or clear this bit. */
     s->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
-    vmstate_save_state(f, pci_get_vmstate(s), s);
+    vmstate_save_state(qemu_file_get_tmp_visitor(f), pci_get_vmstate(s), s);
     /* Restore the interrupt status bit. */
     pci_update_irq_status(s);
 }
hw/ppc/spapr.c +6 -3
index a11e121..34443c1 100644
@@ -835,9 +835,10 @@ static const VMStateDescription vmstate_spapr = {
 #define HPTE_DIRTY(_hpte)  (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_HPTE_DIRTY)
 #define CLEAN_HPTE(_hpte)  ((*(uint64_t *)(_hpte)) &= tswap64(~HPTE64_V_HPTE_DIRTY))
 
-static int htab_save_setup(QEMUFile *f, void *opaque)
+static int htab_save_setup(Visitor *v, void *opaque)
 {
     sPAPREnvironment *spapr = opaque;
+    QEMUFile *f = visitor_get_qemufile(v); // TODO
 
     /* "Iteration" header */
     qemu_put_be32(f, spapr->htab_shift);
@@ -990,8 +991,9 @@ static int htab_save_later_pass(QEMUFile *f, sPAPREnvironment *spapr,
 #define MAX_ITERATION_NS    5000000 /* 5 ms */
 #define MAX_KVM_BUF_SIZE    2048
 
-static int htab_save_iterate(QEMUFile *f, void *opaque)
+static int htab_save_iterate(Visitor *v, void *opaque)
 {
+    QEMUFile *f = visitor_get_qemufile(v); // TODO
     sPAPREnvironment *spapr = opaque;
     int rc = 0;
 
@@ -1020,8 +1022,9 @@ static int htab_save_iterate(QEMUFile *f, void *opaque)
     return rc;
 }
 
-static int htab_save_complete(QEMUFile *f, void *opaque)
+static int htab_save_complete(Visitor *v, void *opaque)
 {
+    QEMUFile *f = visitor_get_qemufile(v); // TODO
     sPAPREnvironment *spapr = opaque;
 
     /* Iteration header */
hw/scsi/spapr_vscsi.c +2 -1
index 34478f0..8e799e2 100644
@@ -624,7 +624,8 @@ static void vscsi_save_request(QEMUFile *f, SCSIRequest *sreq)
     vscsi_req *req = sreq->hba_private;
     assert(req->active);
 
-    vmstate_save_state(f, &vmstate_spapr_vscsi_req, req);
+    vmstate_save_state(qemu_file_get_tmp_visitor(f), &vmstate_spapr_vscsi_req,
+                       req);
 
     DPRINTF("VSCSI: saving tag=%u, current desc#%d, offset=%x\n",
             req->qtag, req->cur_desc_num, req->cur_desc_offset);
include/migration/vmstate.h +4 -2
index a5e4b0b..b66342b 100644
@@ -40,7 +40,7 @@ typedef struct SaveVMHandlers {
     SaveStateHandler *save_state;
 
     void (*cancel)(void *opaque);
-    int (*save_live_complete)(QEMUFile *f, void *opaque);
+    int (*save_live_complete)(Visitor *v, void *opaque);
 
     /* This runs both outside and inside the iothread lock.  */
     bool (*is_active)(void *opaque);
@@ -98,6 +98,8 @@ struct VMStateInfo {
     const char *name;
     int (*get)(QEMUFile *f, void *pv, size_t size);
     void (*put)(QEMUFile *f, void *pv, size_t size);
+    int (*visit)(Visitor *v, void *pdata, const char* name, size_t size,
+                 Error **errp);
 };
 
 enum VMStateFlags {
@@ -759,7 +761,7 @@ extern const VMStateInfo vmstate_info_bitmap;
 
 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
                        void *opaque, int version_id);
-void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
+void vmstate_save_state(Visitor *v, const VMStateDescription *vmsd,
                         void *opaque);
 
 int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
savevm.c +148 -60

324 lines not on this page · raw message

vmstate.c +87 -15

155 lines not on this page · raw message

Signature · 1 line
1.8.5.3
#14 lore raw

RFC PATCH 9/16 Visitor: Load path

In reply to Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

Hacks in piix4.c, pci.c, spapr_vscsi.c for now

---
 arch_init.c                 |  72 ++++++++++++++---------
 hw/acpi/piix4.c             |   5 +-
 hw/pci/pci.c                |   3 +-
 hw/scsi/spapr_vscsi.c       |   3 +-
 include/migration/vmstate.h |   2 +-
 savevm.c                    | 133 +++++++++++++++++++++++++++++++------------
 vmstate.c                   | 135 +++++++++++++++++++++++++++++++++-----------
 7 files changed, 251 insertions(+), 102 deletions(-)
arch_init.c +44 -28
index 02bf78a..145666d 100644
@@ -1004,15 +1004,12 @@ static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)
     return rc;
 }
 
-static inline void *host_from_stream_offset(QEMUFile *f,
-                                            ram_addr_t offset,
-                                            int flags)
+static inline void *host_from_stream_offset(ramsecentry_header *rse_hdr)
 {
     static RAMBlock *block = NULL;
-    char id[256];
-    uint8_t len;
+    ram_addr_t offset = rse_hdr->addr;
 
-    if (flags & RAM_SAVE_FLAG_CONTINUE) {
+    if (rse_hdr->flags & RAM_SAVE_FLAG_CONTINUE) {
         if (!block) {
             fprintf(stderr, "Ack, bad migration stream!\n");
             return NULL;
@@ -1021,16 +1018,13 @@ static inline void *host_from_stream_offset(QEMUFile *f,
         return memory_region_get_ram_ptr(block->mr) + offset;
     }
 
-    len = qemu_get_byte(f);
-    qemu_get_buffer(f, (uint8_t *)id, len);
-    id[len] = 0;
-
     QTAILQ_FOREACH(block, &ram_list.blocks, next) {
-        if (!strncmp(id, block->idstr, sizeof(id)))
+        if (!strncmp(rse_hdr->idstr, block->idstr, sizeof(rse_hdr->idstr))) {
             return memory_region_get_ram_ptr(block->mr) + offset;
+        }
     }
 
-    fprintf(stderr, "Can't find block %s!\n", id);
+    fprintf(stderr, "Can't find block %s!\n", rse_hdr->idstr);
     return NULL;
 }
 
@@ -1051,6 +1045,9 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)
     int flags, ret = 0;
     int error;
     static uint64_t seq_iter;
+    Error *local_err = NULL;
+    ramsecentry_header rse_hdr;
+    Visitor *v = qemu_file_get_tmp_visitor(f); // TODO
 
     seq_iter++;
 
@@ -1058,11 +1055,18 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)
         return -EINVAL;
     }
 
-    do {
-        addr = qemu_get_be64(f);
-
-        flags = addr & ~TARGET_PAGE_MASK;
-        addr &= TARGET_PAGE_MASK;
+    visit_start_sequence_compat(v, "ramseclist", VISIT_SEQ_COMPAT_RAMSECLIST,
+                                NULL, &local_err);
+    LOCAL_ERR_REPORT(return -EINVAL;);
+    while (visit_get_next_type(v, &flags, NULL, "ramseclist", &local_err),
+           (!local_err && !(flags & RAM_SAVE_FLAG_EOS))) {
+        LOCAL_ERR_REPORT(return -EINVAL;);
+        visit_start_sequence_compat(v, "ramsecentry",
+                                    VISIT_SEQ_COMPAT_RAMSECENTRY, &rse_hdr,
+                                    &local_err);
+        addr = rse_hdr.addr;
+        flags = rse_hdr.flags;
+        LOCAL_ERR_REPORT(return -EINVAL;);
 
         if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
             if (version_id == 4) {
@@ -1071,14 +1075,17 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)
                 ram_addr_t length;
                 ram_addr_t total_ram_bytes = addr;
 
+                visit_start_list(v, "blocklist", &local_err);
                 while (total_ram_bytes) {
                     RAMBlock *block;
-                    uint8_t len;
+                    visit_start_struct(v, NULL, "blockid", "blockid", 0,
+                                       &local_err);
+                    visit_type_str256(v, id, "blockname", &local_err);
+                    visit_type_uint64(v, &length, "blocklen", &local_err);
+                    LOCAL_ERR_REPORT(ret = -EINVAL; goto done;);
+                    visit_end_struct(v, &local_err);
+                    LOCAL_ERR_REPORT(ret = -EINVAL; goto done;);
 
-                    len = qemu_get_byte(f);
-                    qemu_get_buffer(f, (uint8_t *)id, len);
-                    id[len] = 0;
-                    length = qemu_get_be64(f);
 
                     QTAILQ_FOREACH(block, &ram_list.blocks, next) {
                         if (!strncmp(id, block->idstr, sizeof(id))) {
@@ -1103,6 +1110,9 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)
 
                     total_ram_bytes -= length;
                 }
+                LOCAL_ERR_REPORT(ret = -EINVAL; goto done;);
+                visit_end_list(v, &local_err);
+                LOCAL_ERR_REPORT(ret = -EINVAL; goto done;);
             }
         }
 
@@ -1110,24 +1120,25 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)
             void *host;
             uint8_t ch;
 
-            host = host_from_stream_offset(f, addr, flags);
+            host = host_from_stream_offset(&rse_hdr);
             if (!host) {
                 return -EINVAL;
             }
 
-            ch = qemu_get_byte(f);
+            visit_type_uint8(v, &ch, "pagefill", &local_err);
             ram_handle_compressed(host, ch, TARGET_PAGE_SIZE);
         } else if (flags & RAM_SAVE_FLAG_PAGE) {
             void *host;
 
-            host = host_from_stream_offset(f, addr, flags);
+            host = host_from_stream_offset(&rse_hdr);
             if (!host) {
                 return -EINVAL;
             }
 
-            qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
+            visit_type_buffer(v, host, TARGET_PAGE_SIZE, false, "page",
+                              &local_err);
         } else if (flags & RAM_SAVE_FLAG_XBZRLE) {
-            void *host = host_from_stream_offset(f, addr, flags);
+            void *host = host_from_stream_offset(&rse_hdr);
             if (!host) {
                 return -EINVAL;
             }
@@ -1144,7 +1155,12 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)
             ret = error;
             goto done;
         }
-    } while (!(flags & RAM_SAVE_FLAG_EOS));
+        visit_end_sequence_compat(v, "ramsecentry",
+                                  VISIT_SEQ_COMPAT_RAMSECENTRY, &local_err);
+    };
+    visit_end_sequence_compat(v, "ramseclist", VISIT_SEQ_COMPAT_RAMSECLIST,
+                              &local_err);
+    LOCAL_ERR_REPORT(return -EINVAL;);
 
 done:
     DPRINTF("Completed load of VM with exit code %d seq iteration "
hw/acpi/piix4.c +3 -2
index 67dc075..a44f4b8 100644
@@ -211,7 +211,8 @@ static int acpi_load_old(QEMUFile *f, void *opaque, int version_id)
     qemu_get_be16s(f, &s->ar.pm1.evt.en);
     qemu_get_be16s(f, &s->ar.pm1.cnt.cnt);
 
-    ret = vmstate_load_state(f, &vmstate_apm, &s->apm, 1);
+    ret = vmstate_load_state(qemu_file_get_tmp_visitor(f), &vmstate_apm,
+                             &s->apm, 1);
     if (ret) {
         return ret;
     }
@@ -229,7 +230,7 @@ static int acpi_load_old(QEMUFile *f, void *opaque, int version_id)
         qemu_get_be16s(f, &temp);
     }
 
-    ret = vmstate_load_state(f, &vmstate_pci_status,
+    ret = vmstate_load_state(qemu_file_get_tmp_visitor(f), &vmstate_pci_status,
         &s->acpi_pci_hotplug.acpi_pcihp_pci_status[ACPI_PCIHP_BSEL_DEFAULT], 1);
     return ret;
 }
hw/pci/pci.c +2 -1
index e91f729..2009c0f 100644
@@ -523,7 +523,8 @@ void pci_device_save(PCIDevice *s, QEMUFile *f)
 int pci_device_load(PCIDevice *s, QEMUFile *f)
 {
     int ret;
-    ret = vmstate_load_state(f, pci_get_vmstate(s), s, s->version_id);
+    ret = vmstate_load_state(qemu_file_get_tmp_visitor(f),
+                             pci_get_vmstate(s), s, s->version_id);
     /* Restore the interrupt status bit. */
     pci_update_irq_status(s);
     return ret;
hw/scsi/spapr_vscsi.c +2 -1
index 8e799e2..c50a741 100644
@@ -643,7 +643,8 @@ static void *vscsi_load_request(QEMUFile *f, SCSIRequest *sreq)
     assert(!req->active);
 
     memset(req, 0, sizeof(*req));
-    rc = vmstate_load_state(f, &vmstate_spapr_vscsi_req, req, 1);
+    rc = vmstate_load_state(qemu_file_get_tmp_visitor(f),
+                            &vmstate_spapr_vscsi_req, req, 1);
     if (rc) {
         fprintf(stderr, "VSCSI: failed loading request tag#%u\n", sreq->tag);
         return NULL;
include/migration/vmstate.h +1 -1
index b66342b..d22c4e3 100644
@@ -759,7 +759,7 @@ extern const VMStateInfo vmstate_info_bitmap;
 #define VMSTATE_END_OF_LIST()                                         \
     {}
 
-int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
+int vmstate_load_state(Visitor *v, const VMStateDescription *vmsd,
                        void *opaque, int version_id);
 void vmstate_save_state(Visitor *v, const VMStateDescription *vmsd,
                         void *opaque);
savevm.c +97 -36

214 lines not on this page · raw message

vmstate.c +102 -33

207 lines not on this page · raw message

Signature · 1 line
1.8.5.3
#15 lore raw

RFC PATCH 10/16 Visitor: Common types to use visitors

In reply to Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

Rework the common types in vmstate.c to use .visit rather than .get/.put and now call the correct visitor function.

---
 vmstate.c | 376 ++++++++++++--------------------------------------------------
 1 file changed, 73 insertions(+), 303 deletions(-)
vmstate.c +73 -303
index af332b7..5a90cbe 100644
@@ -1,3 +1,5 @@
+#include <inttypes.h>
+
 #include "qemu-common.h"
 #include "migration/migration.h"
 #include "migration/qemu-file.h"
@@ -357,320 +359,88 @@ static void vmstate_subsection_save(Visitor *v, const VMStateDescription *vmsd,
     LOCAL_ERR_REPORT(return;);
 }
 
-/* bool */
-
-static int get_bool(QEMUFile *f, void *pv, size_t size)
-{
-    bool *v = pv;
-    *v = qemu_get_byte(f);
-    return 0;
-}
-
-static void put_bool(QEMUFile *f, void *pv, size_t size)
-{
-    bool *v = pv;
-    qemu_put_byte(f, *v);
-}
-
-const VMStateInfo vmstate_info_bool = {
-    .name = "bool",
-    .get  = get_bool,
-    .put  = put_bool,
-};
-
-/* 8 bit int */
-
-static int get_int8(QEMUFile *f, void *pv, size_t size)
-{
-    int8_t *v = pv;
-    qemu_get_s8s(f, v);
-    return 0;
-}
-
-static void put_int8(QEMUFile *f, void *pv, size_t size)
-{
-    int8_t *v = pv;
-    qemu_put_s8s(f, v);
-}
-
-const VMStateInfo vmstate_info_int8 = {
-    .name = "int8",
-    .get  = get_int8,
-    .put  = put_int8,
-};
-
-/* 16 bit int */
-
-static int get_int16(QEMUFile *f, void *pv, size_t size)
-{
-    int16_t *v = pv;
-    qemu_get_sbe16s(f, v);
-    return 0;
-}
-
-static void put_int16(QEMUFile *f, void *pv, size_t size)
-{
-    int16_t *v = pv;
-    qemu_put_sbe16s(f, v);
-}
-
-const VMStateInfo vmstate_info_int16 = {
-    .name = "int16",
-    .get  = get_int16,
-    .put  = put_int16,
-};
-
-/* 32 bit int */
-
-static int get_int32(QEMUFile *f, void *pv, size_t size)
-{
-    int32_t *v = pv;
-    qemu_get_sbe32s(f, v);
-    return 0;
-}
-
-static void put_int32(QEMUFile *f, void *pv, size_t size)
-{
-    int32_t *v = pv;
-    qemu_put_sbe32s(f, v);
-}
-
-const VMStateInfo vmstate_info_int32 = {
-    .name = "int32",
-    .get  = get_int32,
-    .put  = put_int32,
-};
-
-/* 32 bit int. See that the received value is the same than the one
-   in the field */
-
-static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
-{
-    int32_t *v = pv;
-    int32_t v2;
-    qemu_get_sbe32s(f, &v2);
-
-    if (*v == v2) {
-        return 0;
-    }
-    return -EINVAL;
-}
-
-const VMStateInfo vmstate_info_int32_equal = {
-    .name = "int32 equal",
-    .get  = get_int32_equal,
-    .put  = put_int32,
-};
+#define VMSTATE_INFO_FORTYPE(vn, vt) \
+    static int visit_ ## vn(Visitor *v, void *pdata, const char *name, \
+                      size_t size, Error **errp)                       \
+    {                                                                  \
+    visit_type_ ## vn(v, (vt *)pdata, name, errp);                     \
+    return 0;                                                          \
+    }                                                                  \
+    const VMStateInfo vmstate_info_ ## vn = {                          \
+    .name = #vn ,                                                      \
+    .visit = visit_ ## vn,                                             \
+}
+
+VMSTATE_INFO_FORTYPE(bool, bool);
+VMSTATE_INFO_FORTYPE(int8, int8_t);
+VMSTATE_INFO_FORTYPE(int16, int16_t);
+VMSTATE_INFO_FORTYPE(int32, int32_t);
+VMSTATE_INFO_FORTYPE(int64, int64_t);
+VMSTATE_INFO_FORTYPE(uint8, uint8_t);
+VMSTATE_INFO_FORTYPE(uint16, uint16_t);
+VMSTATE_INFO_FORTYPE(uint32, uint32_t);
+VMSTATE_INFO_FORTYPE(uint64, uint64_t);
+
+/* equality entries; Check that the value read from the stream is the same
+ * as the one in the field */
+#define VMSTATE_INFO_EQ_FORTYPE(vn, vt, vf) \
+    static int visit_ ## vn ## _equal(Visitor *v, void *pdata,             \
+                      const char *name, size_t size, Error **errp)         \
+    {                                                                      \
+        if (visitor_loading(v)) {                                          \
+            vt received;                                                   \
+                                                                           \
+            visit_type_ ## vn(v, &received, name, errp);                   \
+            if (*(vt *)pdata == received) {                                \
+                return 0;                                                  \
+            }                                                              \
+            error_setg(errp,                                               \
+                       "Unexpected value for field '%s', expected 0x%" vf  \
+                       " but got 0x%" vf, name, *(vt *)pdata, received);   \
+            return -EINVAL;                                                \
+        } else {                                                           \
+            visit_type_ ## vn(v, (vt *)pdata, name, errp);                 \
+            return 0;                                                      \
+        }                                                                  \
+    }                                                                      \
+    const VMStateInfo vmstate_info_ ## vn ## _equal = {                    \
+    .name = #vn " equal",                                                  \
+    .visit = visit_ ## vn ## _equal,                                       \
+}
+
+VMSTATE_INFO_EQ_FORTYPE(int32, int32_t, PRIx32);
+VMSTATE_INFO_EQ_FORTYPE(uint8, uint8_t, PRIx8);
+VMSTATE_INFO_EQ_FORTYPE(uint16, uint16_t, PRIx16);
+VMSTATE_INFO_EQ_FORTYPE(uint32, uint32_t, PRIx32);
+VMSTATE_INFO_EQ_FORTYPE(uint64, uint64_t, PRIx64);
 
 /* 32 bit int. Check that the received value is less than or equal to
    the one in the field */
-
-static int get_int32_le(QEMUFile *f, void *pv, size_t size)
+static int visit_int32_le(Visitor *v, void *pdata, const char *name,
+                          size_t size, Error **errp)
 {
-    int32_t *cur = pv;
-    int32_t loaded;
-    qemu_get_sbe32s(f, &loaded);
+    if (visitor_loading(v)) {
+        int32_t loaded;
 
-    if (loaded <= *cur) {
-        *cur = loaded;
+        visit_type_int32(v, &loaded, name, errp);
+        if (loaded <= *(int32_t *)pdata) {
+            *(int32_t *)pdata = loaded;
+            return 0;
+        }
+        error_setg(errp, "Unexpected value for field '%s', expected a value"
+                   " no larger than %d but got %d", name, *(int32_t *)pdata,
+                   loaded);
+        return -EINVAL;
+    } else {
+        visit_type_int32(v, (int32_t *)pdata, name, errp);
         return 0;
     }
-    return -EINVAL;
+
 }
 
 const VMStateInfo vmstate_info_int32_le = {
     .name = "int32 le",
-    .get  = get_int32_le,
-    .put  = put_int32,
-};
-
-/* 64 bit int */
-
-static int get_int64(QEMUFile *f, void *pv, size_t size)
-{
-    int64_t *v = pv;
-    qemu_get_sbe64s(f, v);
-    return 0;
-}
-
-static void put_int64(QEMUFile *f, void *pv, size_t size)
-{
-    int64_t *v = pv;
-    qemu_put_sbe64s(f, v);
-}
-
-const VMStateInfo vmstate_info_int64 = {
-    .name = "int64",
-    .get  = get_int64,
-    .put  = put_int64,
-};
-
-/* 8 bit unsigned int */
-
-static int get_uint8(QEMUFile *f, void *pv, size_t size)
-{
-    uint8_t *v = pv;
-    qemu_get_8s(f, v);
-    return 0;
-}
-
-static void put_uint8(QEMUFile *f, void *pv, size_t size)
-{
-    uint8_t *v = pv;
-    qemu_put_8s(f, v);
-}
-
-const VMStateInfo vmstate_info_uint8 = {
-    .name = "uint8",
-    .get  = get_uint8,
-    .put  = put_uint8,
-};
-
-/* 16 bit unsigned int */
-
-static int get_uint16(QEMUFile *f, void *pv, size_t size)
-{
-    uint16_t *v = pv;
-    qemu_get_be16s(f, v);
-    return 0;
-}
-
-static void put_uint16(QEMUFile *f, void *pv, size_t size)
-{
-    uint16_t *v = pv;
-    qemu_put_be16s(f, v);
-}
-
-const VMStateInfo vmstate_info_uint16 = {
-    .name = "uint16",
-    .get  = get_uint16,
-    .put  = put_uint16,
-};
-
-/* 32 bit unsigned int */
-
-static int get_uint32(QEMUFile *f, void *pv, size_t size)
-{
-    uint32_t *v = pv;
-    qemu_get_be32s(f, v);
-    return 0;
-}
-
-static void put_uint32(QEMUFile *f, void *pv, size_t size)
-{
-    uint32_t *v = pv;
-    qemu_put_be32s(f, v);
-}
-
-const VMStateInfo vmstate_info_uint32 = {
-    .name = "uint32",
-    .get  = get_uint32,
-    .put  = put_uint32,
-};
-
-/* 32 bit uint. See that the received value is the same than the one
-   in the field */
-
-static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
-{
-    uint32_t *v = pv;
-    uint32_t v2;
-    qemu_get_be32s(f, &v2);
-
-    if (*v == v2) {
-        return 0;
-    }
-    return -EINVAL;
-}
-
-const VMStateInfo vmstate_info_uint32_equal = {
-    .name = "uint32 equal",
-    .get  = get_uint32_equal,
-    .put  = put_uint32,
-};
-
-/* 64 bit unsigned int */
-
-static int get_uint64(QEMUFile *f, void *pv, size_t size)
-{
-    uint64_t *v = pv;
-    qemu_get_be64s(f, v);
-    return 0;
-}
-
-static void put_uint64(QEMUFile *f, void *pv, size_t size)
-{
-    uint64_t *v = pv;
-    qemu_put_be64s(f, v);
-}
-
-const VMStateInfo vmstate_info_uint64 = {
-    .name = "uint64",
-    .get  = get_uint64,
-    .put  = put_uint64,
-};
-
-/* 64 bit unsigned int. See that the received value is the same than the one
-   in the field */
-
-static int get_uint64_equal(QEMUFile *f, void *pv, size_t size)
-{
-    uint64_t *v = pv;
-    uint64_t v2;
-    qemu_get_be64s(f, &v2);
-
-    if (*v == v2) {
-        return 0;
-    }
-    return -EINVAL;
-}
-
-const VMStateInfo vmstate_info_uint64_equal = {
-    .name = "int64 equal",
-    .get  = get_uint64_equal,
-    .put  = put_uint64,
-};
-
-/* 8 bit int. See that the received value is the same than the one
-   in the field */
-
-static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
-{
-    uint8_t *v = pv;
-    uint8_t v2;
-    qemu_get_8s(f, &v2);
-
-    if (*v == v2) {
-        return 0;
-    }
-    return -EINVAL;
-}
-
-const VMStateInfo vmstate_info_uint8_equal = {
-    .name = "uint8 equal",
-    .get  = get_uint8_equal,
-    .put  = put_uint8,
-};
-
-/* 16 bit unsigned int int. See that the received value is the same than the one
-   in the field */
-
-static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
-{
-    uint16_t *v = pv;
-    uint16_t v2;
-    qemu_get_be16s(f, &v2);
-
-    if (*v == v2) {
-        return 0;
-    }
-    return -EINVAL;
-}
-
-const VMStateInfo vmstate_info_uint16_equal = {
-    .name = "uint16 equal",
-    .get  = get_uint16_equal,
-    .put  = put_uint16,
+    .visit = visit_int32_le,
 };
 
 /* floating point */
Signature · 1 line
1.8.5.3
#16 lore raw

RFC PATCH 11/16 Choose output visitor based on env variable

In reply to Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

---
 savevm.c | 39 ++++++++++++++++++++++++++++++++-------
 1 file changed, 32 insertions(+), 7 deletions(-)
savevm.c +32 -7
index 7431773..25e92f2 100644
@@ -26,6 +26,7 @@
 #include "qemu-common.h"
 #include "qapi/qemu-file-binary-input-visitor.h"
 #include "qapi/qemu-file-binary-output-visitor.h"
+#include "qapi/qemu-file-debug-output-visitor.h"
 #include "hw/hw.h"
 #include "hw/qdev.h"
 #include "net/net.h"
@@ -524,6 +525,35 @@ bool qemu_savevm_state_blocked(Error **errp)
     return false;
 }
 
+/* Return a visitor for use on the QEMUFile; visit_destroy should be
+ * called on it to clean it up.
+ */
+static Visitor *qemu_savevm_get_visitor(QEMUFile *f)
+{
+    char *formatvar = getenv("QEMUMIGFORMAT");
+
+    if (formatvar && (!strcmp(formatvar, "debug"))) {
+        QemuFileDebugOutputVisitor *qfdov =
+            qemu_file_debug_output_visitor_new(f);
+        Visitor *v = qemu_file_debug_output_get_visitor(qfdov);
+
+        qemu_file_set_tmp_visitor(f, v);
+        return v;
+    }
+
+    if (formatvar) {
+        error_report("QEMUMIGFORMAT set to unknown value '%s'", formatvar);
+        assert(0);
+    }
+
+    QemuFileBinOutputVisitor *qfbov = qemu_file_bin_output_visitor_new(f);
+    Visitor *v = qemu_file_bin_output_get_visitor(qfbov);
+
+    qemu_file_set_tmp_visitor(f, v);
+
+    return v;
+}
+
 void qemu_savevm_state_begin(QEMUFile *f,
                              const MigrationParams *params)
 {
@@ -531,10 +561,7 @@ void qemu_savevm_state_begin(QEMUFile *f,
     int ret;
     Error *local_err = NULL;
     SectionHeader sh;
-
-    QemuFileBinOutputVisitor *qfbov = qemu_file_bin_output_visitor_new(f);
-    Visitor *v = qemu_file_bin_output_get_visitor(qfbov);
-    qemu_file_set_tmp_visitor(f, v);
+    Visitor *v = qemu_savevm_get_visitor(f);
 
     QTAILQ_FOREACH(se, &savevm_handlers, entry) {
         if (!se->ops || !se->ops->set_params) {
@@ -781,13 +808,11 @@ static int qemu_savevm_state(QEMUFile *f)
 
 static int qemu_save_device_state(QEMUFile *f)
 {
+    Visitor *v = qemu_savevm_get_visitor(f);
     SaveStateEntry *se;
     Error *local_err;
     SectionHeader sh;
 
-    QemuFileBinOutputVisitor *qfbov = qemu_file_bin_output_visitor_new(f);
-    qemu_file_set_tmp_visitor(f, qemu_file_bin_output_get_visitor(qfbov));
-    Visitor *v = qemu_file_bin_output_get_visitor(qfbov);
     int32_t section_type;
 
     cpu_synchronize_all_states();
Signature · 1 line
1.8.5.3
#17 lore raw

RFC PATCH 12/16 BER Visitor: Create output visitor

In reply to Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

ber.h: Add VMSTATE_CPU_COMMON

---
 include/qapi/ber.h                          | 108 ++++
 include/qapi/qemu-file-ber-output-visitor.h |  26 +
 qapi/Makefile.objs                          |   1 +
 qapi/qemu-file-ber-output-visitor.c         | 916 ++++++++++++++++++++++++++++
 4 files changed, 1051 insertions(+)
 create mode 100644 include/qapi/ber.h
 create mode 100644 include/qapi/qemu-file-ber-output-visitor.h
 create mode 100644 qapi/qemu-file-ber-output-visitor.c
include/qapi/ber.h new file +108 -0
new file mode 100644
index 0000000..960bb17
@@ -0,0 +1,108 @@
+/*
+ * ASN.1 Basic Encoding Rules Common functions
+ *
+ * Copyright IBM, Corp. 2011, 2013
+ * Copyright Red Hat, Inc. 2011
+ *
+ * Authors:
+ *  Stefan Berger     <[email protected]>
+ *  Michael Tsirkin   <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ */
+#ifndef QAPI_BER_H
+#define QAPI_BER_H
+
+/*
+ * This is a subset of BER for QEMU use.
+ * QEMU will use the DER encoding always with one extension from
+ * CER: SET and SEQUENCE types can have indefinite-length encoding
+ * if the encoding is not all immediately available.
+ *
+ * We assume that SET encodings can be available or not available,
+ * and that SEQUENCE encodings are available unless a SEQUENCE includes
+ * a non-available SET.
+ *
+ * The last is an extension to allow an arbitrarily large SET
+ * to be produced online without knowing the length in advance.
+ *
+ * All types used shall be universal, with explicit tagging, to simplify
+ * use by external tools.
+ */
+
+
+#define BER_TYPE_CLASS_SHIFT  6
+#define BER_TYPE_PC_SHIFT     5
+
+typedef enum ber_type_class {
+    BER_TYPE_CLASS_UNIVERSAL = 0x0 << BER_TYPE_CLASS_SHIFT,
+    BER_TYPE_CLASS_APPLICATION = 0x1 << BER_TYPE_CLASS_SHIFT,
+    BER_TYPE_CLASS_CONTENT_SPECIFIC = 0x2 << BER_TYPE_CLASS_SHIFT,
+    BER_TYPE_CLASS_PRIVATE = 0x3 << BER_TYPE_CLASS_SHIFT,
+    BER_TYPE_CLASS_MASK = 0x3 << BER_TYPE_CLASS_SHIFT /* Mask to get class */
+} BERTypeClass;
+
+/* P/C bit */
+typedef enum ber_type_p_c {
+    BER_TYPE_PRIMITIVE = 0x0 << BER_TYPE_PC_SHIFT,
+    BER_TYPE_CONSTRUCTED = 0x1 << BER_TYPE_PC_SHIFT,
+    BER_TYPE_P_C_MASK = 0x1 << BER_TYPE_PC_SHIFT /* Mask to get P/C bit */
+} BERTypePC;
+
+typedef enum ber_type_tag {
+    BER_TYPE_EOC          =  0 /*  P        0       0*/,
+    BER_TYPE_BOOLEAN      =  1 /*  P        1       1*/,
+    BER_TYPE_INTEGER      =  2 /*  P        2       2*/,
+    BER_TYPE_OCTET_STRING =  4 /*  P/C      4       4*/,
+    BER_TYPE_NULL         =  5 /*  P        5       5*/,
+    BER_TYPE_UTF8_STRING  = 12 /*  P/C      12      C*/,
+    BER_TYPE_SEQUENCE     = 16 /*  C        16      10*/,
+    BER_TYPE_SET          = 17 /*  C        17      11*/,
+  /*BER_TYPE_NUMERIC_STRING       P/C      18      12*/
+  /*BER_TYPE_PRINTABLE_STRING     P/C      19      13*/
+  /*BER_TYPE_T61STRING            P/C      20      14*/
+  /*BER_TYPE_VIDEOTEX_STRING      P/C      21      15*/
+  /*BER_TYPE_IA5_STRING           P/C      22      16*/
+  /*BER_TYPE_UTCTIME              P/C      23      17*/
+  /*BER_TYPE_GENERALIZED_TIME     P/C      24      18*/
+  /*BER_TYPE_GRAPHIC_STRING       P/C      25      19*/
+  /*BER_TYPE_VISIBLE_STRING       P/C      26      1A*/
+  /*BER_TYPE_GENERAL_STRING       P/C      27      1B*/
+  /*BER_TYPE_UNIVERSAL_STRING     P/C      28      1C*/
+  /*BER_TYPE_CHARACTER_STRING     P/C      29      1D*/
+  /*BER_TYPE_BMP_STRING           P/C      30      1E*/
+    BER_TYPE_LONG_FORM    = 31/*  -        31      1F*/,
+    BER_TYPE_TAG_MASK = 0x1f /* Mask to get tag */,
+    BER_TYPE_CUSTOM_LIST = 0x20,
+
+    /* It would be nice to autoderive this from the spec file somehow */
+    BER_TYPE_QEMU_FILE = 1270481, /* come out as 7f cd c5 51  - the 51 is Q
+                                   * the c5 and cd being E,M but with the top
+                                   * bit set which BER requires.
+                                   */
+    BER_TYPE_QEMU_RAMSEC_ENTRY =  8914,
+    BER_TYPE_QEMU_RAMSEC_LIST  =  9810,
+    BER_TYPE_QEMU_SEC_MIN      =   211,
+    BER_TYPE_QEMU_SEC_FULL     =  2003,
+    BER_TYPE_QEMU_SUBSEC_LIST  = 10700,
+    BER_TYPE_QEMU_SUBSEC       = 10707,
+
+    /* Specific VMState types */
+    BER_TYPE_QEMU_VMSTATE_CPU_COMMON = 20000,
+} BERTypeTag;
+
+typedef enum ber_length {
+    /* Special length values */
+    BER_LENGTH_INDEFINITE = 0x1 << 7,
+    BER_LENGTH_RESERVED = 0xFF,
+    /* Anything else is either short or long */
+    BER_LENGTH_SHORT = 0x0 << 7,
+    BER_LENGTH_LONG = 0x1 << 7,
+    BER_LENGTH_SHORT_LONG_MASK = 0x1 << 7,
+    BER_LENGTH_MASK = 0x7F,
+} BERLength;
+
+#endif /* QAPI_BER_H */
+
include/qapi/qemu-file-ber-output-visitor.h new file +26 -0
new file mode 100644
index 0000000..341b28a
@@ -0,0 +1,26 @@
+/*
+ * QEMUFile BER format output visitor
+ *
+ * Copyright 2014 Red Hat, Inc. and/or its affiliates
+ *
+ * Authors:
+ *  David Gilbert <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ */
+
+#ifndef QEMU_FILE_BER_OUTPUT_VISITOR_H
+#define QEMU_FILE_BER_OUTPUT_VISITOR_H
+
+#include "visitor.h"
+
+typedef struct QemuFileBEROutputVisitor QemuFileBEROutputVisitor;
+
+QemuFileBEROutputVisitor *qemu_file_ber_output_visitor_new(QEMUFile *f);
+void qemu_file_ber_output_visitor_cleanup(QemuFileBEROutputVisitor *d);
+
+Visitor *qemu_file_ber_output_get_visitor(QemuFileBEROutputVisitor *v);
+
+#endif
qapi/Makefile.objs +1 -0
index 3d9d47a..f8fb347 100644
@@ -2,5 +2,6 @@ util-obj-y = qapi-visit-core.o qapi-dealloc-visitor.o qmp-input-visitor.o
 util-obj-y += qmp-output-visitor.o qmp-registry.o qmp-dispatch.o
 util-obj-y += string-input-visitor.o string-output-visitor.o
 util-obj-y += qemu-file-binary-output-visitor.o qemu-file-binary-input-visitor.o
+util-obj-y += qemu-file-ber-output-visitor.o
 util-obj-y += qemu-file-debug-output-visitor.o
 util-obj-y += opts-visitor.o
qapi/qemu-file-ber-output-visitor.c new file +916 -0

916 lines not on this page · raw message

Signature · 1 line
1.8.5.3
#18 lore raw

RFC PATCH 13/16 BER Visitor: Create input visitor

In reply to Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

---
 include/qapi/qemu-file-ber-input-visitor.h |   26 +
 qapi/Makefile.objs                         |    2 +-
 qapi/qemu-file-ber-input-visitor.c         | 1163 ++++++++++++++++++++++++++++
 3 files changed, 1190 insertions(+), 1 deletion(-)
 create mode 100644 include/qapi/qemu-file-ber-input-visitor.h
 create mode 100644 qapi/qemu-file-ber-input-visitor.c
include/qapi/qemu-file-ber-input-visitor.h new file +26 -0
new file mode 100644
index 0000000..18045f7
@@ -0,0 +1,26 @@
+/*
+ * QEMUFile input visitor for BER format files
+ *
+ * Copyright 2014 Red Hat, Inc. and/or its affiliates
+ *
+ * Authors:
+ *  David Gilbert <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ */
+
+#ifndef QEMU_FILE_BER_INPUT_VISITOR_H
+#define QEMU_FILE_BER_INPUT_VISITOR_H
+
+#include "visitor.h"
+
+typedef struct QemuFileBERInputVisitor QemuFileBERInputVisitor;
+
+QemuFileBERInputVisitor *qemu_file_ber_input_visitor_new(QEMUFile *f);
+void qemu_file_ber_input_visitor_cleanup(QemuFileBERInputVisitor *d);
+
+Visitor *qemu_file_ber_input_get_visitor(QemuFileBERInputVisitor *v);
+
+#endif
qapi/Makefile.objs +1 -1
index f8fb347..c91dc46 100644
@@ -2,6 +2,6 @@ util-obj-y = qapi-visit-core.o qapi-dealloc-visitor.o qmp-input-visitor.o
 util-obj-y += qmp-output-visitor.o qmp-registry.o qmp-dispatch.o
 util-obj-y += string-input-visitor.o string-output-visitor.o
 util-obj-y += qemu-file-binary-output-visitor.o qemu-file-binary-input-visitor.o
-util-obj-y += qemu-file-ber-output-visitor.o
+util-obj-y += qemu-file-ber-input-visitor.o qemu-file-ber-output-visitor.o
 util-obj-y += qemu-file-debug-output-visitor.o
 util-obj-y += opts-visitor.o
qapi/qemu-file-ber-input-visitor.c new file +1163 -0

1,163 lines not on this page · raw message

Signature · 1 line
1.8.5.3
#19 lore raw

RFC PATCH 14/16 Start some BER format docs

In reply to Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

Add information on the QEMUFile/Visitor/VMState split Describe how to verify a BER stream against the schema

---
 docs/migration.txt | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)
docs/migration.txt +32 -2
index 0e0a1d4..894abf3 100644
@@ -48,8 +48,17 @@ This is used for RAM and block devices.  It is not yet ported to vmstate.
 
 === What is the common infrastructure ===
 
-QEMU uses a QEMUFile abstraction to be able to do migration.  Any type
-of migration that wants to use QEMU infrastructure has to create a
+Migration consists of a number of layers that handle different parts of the
+problem:
+    QEMUFile - Buffering/reading/writing data without worrying about it's
+               contents. Versions exist for file I/O, sockets, RDMA and
+               to put data into a QEMU Block device.
+    Visitor  - Visitors format data onto the QEMUFile. A binary format and
+               a BER format currently exist.
+    VMState  - Represent the structure of the data for a given device with
+               support for versioning.
+
+Any type of migration that wants to use QEMU infrastructure has to create a
 QEMUFile with:
 
 QEMUFile *qemu_fopen_ops(void *opaque,
@@ -296,3 +305,24 @@ save/send this state when we are in the middle of a pio operation
 (that is what ide_drive_pio_state_needed() checks).  If DRQ_STAT is
 not enabled, the values on that fields are garbage and don't need to
 be sent.
+
+ASN.1/BER format migration
+==========================
+
+In BER format migration the migration stream is described by an ASN.1 schema
+(that's defined in docs/specs/migration.schema).
+
+The 'asn1c' program can be used to validate a migration stream against the schema
+ - although it'll probably use a lot of memory doing it - so it's best to do it
+with a small RAM image.
+
+To validate an image against the schema:
+  a) Make a temporary directory and cd into it
+  b) asn1c -fcompound-names /path/to/migration.schema
+  c) make -f Makefile.am.sample CFLAGS+="-DPDU=QemuFile -DEMIT_ASN_DEBUG=1 -I."
+  d) ./progname -iber log.ber -d 2> debug > decoding
+
+  If it's happy the 'decoding' output should be an XML like dump of the input file;
+if it's not happy, the debug file might help you find out why (Search backwards
+from the end for the first 'Freeing' to find the point at which it detected something
+bad).
Signature · 1 line
1.8.5.3
#20 lore raw

RFC PATCH 15/16 ASN.1 schema for new migration format

In reply to Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

---
 docs/specs/migration.schema | 113 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 113 insertions(+)
 create mode 100644 docs/specs/migration.schema
docs/specs/migration.schema new file +113 -0
new file mode 100644
index 0000000..d433127
@@ -0,0 +1,113 @@
+-- Copyright 2014 Red Hat, Inc. and/or its affiliates
+--
+-- Authors:
+--   Dave Gilbert <[email protected]>
+--
+-- This work is licensed under the terms of the GNU LGPL, version 2 or later.
+-- See the COPYING.LIB file in the top-level directory.
+
+Qemu {}
+DEFINITIONS IMPLICIT TAGS ::=
+BEGIN
+
+-- Some basic types used in multiple places --
+QemuString ::= UTF8String (SIZE (1..255))
+
+-- TODO: 4096 is actually page size whatever that is
+FullPage ::= OCTET STRING (SIZE (4096))
+
+-- Types for specific devices --
+
+VMState-CPU-Common ::= [ APPLICATION 20000 ] SEQUENCE {
+  halted            INTEGER,
+  interrupt-request INTEGER
+}
+
+--------------------------------
+
+RAMBlockID ::= SEQUENCE {
+  name   QemuString,
+  len    INTEGER
+}
+
+RAMSecEntry ::= [ APPLICATION 8914 ] SEQUENCE {
+  addr         INTEGER,   -- Address or offset or size
+  flags        INTEGER,   -- maybe more explicit type?
+  name         QemuString OPTIONAL,
+
+  body         CHOICE {
+    bl         SEQUENCE OF RAMBlockID,
+    compr      INTEGER (0..255), -- Page filled with this value
+    page       FullPage
+    -- TODO xbzrle --
+  }
+}
+
+RAMSecList ::= [ APPLICATION 9810 ] SEQUENCE OF RAMSecEntry
+
+SubSection ::= [ APPLICATION 10707 ] SEQUENCE {
+  name         QemuString,
+  versionid    INTEGER,
+
+  contents     SEQUENCE OF VMStateEntries
+}
+
+SubSecList ::= [ APPLICATION 10700 ] SEQUENCE OF SubSection
+
+VMStateEntries ::= CHOICE {
+  -- Hmm need to think more --
+  array       SEQUENCE OF VMStateEntries,
+  bool        BOOLEAN,
+  int         INTEGER,
+  oldblob     OCTET STRING,
+  subsecl     SubSecList
+}
+
+VMState ::= CHOICE {
+  SEQUENCE OF VMStateEntries,
+
+  VMState-CPU-Common
+}
+
+-- Restrict to unsigned?
+SectionID ::= INTEGER
+
+SecFull ::= [ APPLICATION 2003 ] SEQUENCE {
+  name         QemuString,
+  sectionid    SectionID,
+  instanceid   INTEGER,
+  versionid    INTEGER,
+
+  contents     CHOICE {
+    ramsec     RAMSecList,
+    -- TODO other iterator initial stuff --
+    vmstate    VMState,
+    oldblob  OCTET STRING
+  }
+}
+
+SecMin ::= [ APPLICATION 211 ] SEQUENCE {
+  sectionid   SectionID,
+
+  contents     CHOICE {
+    ramsec     RAMSecList
+    -- TODO other iterator general/end stuff --
+  }
+}
+
+Sections ::= CHOICE {
+  full    SecFull,
+  min     SecMin
+}
+
+-- The whole file --
+-- Application tag used to get first 32bits of file
+-- to come out as 7f cd c5 51  - the 51 is Q
+-- the c5 and cd being E,M but with the top bit set
+-- which BER requires
+QemuFile ::= [ APPLICATION 1270481 ] SEQUENCE {
+  version INTEGER (3),
+  top     SEQUENCE OF Sections
+}
+
+END
Signature · 1 line
1.8.5.3
#21 lore raw

RFC PATCH 16/16 Wire in BER visitors

In reply to Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

BER output visitor to be selected with env (as other output visitors) BER input visitor recognized by file header

exec.c: Set cpu-common to use it's own type tag (as example)

---
 exec.c                        |  2 ++
 include/migration/migration.h |  2 ++
 savevm.c                      | 76 +++++++++++++++++++++++++++++++------------
 3 files changed, 60 insertions(+), 20 deletions(-)
exec.c +2 -0
index 91513c6..e2f3907 100644
@@ -23,6 +23,7 @@
 #endif
 
 #include "qemu-common.h"
+#include "qapi/ber.h"
 #include "cpu.h"
 #include "tcg.h"
 #include "hw/hw.h"
@@ -427,6 +428,7 @@ static int cpu_common_post_load(void *opaque, int version_id)
 
 const VMStateDescription vmstate_cpu_common = {
     .name = "cpu_common",
+    .ber_tag = BER_TYPE_QEMU_VMSTATE_CPU_COMMON,
     .version_id = 1,
     .minimum_version_id = 1,
     .minimum_version_id_old = 1,
include/migration/migration.h +2 -0
index 1987e79..7ddb934 100644
@@ -27,6 +27,8 @@
 #define QEMU_VM_FILE_VERSION_COMPAT  0x00000002
 #define QEMU_VM_FILE_VERSION         0x00000003
 
+#define QEMU_VM_BER_FILE_MAGIC       0x7fcdc551
+
 #define QEMU_VM_EOF                  0x00
 #define QEMU_VM_SECTION_START        0x01
 #define QEMU_VM_SECTION_PART         0x02
savevm.c +56 -20

128 lines not on this page · raw message

Signature · 1 line
1.8.5.3
#22 lore raw
In reply to Dr. David Alan Gilbert (git)
Quoted 11 lines · Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

This is a work in progress cut of my visitor+BER format migration world; there's lots to do but it's starting to get there. I'd appreciate if anyone who has more experience with either the visitor code, or ASN.1/BER in general has a look to see if I'm doing anything particularly odd.

I've cherry picked bits of Michael Roth's visitor set from 2011 and Stefan Berger's BER code from this time last year, but not necessarily used things in the same way.

Nice! Will take a look at patches later, just responding to cover letter now:

Quoted 23 lines

The good:
It can perform a migration using a BER format file that's described
and verifiable against a schema.

It can also perform a migration using the old binary format.

A lot of the nasty detail of the old format is hidden in the compatibility visitor (e.g. the subsection end detection).

The ugly:
1) The encoding for zero pages is bloated, it can be simplified a lot (see previous discussion with Michael from a week or two back that I've not yet implemented)
2) There are a few shims that get passed visitors but need files or the other way that should be removable. The shim to get from a qemu file back to a visitor is patch 3 and marked as 'tmp' and the main uses are in piix4, pci.c, spapr_vscsi.c.
3) There are places where the visitor interface is too tied to the old file format, but most of those can be removed with effort.
4) I've made extensions to the Visitor type, many of these are very migration specific; and use things like get_next_type and the list visitors in quite a different way from the current uses. Should I really have a separate type?

Quoted 8 lines

(see patch 1)
5) At the moment you select BER output format by setting an environment variable ( export QEMUMIGFORMAT=BER ) , I need to put more thought in to the right way to do this, there are some harder questions like what happens to devices that are still using pre-vmstate encodings (that are currently sent as blobs) when they eventually convert over and thus how to keep compatibility with earlier BER output versions where they were blobs.

A simple way is the way we always changed migration format - trying this to the machine type.
Old machine type gets you binary, newer one BER with blobs, then we can add more flags gradually.

With BER we'll actually be able to do smarter things in the future, such as send out multiple formats and have destination select the one it understands, but this isn't mandatory as a first step.

Quoted 4 lines

6) At the moment for blobs (i.e. pre-vmstate) the whole blob gets loaded into memory and then processed; that's not necessary and can be done with a QEMUFile shim.
7) I need to sort out my Error** handling (probably in a lot of places)

That's mostly broken in migration as it is.

Quoted 8 lines

8) The visitors should be able to share much more code.

The not-yet-done:
a) XBZRLE
b) block-migration
c) SPAPR (it's an iterative migration like RAM and block)
d) RDMA (I'm guessing I've broken it, but haven't tried)
e) Floats are currently sent as blobs.

So at least a,b and c need to be done.
Not sure about RDMA - it was never supported and no one seems to work on it.
Maybe, somehow, it can use the old format when enabled?

Quoted 7 lines

What I've done:
There are separate output visitors (binary compatible, debug and BER) and
they're selected by setting QEMUMIGFORMAT to one of 'debug', 'BER' or just leaving it unset.

The Input visitor is selected automatically from the first 4 bytes of the
file.

Not really necessary if you use machine type.

Quoted 38 lines

In general most types are BER sequences of indefinite length, with some
types that I've allocated an Application specific type tag to. There is a hook to give any VMState it's own type tag (which I've shown using one example for cpu-common). Integers and strings are standard 'universal' types.

Objects with .get/.put methods or register_savevm are saved as an 'octet string'.
There are a few places where a device registered with .get/.put calls back into vmstate_save_state for part of their state (typically for devices built on PCI) and when they do that, even when in BER mode, those components get stored inside the octet string in the old format.

I've used the 'asn1c' tool to validate the schema (which is
in docs/specs/migration.schema) and also to verify streams that I've produced match the schema.

I've tested it with virt-test (hacked to have different source/dest qemu's)
and tried bin-bin, ber-ber, and pre-visitor-qemu -> this and
this -> pre-visitor-qemu just on the standard migration.default.tcp migration test, but I've not tried a large combination of configurations.

My fix for qemu_peek_buffer that's on the list is needed to stabilise
the binary format input visitor.

I'll keep chipping away at that list, and would expect to pop another
version out in a month or so.

Dave

Dr. David Alan Gilbert (16):
Visitor: Add methods for migration format use
QEMUSizedBuffer/QEMUFile
qemu-file: Add set/get tmp_visitor
Header/constant/types fixes for visitors
Visitor: Binary compatible output visitor
Visitor: Debug output visitor
Visitor: Binary compatible input visitor
Visitor: Output path

Quoted 8 lines

Visitor: Load path
Visitor: Common types to use visitors
Choose output visitor based on env variable
BER Visitor: Create output visitor
BER Visitor: Create input visitor
Start some BER format docs
ASN.1 schema for new migration format
Wire in BER visitors

One nifty feature of Stefan's patches was that a
set of unit tests for parser was included.
Worth reusing?

Quoted 47 lines
 arch_init.c                                    |  231 +++--
 block-migration.c                              |   13 +-
 docs/migration.txt                             |   34 +-
 docs/specs/migration.schema                    |  113 +++
 exec.c                                         |    2 +
 hw/acpi/piix4.c                                |    5 +-
 hw/pci/pci.c                                   |    5 +-
 hw/ppc/spapr.c                                 |    9 +-
 hw/scsi/spapr_vscsi.c                          |    6 +-
 include/migration/migration.h                  |   17 +
 include/migration/qemu-file.h                  |   31 +
 include/migration/vmstate.h                    |   28 +-
 include/qapi/ber.h                             |  108 +++
 include/qapi/qemu-file-ber-input-visitor.h     |   26 +
 include/qapi/qemu-file-ber-output-visitor.h    |   26 +
 include/qapi/qemu-file-binary-input-visitor.h  |   27 +
 include/qapi/qemu-file-binary-output-visitor.h |   26 +
 include/qapi/qemu-file-debug-output-visitor.h  |   26 +
 include/qapi/visitor-impl.h                    |   23 +
 include/qapi/visitor.h                         |   51 ++
 include/qemu/typedefs.h                        |    4 +-
 qapi/Makefile.objs                             |    4 +-
 qapi/qapi-visit-core.c                         |   80 ++
 qapi/qemu-file-ber-input-visitor.c             | 1163 ++++++++++++++++++++++++
 qapi/qemu-file-ber-output-visitor.c            |  916 +++++++++++++++++++
 qapi/qemu-file-binary-input-visitor.c          |  688 ++++++++++++++
 qapi/qemu-file-binary-output-visitor.c         |  564 ++++++++++++
 qapi/qemu-file-debug-output-visitor.c          |  471 ++++++++++
 qemu-file.c                                    |  423 +++++++++
 savevm.c                                       |  412 +++++++--
 vmstate.c                                      |  619 ++++++-------
 31 files changed, 5593 insertions(+), 558 deletions(-)
 create mode 100644 docs/specs/migration.schema
 create mode 100644 include/qapi/ber.h
 create mode 100644 include/qapi/qemu-file-ber-input-visitor.h
 create mode 100644 include/qapi/qemu-file-ber-output-visitor.h
 create mode 100644 include/qapi/qemu-file-binary-input-visitor.h
 create mode 100644 include/qapi/qemu-file-binary-output-visitor.h
 create mode 100644 include/qapi/qemu-file-debug-output-visitor.h
 create mode 100644 qapi/qemu-file-ber-input-visitor.c
 create mode 100644 qapi/qemu-file-ber-output-visitor.c
 create mode 100644 qapi/qemu-file-binary-input-visitor.c
 create mode 100644 qapi/qemu-file-binary-output-visitor.c
 create mode 100644 qapi/qemu-file-debug-output-visitor.c
Signature · 1 line
1.8.5.3
#23 lore raw
Quoted 1 line · * Michael S. Tsirkin ([email protected]) wrote:

On Tue, Mar 25, 2014 at 08:17:11PM +0000, Dr. David Alan Gilbert (git) wrote:

<snip>

Quoted 8 lines
Quoted 7 lines

5) At the moment you select BER output format by setting an environment variable ( export QEMUMIGFORMAT=BER ) , I need to put more thought in to the right way to do this, there are some harder questions like what happens to devices that are still using pre-vmstate encodings (that are currently sent as blobs) when they eventually convert over and thus how to keep compatibility with earlier BER output versions where they were blobs.

Quoted 4 lines

A simple way is the way we always changed migration format - trying this to the machine type.
Old machine type gets you binary, newer one BER with blobs, then we can add more flags gradually.

Right, that should work; it gets a little messy to tie machine type to formats for individual devices, but no big issue.

Quoted 13 lines

With BER we'll actually be able to do smarter things in the future, such as send out multiple formats and have destination select the one it understands, but this isn't mandatory as a first step.

Quoted 4 lines

6) At the moment for blobs (i.e. pre-vmstate) the whole blob gets loaded into memory and then processed; that's not necessary and can be done with a QEMUFile shim.
7) I need to sort out my Error** handling (probably in a lot of places)

That's mostly broken in migration as it is.

Quoted 2 lines

8) The visitors should be able to share much more code.

Quoted 8 lines
Quoted 6 lines

The not-yet-done:
a) XBZRLE
b) block-migration
c) SPAPR (it's an iterative migration like RAM and block)
d) RDMA (I'm guessing I've broken it, but haven't tried)
e) Floats are currently sent as blobs.

So at least a,b and c need to be done.

Yep, agreed; iterative migration stuff has direct byte/word putting all over so is rather manual, so I just did enough to get it going first.

Quoted 3 lines

Not sure about RDMA - it was never supported and no one seems to work on it.
Maybe, somehow, it can use the old format when enabled?

I think Michael still throws bug fixes in from time to time, however my worry is actually more about the Visitor conversion than the actual format change that might break it; anyway needs testing and then we'll see.

Quoted 9 lines
Quoted 7 lines

What I've done:
There are separate output visitors (binary compatible, debug and BER) and
they're selected by setting QEMUMIGFORMAT to one of 'debug', 'BER' or just leaving it unset.

The Input visitor is selected automatically from the first 4 bytes of the
file.

Not really necessary if you use machine type.

It's easy anyway.

Quoted 46 lines
Quoted 37 lines

In general most types are BER sequences of indefinite length, with some
types that I've allocated an Application specific type tag to. There is a hook to give any VMState it's own type tag (which I've shown using one example for cpu-common). Integers and strings are standard 'universal' types.

Objects with .get/.put methods or register_savevm are saved as an 'octet string'.
There are a few places where a device registered with .get/.put calls back into vmstate_save_state for part of their state (typically for devices built on PCI) and when they do that, even when in BER mode, those components get stored inside the octet string in the old format.

I've used the 'asn1c' tool to validate the schema (which is
in docs/specs/migration.schema) and also to verify streams that I've produced match the schema.

I've tested it with virt-test (hacked to have different source/dest qemu's)
and tried bin-bin, ber-ber, and pre-visitor-qemu -> this and
this -> pre-visitor-qemu just on the standard migration.default.tcp migration test, but I've not tried a large combination of configurations.

My fix for qemu_peek_buffer that's on the list is needed to stabilise
the binary format input visitor.

I'll keep chipping away at that list, and would expect to pop another
version out in a month or so.

Dave

Dr. David Alan Gilbert (16):
Visitor: Add methods for migration format use
QEMUSizedBuffer/QEMUFile
qemu-file: Add set/get tmp_visitor
Header/constant/types fixes for visitors
Visitor: Binary compatible output visitor
Visitor: Debug output visitor
Visitor: Binary compatible input visitor
Visitor: Output path

Quoted 8 lines

Visitor: Load path
Visitor: Common types to use visitors
Choose output visitor based on env variable
BER Visitor: Create output visitor
BER Visitor: Create input visitor
Start some BER format docs
ASN.1 schema for new migration format
Wire in BER visitors

Quoted 3 lines

One nifty feature of Stefan's patches was that a
set of unit tests for parser was included.
Worth reusing?

Yes, unit tests are one thing I missed off my list but know need doing.

Dave
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK