New Migration Protocol using Visitor Interface
OVERVIEW
This patch series implements a QEMUFile Visitor class that's intended to abstract away direct calls to qemu_put_*/qemu_get_* for save/load functions. Currently this is done by always creating a QEMUFileInputVisitor/QEMUFileOutputVisitor pair with each call to qemu_fopen_ops() and maintaining a QEMUFile->I/O Visitor mapping. save/load functions that are to be converted would them simply use lookup routines to get a Visitor based on their QEMUFile arguments. Once these save/load functions are all converted, we can then change the interfaces in bulk and switch over to passing in the Visitor directly.
An example conversion of Slirp, which still uses old-style save/load routines, is included as part of this series. Anthony I believe has VMState converted over to using Visitors, so theoretically all VMStatified devices are good to go (Anthony: if that's the case feel free to rebase on this or point me to the repo to pull in, and I'll work off the resulting branch).
PLANS
Ultimately, the goal is to implement a new migration protocol that is self-describing, such that incompatibilities or migration errors can be more easilly detected. Currently, a simple change in data types for a particular device can introduce subtle bugs that won't be detected by the target, since the target interprets the data according to it's own expectation of what those data types are. Currently the plan is to use ASN.1 BER in place of QEMUFile.
By using a Visitor interface for migration, we also gain the ability to generate a migration schema (via, say, a JSONSchemaVisitor). This is similar to the VMState schema generator in Anthony's "Add live migration unit tests" series (http://thread.gmane.org/gmane.comp.emulators.qemu/97754/focus=97754), except that it is applicable to both VMState and old-style save/load functions.
There is still quite a bit a work to get to that point. Anthony addressed some of the issues on the VMState side of things in the aforementioned series, and I believe he already has some patches that convert VMState over to using visitors insteads of qemu_put_*/qemu_get_* directly. That being the case, what's left is:
1) Convert old-style save/load functions over to using visitors. I'm not sure what the best approach is for this. We basically have 2 options: either by converting them to VMState, or converting the functions over to using visitors, such as with the example slirp conversion. Even if we do option 2 it would likely need to be done in 2 phases:
phase 1: a mechanical conversion where we basically replace each qemu_put*/qemu_get* with a visit_*. This allows use to plop in a new, say BERVisitor to switch to using the new migration protocol. It should also be possible to do this without breaking migration compatibility with older versions. It does not however result in a well-specified schema if we were to plop in, say, a JSONSchemaVisitor. This is where we need phase 2.
phase 2: Currently, runtime state modifies our "schema" the way things are currently done. Slirp instance and virtqueue enumeration, for instance:
slirp/slirp.c:
for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
if (ex_ptr->ex_pty == 3) {
struct socket *so;
so = slirp_find_ctl_socket(slirp, ex_ptr->ex_addr,
ntohs(ex_ptr->ex_fport));
if (!so)
continue;
qemu_put_byte(f, 42);
slirp_socket_save(f, so);
}hw/virtio.c:
for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
if (vdev->vq[i].vring.num == 0)
break;
qemu_put_be32(f, vdev->vq[i].vring.num);
qemu_put_be64(f, vdev->vq[i].pa);
qemu_put_be16s(f, &vdev->vq[i].last_avail_idx);
if (vdev->binding->save_queue)
vdev->binding->save_queue(vdev->binding_opaque, i, f);
}To be able to build a schema around this we'd need to do a visit_start_array() or visit_start_list(), to denote a list of entries, and ideally we'd also describe structure of these entries. But often there is no structure of these entries...they may just be values taken from various data structures. So, either we do hacky things like using visitor intefaces to create "fake" structs (we say we're dealing with a struct when we're really not), or we actually put these into an intermediate struct which we then use to assign to store/load migration fields from.
phase 2 sounds a whole lot like converting over to VMState. So I think we'll only want to go with option 2 in places where converting to VMState is not practical/planned. Or we can hold off on the phase 2 stuff and just focus on getting the new protocol in place ASAP. After which, we can rework things to support generating a well-specified schema.
2) Once the conversion stuff is done, we can modify the save/load interfaces in bulk to accept a Visitor in place of a QEMUFile (but continue using the old protocol/QEMUFile*Visitor for the time being)
3) Implement migration capabilities negotiation. This was discussed to some extent in the XBZRLE v4 thread (http://lists.gnu.org/archive/html/qemu-devel/2011-08/msg01155.html), basically having a set of migration capabilities that can be queried via QMP. Management tools would then choose the optimal migration settings from the intersection of these.
4) Implement the BERVisitor and make this the default migration protocol.
Most of the work will be in 1), though with the implementation in this series we should be able to do it incrementally. I'm not sure if the best approach is doing the mechanical phase 1 conversion, then doing phase 2 sometime after 4), doing phase 1 + 2 as part of 1), or just doing VMState conversions which gives basically the same capabilities as phase 1 + 2.
Thoughts?
Makefile | 4 +- Makefile.objs | 19 +- cutils.c | 8 + hw/hw.h | 20 ++ qapi/qapi-visit-core.c | 78 ++++++ qapi/qapi-visit-core.h | 30 ++ qapi/qemu-file-input-visitor.c | 350 +++++++++++++++++++++++ qapi/qemu-file-input-visitor.h | 26 ++ qapi/qemu-file-output-visitor.c | 353 +++++++++++++++++++++++ qapi/qemu-file-output-visitor.h | 26 ++ qemu-common.h | 1 + qemu-file.c | 585 +++++++++++++++++++++++++++++++++++++++ savevm.c | 494 --------------------------------- slirp/slirp.c | 366 ++++++++++++++---------- test-visitor.c | 404 +++++++++++++++++++++++++++ 15 files changed, 2110 insertions(+), 654 deletions(-)
RFC 1/8 qapi: add Visitor interfaces for uint*_t and int*_t
--- qapi/qapi-visit-core.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ qapi/qapi-visit-core.h | 30 ++++++++++++++++++ 2 files changed, 108 insertions(+), 0 deletions(-)
qapi/qapi-visit-core.c +78 -0
@@ -66,6 +66,28 @@ 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) +{ +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) {
@@ -96,6 +118,62 @@ void visit_type_int(Visitor *v, int64_t *obj, const char *name, Error **errp) } } +void visit_type_uint8_t (Visitor *v,uint8_t *obj,const char *name,Error **errp) +{ +if (!error_is_set (errp)) { + v->type_uint8_t (v, obj, name, errp); + } +} + +void visit_type_uint16_t (Visitor *v,uint16_t *obj,const char *name,Error **errp) +{ +if (!error_is_set (errp)) { + v->type_uint16_t (v, obj, name, errp); + } +} + +void visit_type_uint32_t (Visitor *v,uint32_t *obj,const char *name,Error **errp) +{ +if (!error_is_set (errp)) { + v->type_uint32_t (v, obj, name, errp); + } +} + +void visit_type_uint64_t (Visitor *v,uint64_t *obj,const char *name,Error **errp) +{ +if (!error_is_set (errp)) { + v->type_uint64_t (v, obj, name, errp); + } +} + +void visit_type_int8_t (Visitor *v,int8_t *obj,const char *name,Error **errp) +{ +if (!error_is_set (errp)) { + v->type_int8_t (v, obj, name, errp); + } +} + +void visit_type_int16_t (Visitor *v,int16_t *obj,const char *name,Error **errp) +{ +if (!error_is_set (errp)) { + v->type_int16_t (v, obj, name, errp); + } +} + +void visit_type_int32_t (Visitor *v,int32_t *obj,const char *name,Error **errp) +{ +if (!error_is_set (errp)) { + v->type_int32_t (v, obj, name, errp); + } +} + +void visit_type_int64_t (Visitor *v,int64_t *obj,const char *name,Error **errp) +{ +if (!error_is_set (errp)) { + v->type_int64_t (v, obj, name, errp); + } +} +void visit_type_bool (Visitor *v,bool *obj,const char *name,Error **errp) {if (!error_is_set (errp)) {
qapi/qapi-visit-core.h +30 -0
@@ -22,6 +22,11 @@ typedef struct GenericListstruct GenericList *next; } GenericList; +typedef struct GenericItem +{ +void *value; +}GenericItem ; +typedef struct Visitor Visitor ;struct Visitor
@@ -35,10 +40,23 @@ struct Visitor 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);void (*type_int)(Visitor *v,int64_t *obj,const char *name,Error **errp); +void (*type_uint8_t)(Visitor *v,uint8_t *obj,const char *name,Error **errp); +void (*type_uint16_t)(Visitor *v,uint16_t *obj,const char *name,Error **errp); +void (*type_uint32_t)(Visitor *v,uint32_t *obj,const char *name,Error **errp); +void (*type_uint64_t)(Visitor *v,uint64_t *obj,const char *name,Error **errp); +void (*type_int8_t)(Visitor *v,int8_t *obj,const char *name,Error **errp); +void (*type_int16_t)(Visitor *v,int16_t *obj,const char *name,Error **errp); +void (*type_int32_t)(Visitor *v,int32_t *obj,const char *name,Error **errp); +void (*type_int64_t)(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_number)(Visitor *v, double *obj, const char *name,
@@ -63,12 +81,24 @@ void visit_end_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,Error **errp);void visit_end_optional (Visitor *v,Error **errp);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); +void visit_type_uint8_t (Visitor *v,uint8_t *obj,const char *name,Error **errp); +void visit_type_uint16_t (Visitor *v,uint16_t *obj,const char *name,Error **errp); +void visit_type_uint32_t (Visitor *v,uint32_t *obj,const char *name,Error **errp); +void visit_type_uint64_t (Visitor *v,uint64_t *obj,const char *name,Error **errp); +void visit_type_int8_t (Visitor *v,int8_t *obj,const char *name,Error **errp); +void visit_type_int16_t (Visitor *v,int16_t *obj,const char *name,Error **errp); +void visit_type_int32_t (Visitor *v,int32_t *obj,const char *name,Error **errp); +void visit_type_int64_t (Visitor *v,int64_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);void visit_type_number (Visitor *v,double *obj,const char *name,Error **errp);
Signature · 1 line
1.7.0.4
RFC 2/8 qapi: add QemuFileOutputVisitor
In reply to Michael RothVisitor interface to write values to a QEMUFile.
--- Makefile.objs | 16 +- qapi/qemu-file-output-visitor.c | 353 +++++++++++++++++++++++++++++++++++++++ qapi/qemu-file-output-visitor.h | 26 +++ 3 files changed, 388 insertions(+), 7 deletions(-) create mode 100644 qapi/qemu-file-output-visitor.c create mode 100644 qapi/qemu-file-output-visitor.h
Makefile.objs +9 -7
@@ -20,6 +20,13 @@ coroutine-obj-$(CONFIG_POSIX) += coroutine-gthread.o endif coroutine-obj-$(CONFIG_WIN32) += coroutine-win32.o +###################################################################### +# qapi + +qapi-nested-y = qapi-visit-core.o qmp-input-visitor.o qmp-output-visitor.o qapi-dealloc-visitor.o +qapi-nested-y += qmp-registry.o qmp-dispatch.o +qapi-obj-y = $(addprefix qapi/, $(qapi-nested-y)) + ####################################################################### # block-obj-y is code used by both qemu system emulation and qemu-img
@@ -73,6 +80,8 @@ fsdev-obj-$(CONFIG_VIRTFS) += $(addprefix fsdev/, $(fsdev-nested-y)) # CPUs and machines. common-obj-y = $(block-obj-y) blockdev.o +common-obj-y += $(qapi-obj-y) +common-obj-y += qapi/qemu-file-output-visitor.o common-obj-y += $(net-obj-y) common-obj-y += $(qobject-obj-y) common-obj-$(CONFIG_LINUX) += $(fsdev-obj-$(CONFIG_LINUX))
@@ -399,13 +408,6 @@ $(trace-obj-y): $(GENERATED_HEADERS) libcacard-y = cac.o event.o vcard.o vreader.o vcard_emul_nss.o vcard_emul_type.o card_7816.o ###################################################################### -# qapi - -qapi-nested-y = qapi-visit-core.o qmp-input-visitor.o qmp-output-visitor.o qapi-dealloc-visitor.o -qapi-nested-y += qmp-registry.o qmp-dispatch.o -qapi-obj-y = $(addprefix qapi/, $(qapi-nested-y)) - -###################################################################### # guest agent qga-nested-y = guest-agent-commands.o guest-agent-command-state.o
qapi/qemu-file-output-visitor.c new file +353 -0
353 lines not on this page · raw message
qapi/qemu-file-output-visitor.h new file +26 -0
@@ -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_OUTPUT_VISITOR_H +#define QEMU_FILE_OUTPUT_VISITOR_H + +#include "qapi-visit-core.h" + +typedef struct QemuFileOutputVisitor QemuFileOutputVisitor ; + +QemuFileOutputVisitor *qemu_file_output_visitor_new (QEMUFile *f); +void qemu_file_output_visitor_cleanup (QemuFileOutputVisitor *d); + +Visitor *qemu_file_output_get_visitor (QemuFileOutputVisitor *v); + +#endif
Signature · 1 line
1.7.0.4
RFC 4/8 savevm: move QEMUFile interfaces into qemu-file.c
In reply to Michael Roth--- Makefile.objs | 2 +- hw/hw.h | 1 + qemu-file.c | 521 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ savevm.c | 494 ------------------------------------------------------ 4 files changed, 523 insertions(+), 495 deletions(-) create mode 100644 qemu-file.c
Makefile.objs +1 -1
@@ -119,7 +119,7 @@ common-obj-$(CONFIG_SD) += sd.o common-obj-y += bt.o bt-host.o bt-vhci.o bt-l2cap.o bt-sdp.o bt-hci.o bt-hid.o usb-bt.o common-obj-y += bt-hci-csr.o common-obj-y += buffered_file.o migration.o migration-tcp.o -common-obj-y += qemu-char.o savevm.o #aio.o +common-obj-y += qemu-char.o savevm.o qemu-file.o #aio.o common-obj-y += msmouse.o ps2.o common-obj-y += qdev.o qdev-properties.o common-obj-y += block-migration.o iohandler.o
hw/hw.h +1 -0
@@ -58,6 +58,7 @@ void qemu_fflush(QEMUFile *f);int qemu_fclose (QEMUFile *f);void qemu_put_buffer (QEMUFile *f,const uint8_t *buf,int size);void qemu_put_byte (QEMUFile *f,int v); +int qemu_peek_byte (QEMUFile *f);static inline void qemu_put_ubyte (QEMUFile *f,unsigned int v) {
qemu-file.c new file +521 -0
521 lines not on this page · raw message
savevm.c +0 -494
506 lines not on this page · raw message
Signature · 1 line
1.7.0.4
RFC 6/8 savevm: add QEMUFile->visitor lookup routines
In reply to Michael Roth--- hw/hw.h | 19 +++++++++++++++++ qemu-file.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 0 deletions(-)
hw/hw.h +19 -0
@@ -10,6 +10,25 @@#include "ioport.h" #include "irq.h" +#include "qemu-queue.h" +#include "qapi/qapi-visit-core.h" +#include "qapi/qemu-file-output-visitor.h" +#include "qapi/qemu-file-input-visitor.h" + +/* QEMUFile->Visitor lookup routines to support legacy qemu_put_* calls */ +typedef struct QemuFileVisitorNode { +void *opaque;/* Visitor type */ +QemuFileOutputVisitor *ov; +QemuFileInputVisitor *iv; +QEMUFile *f; +QTAILQ_ENTRY (QemuFileVisitorNode) entry; +}QemuFileVisitorNode ; + +Visitor *qemu_file_get_input_visitor (QEMUFile *f); +Visitor *qemu_file_get_output_visitor (QEMUFile *f); +QemuFileVisitorNode *qemu_file_get_visitor_node (QEMUFile *f); +void qemu_file_put_visitor_node (QemuFileVisitorNode *n); +void qemu_file_remove_visitor_node (QemuFileVisitorNode *n);/* VM Load/Save */
qemu-file.c +64 -0
88 lines not on this page · raw message
Signature · 1 line
1.7.0.4
RFC 5/8 qapi: test cases for QEMUFile input/output visitors
In reply to Michael Roth--- Makefile | 4 +- test-visitor.c | 404 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 406 insertions(+), 2 deletions(-)
Makefile +2 -2
@@ -155,7 +155,7 @@ check-qint.o check-qstring.o check-qdict.o check-qlist.o check-qfloat.o check-qj CHECK_PROG_DEPS = $(oslib-obj-y) $(trace-obj-y) qemu-tool.o -check-qint: check-qint.o qint.o $(CHECK_PROG_DEPS) +CHECK-QINt: check-qint.o qint.o $(CHECK_PROG_DEPS) check-qstring: check-qstring.o qstring.o $(CHECK_PROG_DEPS) check-qdict: check-qdict.o qdict.o qfloat.o qint.o qstring.o qbool.o qlist.o $(CHECK_PROG_DEPS) check-qlist: check-qlist.o qlist.o qint.o $(CHECK_PROG_DEPS)
@@ -188,7 +188,7 @@ $(qapi-dir)/qga-qmp-marshal.c: $(SRC_PATH)/qapi-schema-guest.json $(SRC_PATH)/sc $(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-commands.py -o "$(qapi-dir)" -p "qga-" < $<, " GEN $@") test-visitor.o: $(addprefix $(qapi-dir)/, test-qapi-types.c test-qapi-types.h test-qapi-visit.c test-qapi-visit.h) $(qapi-obj-y) -test-visitor: test-visitor.o qfloat.o qint.o qdict.o qstring.o qlist.o qbool.o $(qapi-obj-y) error.o osdep.o $(oslib-obj-y) qjson.o json-streamer.o json-lexer.o json-parser.o qerror.o qemu-error.o qemu-tool.o $(qapi-dir)/test-qapi-visit.o $(qapi-dir)/test-qapi-types.o +test-visitor: test-visitor.o qfloat.o qint.o qdict.o qstring.o qlist.o qbool.o $(qapi-obj-y) error.o osdep.o $(oslib-obj-y) qjson.o json-streamer.o json-lexer.o json-parser.o qerror.o qemu-error.o qemu-tool.o $(qapi-dir)/test-qapi-visit.o $(qapi-dir)/test-qapi-types.o qapi/qemu-file-input-visitor.o qapi/qemu-file-output-visitor.o qemu-file.o test-qmp-commands.o: $(addprefix $(qapi-dir)/, test-qapi-types.c test-qapi-types.h test-qapi-visit.c test-qapi-visit.h test-qmp-marshal.c test-qmp-commands.h) $(qapi-obj-y) test-qmp-commands: test-qmp-commands.o qfloat.o qint.o qdict.o qstring.o qlist.o qbool.o $(qapi-obj-y) error.o osdep.o $(oslib-obj-y) qjson.o json-streamer.o json-lexer.o json-parser.o qerror.o qemu-error.o qemu-tool.o $(qapi-dir)/test-qapi-visit.o $(qapi-dir)/test-qapi-types.o $(qapi-dir)/test-qmp-marshal.o module.o
test-visitor.c +404 -0
422 lines not on this page · raw message
Signature · 1 line
1.7.0.4
RFC 7/8 cutil: add strocat(), to concat a string to an offset in another
In reply to Michael Roth--- cutils.c | 8 ++++++++ qemu-common.h | 1 + 2 files changed, 9 insertions(+), 0 deletions(-)
cutils.c +8 -0
@@ -84,6 +84,14 @@ int stristart(const char *str, const char *val, const char **ptr)return 1 ; } +/* concat first 'offset' chars in 'dest' with 'src' */ +char *strocat (char *buf,const char *src,int offset) +{ +memcpy (&buf[offset], src,strlen (src)); + buf[offset+strlen (src)] ='\0' ; +return buf; +} +/* XXX: use host strnlen if available ? */ int qemu_strnlen (const char *s,int max_len) {
qemu-common.h +1 -0
@@ -138,6 +138,7 @@ void pstrcpy(char *buf, int buf_size, const char *str);char *pstrcat (char *buf,int buf_size,const char *s);int strstart (const char *str,const char *val,const char **ptr);int stristart (const char *str,const char *val,const char **ptr); +char *strocat (char *dest,const char *src,int offset);int qemu_strnlen (const char *s,int max_len);time_t mktimegm (struct tm *tm);int qemu_fls (int i);
Signature · 1 line
1.7.0.4
RFC 3/8 qapi: add QemuFileInputVisitor
In reply to Michael RothVisitor interfaces to read values from a QEMUFile
--- Makefile.objs | 1 + qapi/qemu-file-input-visitor.c | 350 ++++++++++++++++++++++++++++++++++++++++ qapi/qemu-file-input-visitor.h | 26 +++ 3 files changed, 377 insertions(+), 0 deletions(-) create mode 100644 qapi/qemu-file-input-visitor.c create mode 100644 qapi/qemu-file-input-visitor.h
Makefile.objs +1 -0
@@ -82,6 +82,7 @@ fsdev-obj-$(CONFIG_VIRTFS) += $(addprefix fsdev/, $(fsdev-nested-y)) common-obj-y = $(block-obj-y) blockdev.o common-obj-y += $(qapi-obj-y) common-obj-y += qapi/qemu-file-output-visitor.o +common-obj-y += qapi/qemu-file-input-visitor.o common-obj-y += $(net-obj-y) common-obj-y += $(qobject-obj-y) common-obj-$(CONFIG_LINUX) += $(fsdev-obj-$(CONFIG_LINUX))
qapi/qemu-file-input-visitor.c new file +350 -0
350 lines not on this page · raw message
qapi/qemu-file-input-visitor.h new file +26 -0
@@ -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_INPUT_VISITOR_H +#define QEMU_FILE_INPUT_VISITOR_H + +#include "qapi-visit-core.h" + +typedef struct QemuFileInputVisitor QemuFileInputVisitor ; + +QemuFileInputVisitor *qemu_file_input_visitor_new (QEMUFile *f); +void qemu_file_input_visitor_cleanup (QemuFileInputVisitor *d); + +Visitor *qemu_file_input_get_visitor (QemuFileInputVisitor *v); + +#endif
Signature · 1 line
1.7.0.4
RFC 8/8 slirp: convert save/load function to visitor interface
In reply to Michael RothWhere possible common routines are used for both input and output, thus save on lines of code, theoretically. The added lines here are mostly due to extra logic for each save/load routine to manipulate strings into a unique field name for each saved field, and in some cases a few extra Visitor calls to due list/struct i/o. With some reworking we can probably optimize all of these to reduce the amount of added code.
--- slirp/slirp.c | 366 +++++++++++++++++++++++++++++++++----------------------- 1 files changed, 216 insertions(+), 150 deletions(-)
slirp/slirp.c +216 -150
@@ -26,6 +26,9 @@#include "qemu-char.h" #include "slirp.h" #include "hw/hw.h" +#include "qemu-error.h" + +#define SLIRP_DELIMITER 42/* used to separate slirp instances in save/load */ /* host loopback address */ struct in_addr loopback_addr;
@@ -871,96 +874,171 @@ void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr, int guest_port,tcp_output (sototcpcb (so)); } -static void slirp_tcp_save (QEMUFile *f,struct tcpcb *tp) +static void slirp_tcp_visit (Visitor *v,struct tcpcb *tp,const char *pfield,Error *err) { -int i; - -qemu_put_sbe16 (f, tp->t_state); -for (i =0 ; i <TCPT_NTIMERS ; i++) -qemu_put_sbe16 (f, tp->t_timer[i]); -qemu_put_sbe16 (f, tp->t_rxtshift); -qemu_put_sbe16 (f, tp->t_rxtcur); -qemu_put_sbe16 (f, tp->t_dupacks); -qemu_put_be16 (f, tp->t_maxseg); -qemu_put_sbyte (f, tp->t_force); -qemu_put_be16 (f, tp->t_flags); -qemu_put_be32 (f, tp->snd_una); -qemu_put_be32 (f, tp->snd_nxt); -qemu_put_be32 (f, tp->snd_up); -qemu_put_be32 (f, tp->snd_wl1); -qemu_put_be32 (f, tp->snd_wl2); -qemu_put_be32 (f, tp->iss); -qemu_put_be32 (f, tp->snd_wnd); -qemu_put_be32 (f, tp->rcv_wnd); -qemu_put_be32 (f, tp->rcv_nxt); -qemu_put_be32 (f, tp->rcv_up); -qemu_put_be32 (f, tp->irs); -qemu_put_be32 (f, tp->rcv_adv); -qemu_put_be32 (f, tp->snd_max); -qemu_put_be32 (f, tp->snd_cwnd); -qemu_put_be32 (f, tp->snd_ssthresh); -qemu_put_sbe16 (f, tp->t_idle); -qemu_put_sbe16 (f, tp->t_rtt); -qemu_put_be32 (f, tp->t_rtseq); -qemu_put_sbe16 (f, tp->t_srtt); -qemu_put_sbe16 (f, tp->t_rttvar); -qemu_put_be16 (f, tp->t_rttmin); -qemu_put_be32 (f, tp->max_sndwnd); -qemu_put_byte (f, tp->t_oobflags); -qemu_put_byte (f, tp->t_iobc); -qemu_put_sbe16 (f, tp->t_softerror); -qemu_put_byte (f, tp->snd_scale); -qemu_put_byte (f, tp->rcv_scale); -qemu_put_byte (f, tp->request_r_scale); -qemu_put_byte (f, tp->requested_s_scale); -qemu_put_be32 (f, tp->ts_recent); -qemu_put_be32 (f, tp->ts_recent_age); -qemu_put_be32 (f, tp->last_ack_sent); +char f[128 ]; +int i, l =0 ; +int16_t *ptr; + +if (pfield) { +assert (strlen (pfield) <sizeof (f)); +strcpy (f, pfield); + l =strlen (pfield); + } + +visit_type_int16_t (v, &tp->t_state,strocat (f,".t_state" , l), &err); + ptr = tp->t_timer; +visit_start_array (v, (void **)&ptr,strocat (f,".t_timer" , l),TCPT_NTIMERS ,sizeof (*ptr), &err); +for (i =0 ; i <TCPT_NTIMERS ; ++i) { +visit_type_int16_t (v, &ptr[i],NULL , &err); + } +visit_end_array (v, &err); +visit_type_int16_t (v, &tp->t_rxtshift,strocat (f,".t_rxtshift" , l), &err); +visit_type_int16_t (v, &tp->t_rxtcur,strocat (f,".t_rxtcur" , l), &err); +visit_type_int16_t (v, &tp->t_dupacks,strocat (f,".t_dupacks" , l), &err); +visit_type_uint16_t (v, &tp->t_maxseg,strocat (f,".t_maxseg" , l), &err); +visit_type_uint8_t (v, (uint8_t *)&tp->t_force,strocat (f,".t_force" , l), &err); +visit_type_uint16_t (v, &tp->t_flags,strocat (f,".t_flags" , l), &err); +visit_type_uint32_t (v, &tp->snd_una,strocat (f,".snd_una" , l), &err); +visit_type_uint32_t (v, &tp->snd_nxt,strocat (f,".snd_nxt" , l), &err); +visit_type_uint32_t (v, &tp->snd_up,strocat (f,".snd_up" , l), &err); +visit_type_uint32_t (v, &tp->snd_wl1,strocat (f,".snd_wl1" , l), &err); +visit_type_uint32_t (v, &tp->snd_wl2,strocat (f,".snd_wl2" , l), &err); +visit_type_uint32_t (v, &tp->iss,strocat (f,".iss" , l), &err); +visit_type_uint32_t (v, &tp->snd_wnd,strocat (f,".snd_wnd" , l), &err); +visit_type_uint32_t (v, &tp->rcv_wnd,strocat (f,".rcv_wnd" , l), &err); +visit_type_uint32_t (v, &tp->rcv_nxt,strocat (f,".rcv_nxt" , l), &err); +visit_type_uint32_t (v, &tp->rcv_up,strocat (f,".rcv_up" , l), &err); +visit_type_uint32_t (v, &tp->irs,strocat (f,".irs" , l), &err); +visit_type_uint32_t (v, &tp->rcv_adv,strocat (f,".rcv_adv" , l), &err); +visit_type_uint32_t (v, &tp->snd_max,strocat (f,".snd_max" , l), &err); +visit_type_uint32_t (v, &tp->snd_cwnd,strocat (f,".snd_cwnd" , l), &err); +visit_type_uint32_t (v, &tp->snd_ssthresh,strocat (f,".snd_ssthresh" , l), &err); +visit_type_int16_t (v, &tp->t_idle,strocat (f,".t_idle" , l), &err); +visit_type_int16_t (v, &tp->t_rtt,strocat (f,".t_rtt" , l), &err); +visit_type_uint32_t (v, &tp->t_rtseq,strocat (f,".t_rtseq" , l), &err); +visit_type_int16_t (v, &tp->t_srtt,strocat (f,".t_srtt" , l), &err); +visit_type_int16_t (v, &tp->t_rttvar,strocat (f,".t_rttvar" , l), &err); +visit_type_uint16_t (v, &tp->t_rttmin,strocat (f,".t_rttmin" , l), &err); +visit_type_uint32_t (v, &tp->max_sndwnd,strocat (f,".max_sndwnd" , l), &err); +visit_type_uint8_t (v, (uint8_t *)&tp->t_oobflags,strocat (f,".t_oobflags" , l), &err); +visit_type_uint8_t (v, (uint8_t *)&tp->t_iobc,strocat (f,".t_iobc" , l), &err); +visit_type_int16_t (v, &tp->t_softerror,strocat (f,".t_softerror" , l), &err); +visit_type_uint8_t (v, &tp->snd_scale,strocat (f,".snd_scale" , l), &err); +visit_type_uint8_t (v, &tp->rcv_scale,strocat (f,".rcv_scale" , l), &err); +visit_type_uint8_t (v, &tp->request_r_scale,strocat (f,".request_r_scale" , l), &err); +visit_type_uint8_t (v, &tp->requested_s_scale,strocat (f,".requested_s_scale" , l), &err); +visit_type_uint32_t (v, &tp->ts_recent,strocat (f,".ts_recent" , l), &err); +visit_type_uint32_t (v, &tp->ts_recent_age,strocat (f,".ts_recent_age" , l), &err); +visit_type_uint32_t (v, &tp->last_ack_sent,strocat (f,".last_ack_sent" , l), &err); } -static void slirp_sbuf_save (QEMUFile *f,struct sbuf *sbuf) +static void slirp_tcp_save (Visitor *v,struct tcpcb *tp,const char *pfield,Error *err) { -uint32_t off; +slirp_tcp_visit (v, tp, pfield, err); +} -qemu_put_be32 (f, sbuf->sb_cc); -qemu_put_be32 (f, sbuf->sb_datalen); +static void slirp_sbuf_save (Visitor *v,struct sbuf *sbuf,const char *pfield,Error *err) +{ +int32_t off; +char f[128 ]; +int i, l =0 ; + +if (pfield) { +assert (strlen (pfield) <sizeof (f)); +strcpy (f, pfield); + l =strlen (f); + } + +visit_type_uint32_t (v, &sbuf->sb_cc,strocat (f,".sb_cc" , l), &err); +visit_type_uint32_t (v, &sbuf->sb_datalen,strocat (f,".sb_datalen" , l), &err); off = (uint32_t )(sbuf->sb_wptr - sbuf->sb_data); -qemu_put_sbe32 (f, off); +visit_type_int32_t (v, &off,strocat (f,".sb_wptr_off" , l), &err); off = (uint32_t )(sbuf->sb_rptr - sbuf->sb_data); -qemu_put_sbe32 (f, off); -qemu_put_buffer (f, (unsigned char *)sbuf->sb_data, sbuf->sb_datalen); +visit_type_int32_t (v, &off,strocat (f,".sb_rptr_off" , l), &err); + +visit_start_array (v, (void **)&sbuf->sb_data,strocat (f,".sb_data" , l), + sbuf->sb_datalen,sizeof (*sbuf->sb_data), &err); +for (i =0 ; i < sbuf->sb_datalen; i++) { +visit_type_uint8_t (v, (uint8_t *)&sbuf->sb_data[i],NULL , &err); + } +visit_end_array (v, &err); +} + +static void slirp_socket_visit (Visitor *v,struct socket *so,const char *pfield,Error *err) +{ +char f[64 ]; +int l =0 ; + +if (pfield) { +assert (strlen (pfield) <sizeof (f)); +strcpy (f, pfield); + l =strlen (f); + } +visit_type_int32_t (v, &so->so_urgc,strocat (f,".so_urgc" , l), &err); +visit_type_uint32_t (v, &so->so_faddr.s_addr,strocat (f,".so_faddr.s_addr" , l), &err); +visit_type_uint32_t (v, &so->so_laddr.s_addr,strocat (f,".so_urgc" , l), &err); +visit_type_uint16_t (v, &so->so_fport,strocat (f,".so_fport" , l), &err); +visit_type_uint16_t (v, &so->so_lport,strocat (f,".so_lport" , l), &err); +visit_type_uint8_t (v, &so->so_iptos,strocat (f,".so_iptos" , l), &err); +visit_type_uint8_t (v, &so->so_emu,strocat (f,".so_emu" , l), &err); +visit_type_uint8_t (v, &so->so_type,strocat (f,".so_type" , l), &err); +visit_type_int32_t (v, &so->so_state,strocat (f,".so_state" , l), &err); } -static void slirp_socket_save (QEMUFile *f,struct socket *so) +static void slirp_socket_save (Visitor *v,struct socket *so,const char *pfield,Error *err) { -qemu_put_be32 (f, so->so_urgc); -qemu_put_be32 (f, so->so_faddr.s_addr); -qemu_put_be32 (f, so->so_laddr.s_addr); -qemu_put_be16 (f, so->so_fport); -qemu_put_be16 (f, so->so_lport); -qemu_put_byte (f, so->so_iptos); -qemu_put_byte (f, so->so_emu); -qemu_put_byte (f, so->so_type); -qemu_put_be32 (f, so->so_state); -slirp_sbuf_save (f, &so->so_rcv); -slirp_sbuf_save (f, &so->so_snd); -slirp_tcp_save (f, so->so_tcpcb); +char f[64 ]; +int l =0 ; + +if (pfield) { +assert (strlen (pfield) <sizeof (f)); +strcpy (f, pfield); + l =strlen (f); + } + +slirp_socket_visit (v, so, f, err); + +slirp_sbuf_save (v, &so->so_rcv,strocat (f,".so_rcv" , l), err); +slirp_sbuf_save (v, &so->so_snd,strocat (f,".so_snd" , l), err); +slirp_tcp_save (v, so->so_tcpcb,strocat (f,".so_tcpcb" , l), err); } -static void slirp_bootp_save (QEMUFile *f,Slirp *slirp) +static void slirp_bootp_visit (Visitor *v,Slirp *slirp,const char *pfield,Error *err) { -int i; +int i, j, l; +void *ptr; +char f[64 ]; + +if (pfield) { +assert (strlen (pfield) <sizeof (f)); +strcpy (f, pfield); + l =strlen (f); + } + ptr = slirp->bootp_clients; +visit_start_array (v, &ptr,strocat (f,".bootp_clients" , l),NB_BOOTP_CLIENTS ,8 , &err);for (i =0 ; i <NB_BOOTP_CLIENTS ; i++) { -qemu_put_be16 (f, slirp->bootp_clients[i].allocated); -qemu_put_buffer (f, slirp->bootp_clients[i].macaddr,6 ); +visit_type_uint16_t (v, &slirp->bootp_clients[i].allocated,"allocated" , &err); + ptr = slirp->bootp_clients[i].macaddr; +visit_start_array (v, &ptr,"macaddr" ,6 ,1 , &err); +for (j =0 ; j <6 ; j++) { +visit_type_uint8_t (v, (uint8_t *)&slirp->bootp_clients[j],NULL , &err); + } +visit_end_array (v, &err); } +visit_end_array (v, &err); }static void slirp_state_save (QEMUFile *f,void *opaque) {Slirp *slirp = opaque;struct ex_list *ex_ptr; +int i =0 ; +uint8_t padding; +Visitor *v =qemu_file_get_output_visitor (f); +Error *err =NULL ; +char id[32 ];for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)if (ex_ptr->ex_pty ==3 ) {
@@ -970,70 +1048,44 @@ static void slirp_state_save(QEMUFile *f, void *opaque)if (!so)continue ; -qemu_put_byte (f,42 ); -slirp_socket_save (f, so); + padding =SLIRP_DELIMITER ; +visit_type_uint8_t (v, &padding,"padding" , &err); +slirp_socket_save (v, so,strocat (id,"so" ,strlen (id)), err); } -qemu_put_byte (f,0 ); + padding =0 ; +visit_type_uint8_t (v, &padding,"padding" , &err); + +visit_type_uint16_t (v, &slirp->ip_id,"slirp.ip_id" , &err); -qemu_put_be16 (f, slirp->ip_id); +slirp_bootp_visit (v, slirp,"slirp" , err); -slirp_bootp_save (f, slirp); +if (err) { +error_report ("error saving slirp state: %s" ,error_get_pretty (err)); +error_free (err); + } } -static void slirp_tcp_load (QEMUFile *f,struct tcpcb *tp) +static void slirp_tcp_load (Visitor *v,struct tcpcb *tp,const char *pfield,Error *err) { -int i; - - tp->t_state =qemu_get_sbe16 (f); -for (i =0 ; i <TCPT_NTIMERS ; i++) - tp->t_timer[i] =qemu_get_sbe16 (f); - tp->t_rxtshift =qemu_get_sbe16 (f); - tp->t_rxtcur =qemu_get_sbe16 (f); - tp->t_dupacks =qemu_get_sbe16 (f); - tp->t_maxseg =qemu_get_be16 (f); - tp->t_force =qemu_get_sbyte (f); - tp->t_flags =qemu_get_be16 (f); - tp->snd_una =qemu_get_be32 (f); - tp->snd_nxt =qemu_get_be32 (f); - tp->snd_up =qemu_get_be32 (f); - tp->snd_wl1 =qemu_get_be32 (f); - tp->snd_wl2 =qemu_get_be32 (f); - tp->iss =qemu_get_be32 (f); - tp->snd_wnd =qemu_get_be32 (f); - tp->rcv_wnd =qemu_get_be32 (f); - tp->rcv_nxt =qemu_get_be32 (f); - tp->rcv_up =qemu_get_be32 (f); - tp->irs =qemu_get_be32 (f); - tp->rcv_adv =qemu_get_be32 (f); - tp->snd_max =qemu_get_be32 (f); - tp->snd_cwnd =qemu_get_be32 (f); - tp->snd_ssthresh =qemu_get_be32 (f); - tp->t_idle =qemu_get_sbe16 (f); - tp->t_rtt =qemu_get_sbe16 (f); - tp->t_rtseq =qemu_get_be32 (f); - tp->t_srtt =qemu_get_sbe16 (f); - tp->t_rttvar =qemu_get_sbe16 (f); - tp->t_rttmin =qemu_get_be16 (f); - tp->max_sndwnd =qemu_get_be32 (f); - tp->t_oobflags =qemu_get_byte (f); - tp->t_iobc =qemu_get_byte (f); - tp->t_softerror =qemu_get_sbe16 (f); - tp->snd_scale =qemu_get_byte (f); - tp->rcv_scale =qemu_get_byte (f); - tp->request_r_scale =qemu_get_byte (f); - tp->requested_s_scale =qemu_get_byte (f); - tp->ts_recent =qemu_get_be32 (f); - tp->ts_recent_age =qemu_get_be32 (f); - tp->last_ack_sent =qemu_get_be32 (f); +slirp_tcp_visit (v, tp, pfield, err); +tcp_template (tp); } -static int slirp_sbuf_load (QEMUFile *f,struct sbuf *sbuf) +static int slirp_sbuf_load (Visitor *v,struct sbuf *sbuf,const char *pfield,Error *err) {uint32_t off, sb_cc, sb_datalen; +int l =0 , i; +char f[64 ]; + +if (pfield) { +assert (strlen (pfield) <sizeof (f)); +strcpy (f, pfield); + l =strlen (f); + } - sb_cc =qemu_get_be32 (f); - sb_datalen =qemu_get_be32 (f); +visit_type_uint32_t (v, &sb_cc,strocat (f,".sb_cc" , l), &err); +visit_type_uint32_t (v, &sb_datalen,strocat (f,".sb_datalen" , l), &err);sbreserve (sbuf, sb_datalen);
@@ -1042,61 +1094,68 @@ static int slirp_sbuf_load(QEMUFile *f, struct sbuf *sbuf) sbuf->sb_cc = sb_cc; - off =qemu_get_sbe32 (f); +visit_type_uint32_t (v, &off,strocat (f,".sb_wptr_off" , l), &err); sbuf->sb_wptr = sbuf->sb_data + off; - off =qemu_get_sbe32 (f); +visit_type_uint32_t (v, &off,strocat (f,".sb_rptr_off" , l), &err); sbuf->sb_rptr = sbuf->sb_data + off; -qemu_get_buffer (f, (unsigned char *)sbuf->sb_data, sbuf->sb_datalen); + +visit_start_array (v, (void **)&sbuf->sb_data,strocat (f,".sb_data" , l), + sbuf->sb_datalen,sizeof (*sbuf->sb_data), &err); +for (i =0 ; i < sbuf->sb_datalen; i++) { +visit_type_uint8_t (v, (uint8_t *)&sbuf->sb_data[i],NULL , &err); + } +visit_end_array (v, &err);return 0 ; } -static int slirp_socket_load (QEMUFile *f,struct socket *so) +static int slirp_socket_load (Visitor *v,struct socket *so,const char *pfield,Error *err) { +char f[64 ]; +int l =0 ; + +if (pfield) { +assert (strlen (pfield) <sizeof (f)); +strcpy (f, pfield); + l =strlen (f); + } + +assert (v);if (tcp_attach (so) <0 )return -ENOMEM ; - so->so_urgc =qemu_get_be32 (f); - so->so_faddr.s_addr =qemu_get_be32 (f); - so->so_laddr.s_addr =qemu_get_be32 (f); - so->so_fport =qemu_get_be16 (f); - so->so_lport =qemu_get_be16 (f); - so->so_iptos =qemu_get_byte (f); - so->so_emu =qemu_get_byte (f); - so->so_type =qemu_get_byte (f); - so->so_state =qemu_get_be32 (f); -if (slirp_sbuf_load (f, &so->so_rcv) <0 ) +slirp_socket_visit (v, so, pfield, err); + +if (slirp_sbuf_load (v, &so->so_rcv,strocat (f,".so_rcv" , l), err) <0 )return -ENOMEM ; -if (slirp_sbuf_load (f, &so->so_snd) <0 ) +if (slirp_sbuf_load (v, &so->so_snd,strocat (f,".so_snd" , l), err) <0 )return -ENOMEM ; -slirp_tcp_load (f, so->so_tcpcb); +slirp_tcp_load (v, so->so_tcpcb,strocat (f,".so_tcpcb" , l), err);return 0 ; } -static void slirp_bootp_load (QEMUFile *f,Slirp *slirp) -{ -int i; - -for (i =0 ; i <NB_BOOTP_CLIENTS ; i++) { - slirp->bootp_clients[i].allocated =qemu_get_be16 (f); -qemu_get_buffer (f, slirp->bootp_clients[i].macaddr,6 ); - } -} -static int slirp_state_load (QEMUFile *f,void *opaque,int version_id) {Slirp *slirp = opaque;struct ex_list *ex_ptr; +Visitor *v =qemu_file_get_input_visitor (f); +Error *err =NULL ; +char id[32 ]; +uint8_t padding, i =0 ; -while (qemu_get_byte (f)) { +visit_type_uint8_t (v, &padding,"padding" , &err); + +while (padding ==SLIRP_DELIMITER ) {int ret;struct socket *so =socreate (slirp);if (!so)return -ENOMEM ; - ret =slirp_socket_load (f, so); +sprintf (id,"slirp[%d]" , i++); + + ret =slirp_socket_load (v, so,strocat (id,"so" ,strlen (id)), err);if (ret <0 )return ret;
@@ -1118,12 +1177,19 @@ static int slirp_state_load(QEMUFile *f, void *opaque, int version_id) so->extra = (void *)ex_ptr->ex_exec; } +if (version_id >=2 ) { - slirp->ip_id =qemu_get_be16 (f); +visit_type_uint16_t (v, &slirp->ip_id,"slirp.ip_id" , &err); }if (version_id >=3 ) { -slirp_bootp_load (f, slirp); +slirp_bootp_visit (v, slirp,"slirp" , err); + } + +if (err) { +error_report ("error loading slirp state: %s" ,error_get_pretty (err)); +error_free (err); +return -EINVAL ; }return 0 ;
Signature · 1 line
1.7.0.4
Re: [Qemu-devel] [RFC 7/8] cutil: add strocat(), to concat a string to an offset in another
In reply to Michael Rothcutils.c int stristart(const char *str, const char *val, const char **ptr) · line 91
Quoted 5 lines · Michael Roth
cutils.c +5
-0+/* concat first 'offset' chars in 'dest' with 'src' */ +char *strocat (char *buf,const char *src,int offset) +{ +memcpy (&buf[offset], src,strlen (src)); + buf[offset+strlen (src)] ='\0' ;
This is just strcpy(&buf[offset], src);
cutils.c int stristart(const char *str, const char *val, const char **ptr) · line 93
Quoted 2 lines
cutils.c +2
-0+return buf; +}
Paolo
Re: [Qemu-devel] [RFC 4/8] savevm: move QEMUFile interfaces into qemu-file.c
In reply to Michael RothQuoted 123 lines · Michael Roth
Signed-off-by Michael Roth <[email protected]>---
Makefile.objs | 2 +-
hw/hw.h | 1 +
qemu-file.c | 521 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++savevm.c| 494 ------------------------------------------------------4 files changed, 523 insertions(+), 495 deletions(-)create mode 100644
qemu-file.cMakefile.objs +0
-0@@ -119,7 +119,7 @@ common-obj-$(CONFIG_SD) += sd.ocommon-obj-y += bt.o bt-host.o bt-vhci.o bt-l2cap.o bt-sdp.o bt-hci.o bt-hid.o usb-bt.o common-obj-y += bt-hci-csr.o
common-obj-y += buffered_file.o migration.o migration-tcp.oMakefile.objs +1
-1-common-obj-y += qemu-char.o savevm.o #aio.o +common-obj-y += qemu-char.o savevm.o qemu-file.o #aio.ocommon-obj-y += msmouse.o ps2.o
common-obj-y += qdev.o qdev-properties.o
common-obj-y += block-migration.o iohandler.ohw/hw.h +1
-0@@ -58,6 +58,7 @@ void qemu_fflush(QEMUFile *f); int qemu_fclose(QEMUFile *f); void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size); void qemu_put_byte(QEMUFile *f, int v); +int qemu_peek_byte (QEMUFile *f); static inline void qemu_put_ubyte(QEMUFile *f, unsigned int v) {qemu-file.c new file +82
-0@@ -0,0 +1,521 @@ +/* +* QEMU System Emulator +* +* Copyright (c) 2003-2008 Fabrice Bellard +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +#include "qemu-common.h" +#include "qemu_socket.h" +#include "hw/hw.h" + +#define IO_BUF_SIZE 32768 + +struct QEMUFile { + QEMUFilePutBufferFunc *put_buffer; + QEMUFileGetBufferFunc *get_buffer; + QEMUFileCloseFunc *close; + QEMUFileRateLimit *rate_limit; + QEMUFileSetRateLimit *set_rate_limit; + QEMUFileGetRateLimit *get_rate_limit; + void *opaque; + int is_write; + + int64_t buf_offset; /* start of buffer when writing, end of buffer +when reading */ + int buf_index; + int buf_size; /* 0 when writing */ + uint8_t buf[IO_BUF_SIZE]; + + int has_error; +}; + +QEMUFile *qemu_fopen_ops (void *opaque,QEMUFilePutBufferFunc *put_buffer, + QEMUFileGetBufferFunc *get_buffer, + QEMUFileCloseFunc *close, + QEMUFileRateLimit *rate_limit, + QEMUFileSetRateLimit *set_rate_limit, + QEMUFileGetRateLimit *get_rate_limit) +{ + QEMUFile *f; + + f = g_malloc0(sizeof(QEMUFile)); + + f->opaque = opaque; + f->put_buffer = put_buffer; + f->get_buffer = get_buffer; + f->close = close; + f->rate_limit = rate_limit; + f->set_rate_limit = set_rate_limit; + f->get_rate_limit = get_rate_limit; + f->is_write = 0; + + return f; +} + +int qemu_file_has_error (QEMUFile *f) +{ + return f->has_error; +} + +void qemu_file_set_error (QEMUFile *f) +{ + f->has_error = 1; +} +
qemu-file.c line 92
Quoted 10 lines
qemu-file.c new file +10
-0+void qemu_fflush (QEMUFile *f) +{ + if (!f->put_buffer) + return; + + if (f->is_write && f->buf_index > 0) { + int len; + + len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index); + if (len > 0)
Please fix CODING_STYLE issues before moving, so the old style will not persist forever.
Quoted 946 lines
qemu-file.c new file +429
-0+ f->buf_offset += f->buf_index; + else + f->has_error = 1; + f->buf_index = 0; + } +} + +static void qemu_fill_buffer (QEMUFile *f) +{ + int len; + + if (!f->get_buffer) + return; + + if (f->is_write) + abort(); + + len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE); + if (len > 0) { + f->buf_index = 0; + f->buf_size = len; + f->buf_offset += len; + } else if (len != -EAGAIN) + f->has_error = 1; +} + +int qemu_fclose (QEMUFile *f) +{ + int ret = 0; + qemu_fflush(f); + if (f->close) + ret = f->close(f->opaque); + g_free(f); + return ret; +} + +void qemu_file_put_notify (QEMUFile *f) +{ + f->put_buffer(f->opaque, NULL, 0, 0); +} + +void qemu_put_buffer (QEMUFile *f,const uint8_t *buf,int size) +{ + int l; + + if (!f->has_error && f->is_write == 0 && f->buf_index > 0) { + fprintf(stderr, + "Attempted to write to buffer while read buffer is not empty\n"); + abort(); + } + + while (!f->has_error && size > 0) { + l = IO_BUF_SIZE - f->buf_index; + if (l > size) + l = size; + memcpy(f->buf + f->buf_index, buf, l); + f->is_write = 1; + f->buf_index += l; + buf += l; + size -= l; + if (f->buf_index >= IO_BUF_SIZE) + qemu_fflush(f); + } +} + +void qemu_put_byte (QEMUFile *f,int v) +{ + if (!f->has_error && f->is_write == 0 && f->buf_index > 0) { + fprintf(stderr, + "Attempted to write to buffer while read buffer is not empty\n"); + abort(); + } + + f->buf[f->buf_index++] = v; + f->is_write = 1; + if (f->buf_index >= IO_BUF_SIZE) + qemu_fflush(f); +} + +int qemu_get_buffer (QEMUFile *f,uint8_t *buf,int size1) +{ + int size, l; + + if (f->is_write) + abort(); + + size = size1; + while (size > 0) { + l = f->buf_size - f->buf_index; + if (l == 0) { + qemu_fill_buffer(f); + l = f->buf_size - f->buf_index; + if (l == 0) + break; + } + if (l > size) + l = size; + memcpy(buf, f->buf + f->buf_index, l); + f->buf_index += l; + buf += l; + size -= l; + } + return size1 - size; +} + +int qemu_get_byte (QEMUFile *f) +{ + if (f->is_write) + abort(); + + if (f->buf_index >= f->buf_size) { + qemu_fill_buffer(f); + if (f->buf_index >= f->buf_size) + return 0; + } + return f->buf[f->buf_index++]; +} + +int64_t qemu_ftell (QEMUFile *f) +{ + return f->buf_offset - f->buf_size + f->buf_index; +} + +int64_t qemu_fseek (QEMUFile *f,int64_t pos,int whence) +{ + if (whence == SEEK_SET) { + /* nothing to do */ + } else if (whence == SEEK_CUR) { + pos += qemu_ftell(f); + } else { + /* SEEK_END not supported */ + return -1; + } + if (f->put_buffer) { + qemu_fflush(f); + f->buf_offset = pos; + } else { + f->buf_offset = pos; + f->buf_index = 0; + f->buf_size = 0; + } + return pos; +} + +int qemu_peek_byte (QEMUFile *f) +{ + if (f->is_write) + abort(); + + if (f->buf_index >= f->buf_size) { + qemu_fill_buffer(f); + if (f->buf_index >= f->buf_size) + return 0; + } + return f->buf[f->buf_index]; +} + +int qemu_file_rate_limit (QEMUFile *f) +{ + if (f->rate_limit) + return f->rate_limit(f->opaque); + + return 0; +} + +int64_t qemu_file_get_rate_limit (QEMUFile *f) +{ + if (f->get_rate_limit) + return f->get_rate_limit(f->opaque); + + return 0; +} + +int64_t qemu_file_set_rate_limit (QEMUFile *f,int64_t new_rate) +{ + /* any failed or completed migration keeps its state to allow probing of + * migration data, but has no associated file anymore */ + if (f && f->set_rate_limit) + return f->set_rate_limit(f->opaque, new_rate); + + return 0; +} + +void qemu_put_be16 (QEMUFile *f,unsigned int v) +{ + qemu_put_byte(f, v >> 8); + qemu_put_byte(f, v); +} + +void qemu_put_be32 (QEMUFile *f,unsigned int v) +{ + qemu_put_byte(f, v >> 24); + qemu_put_byte(f, v >> 16); + qemu_put_byte(f, v >> 8); + qemu_put_byte(f, v); +} + +void qemu_put_be64 (QEMUFile *f,uint64_t v) +{ + qemu_put_be32(f, v >> 32); + qemu_put_be32(f, v); +} + +unsigned int qemu_get_be16 (QEMUFile *f) +{ + unsigned int v; + v = qemu_get_byte(f) << 8; + v |= qemu_get_byte(f); + return v; +} + +unsigned int qemu_get_be32 (QEMUFile *f) +{ + unsigned int v; + v = qemu_get_byte(f) << 24; + v |= qemu_get_byte(f) << 16; + v |= qemu_get_byte(f) << 8; + v |= qemu_get_byte(f); + return v; +} + +uint64_t qemu_get_be64 (QEMUFile *f) +{ + uint64_t v; + v = (uint64_t)qemu_get_be32(f) << 32; + v |= qemu_get_be32(f); + return v; +} + +typedef struct QEMUFileStdio +{ + FILE *stdio_file; + QEMUFile *file; +}QEMUFileStdio ; + +typedef struct QEMUFileSocket +{ + int fd; + QEMUFile *file; +}QEMUFileSocket ; + +static int socket_get_buffer (void *opaque,uint8_t *buf,int64_t pos,int size) +{ + QEMUFileSocket *s = opaque; + ssize_t len; + + do { + len = qemu_recv(s->fd, buf, size, 0); + } while (len == -1 && socket_error() == EINTR); + + if (len == -1) + len = -socket_error(); + + return len; +} + +static int socket_close (void *opaque) +{ + QEMUFileSocket *s = opaque; + g_free(s); + return 0; +} + +int qemu_stdio_fd (QEMUFile *f) +{ + QEMUFileStdio *p; + int fd; + + p = (QEMUFileStdio *)f->opaque; + fd = fileno(p->stdio_file); + + return fd; +} + +static int stdio_put_buffer (void *opaque,const uint8_t *buf,int64_t pos,int size) +{ + QEMUFileStdio *s = opaque; + return fwrite(buf, 1, size, s->stdio_file); +} + +static int stdio_get_buffer (void *opaque,uint8_t *buf,int64_t pos,int size) +{ + QEMUFileStdio *s = opaque; + FILE *fp = s->stdio_file; + int bytes; + + do { + clearerr(fp); + bytes = fread(buf, 1, size, fp); + } while ((bytes == 0) && ferror(fp) && (errno == EINTR)); + return bytes; +} + +static int stdio_pclose (void *opaque) +{ + QEMUFileStdio *s = opaque; + int ret; + ret = pclose(s->stdio_file); + g_free(s); + return ret; +} + +static int stdio_fclose (void *opaque) +{ + QEMUFileStdio *s = opaque; + fclose(s->stdio_file); + g_free(s); + return 0; +} + +QEMUFile *qemu_popen (FILE *stdio_file,const char *mode) +{ + QEMUFileStdio *s; + + if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) { + fprintf(stderr, "qemu_popen: Argument validity check failed\n"); + return NULL; + } + + s = g_malloc0(sizeof(QEMUFileStdio)); + + s->stdio_file = stdio_file; + + if(mode[0] == 'r') { + s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_pclose, + NULL, NULL, NULL); + } else { + s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_pclose, + NULL, NULL, NULL); + } + return s->file; +} + +QEMUFile *qemu_popen_cmd (const char *command,const char *mode) +{ + FILE *popen_file; + + popen_file = popen(command, mode); + if(popen_file == NULL) { + return NULL; + } + + return qemu_popen(popen_file, mode); +} + +QEMUFile *qemu_fdopen (int fd,const char *mode) +{ + QEMUFileStdio *s; + + if (mode == NULL || + (mode[0] != 'r' && mode[0] != 'w') || + mode[1] != 'b' || mode[2] != 0) { + fprintf(stderr, "qemu_fdopen: Argument validity check failed\n"); + return NULL; + } + + s = g_malloc0(sizeof(QEMUFileStdio)); + s->stdio_file = fdopen(fd, mode); + if (!s->stdio_file) + goto fail; + + if(mode[0] == 'r') { + s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_fclose, + NULL, NULL, NULL); + } else { + s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_fclose, + NULL, NULL, NULL); + } + return s->file; + +fail: + g_free(s); + return NULL; +} + +QEMUFile *qemu_fopen_socket (int fd) +{ + QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket)); + + s->fd = fd; + s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close, + NULL, NULL, NULL); + return s->file; +} + +static int file_put_buffer (void *opaque,const uint8_t *buf, + int64_t pos, int size) +{ + QEMUFileStdio *s = opaque; + fseek(s->stdio_file, pos, SEEK_SET); + return fwrite(buf, 1, size, s->stdio_file); +} + +static int file_get_buffer (void *opaque,uint8_t *buf,int64_t pos,int size) +{ + QEMUFileStdio *s = opaque; + fseek(s->stdio_file, pos, SEEK_SET); + return fread(buf, 1, size, s->stdio_file); +} + +QEMUFile *qemu_fopen (const char *filename,const char *mode) +{ + QEMUFileStdio *s; + + if (mode == NULL || + (mode[0] != 'r' && mode[0] != 'w') || + mode[1] != 'b' || mode[2] != 0) { + fprintf(stderr, "qemu_fopen: Argument validity check failed\n"); + return NULL; + } + + s = g_malloc0(sizeof(QEMUFileStdio)); + + s->stdio_file = fopen(filename, mode); + if (!s->stdio_file) + goto fail; + + if(mode[0] == 'w') { + s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose, + NULL, NULL, NULL); + } else { + s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose, + NULL, NULL, NULL); + } + return s->file; +fail: + g_free(s); + return NULL; +}savevm.c +0
-495@@ -155,228 +155,6 @@ void qemu_announce_self(void) /***********************************************************/ /* savevm/loadvm support */ -#define IO_BUF_SIZE 32768 - -struct QEMUFile { - QEMUFilePutBufferFunc *put_buffer; - QEMUFileGetBufferFunc *get_buffer; - QEMUFileCloseFunc *close; - QEMUFileRateLimit *rate_limit; - QEMUFileSetRateLimit *set_rate_limit; - QEMUFileGetRateLimit *get_rate_limit; - void *opaque; - int is_write; - - int64_t buf_offset; /* start of buffer when writing, end of buffer -when reading */ - int buf_index; - int buf_size; /* 0 when writing */ - uint8_t buf[IO_BUF_SIZE]; - - int has_error; -}; - -typedef struct QEMUFileStdio -{ - FILE *stdio_file; - QEMUFile *file; -}QEMUFileStdio ; - -typedef struct QEMUFileSocket -{ - int fd; - QEMUFile *file; -}QEMUFileSocket ; - -static int socket_get_buffer (void *opaque,uint8_t *buf,int64_t pos,int size) -{ - QEMUFileSocket *s = opaque; - ssize_t len; - - do { - len = qemu_recv(s->fd, buf, size, 0); - } while (len == -1 && socket_error() == EINTR); - - if (len == -1) - len = -socket_error(); - - return len; -} - -static int socket_close (void *opaque) -{ - QEMUFileSocket *s = opaque; - g_free(s); - return 0; -} - -static int stdio_put_buffer (void *opaque,const uint8_t *buf,int64_t pos,int size) -{ - QEMUFileStdio *s = opaque; - return fwrite(buf, 1, size, s->stdio_file); -} - -static int stdio_get_buffer (void *opaque,uint8_t *buf,int64_t pos,int size) -{ - QEMUFileStdio *s = opaque; - FILE *fp = s->stdio_file; - int bytes; - - do { - clearerr(fp); - bytes = fread(buf, 1, size, fp); - } while ((bytes == 0) && ferror(fp) && (errno == EINTR)); - return bytes; -} - -static int stdio_pclose (void *opaque) -{ - QEMUFileStdio *s = opaque; - int ret; - ret = pclose(s->stdio_file); - g_free(s); - return ret; -} - -static int stdio_fclose (void *opaque) -{ - QEMUFileStdio *s = opaque; - fclose(s->stdio_file); - g_free(s); - return 0; -} - -QEMUFile *qemu_popen (FILE *stdio_file,const char *mode) -{ - QEMUFileStdio *s; - - if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) { - fprintf(stderr, "qemu_popen: Argument validity check failed\n"); - return NULL; - } - - s = g_malloc0(sizeof(QEMUFileStdio)); - - s->stdio_file = stdio_file; - - if(mode[0] == 'r') { - s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_pclose, - NULL, NULL, NULL); - } else { - s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_pclose, - NULL, NULL, NULL); - } - return s->file; -} - -QEMUFile *qemu_popen_cmd (const char *command,const char *mode) -{ - FILE *popen_file; - - popen_file = popen(command, mode); - if(popen_file == NULL) { - return NULL; - } - - return qemu_popen(popen_file, mode); -} - -int qemu_stdio_fd (QEMUFile *f) -{ - QEMUFileStdio *p; - int fd; - - p = (QEMUFileStdio *)f->opaque; - fd = fileno(p->stdio_file); - - return fd; -} - -QEMUFile *qemu_fdopen (int fd,const char *mode) -{ - QEMUFileStdio *s; - - if (mode == NULL || - (mode[0] != 'r' && mode[0] != 'w') || - mode[1] != 'b' || mode[2] != 0) { - fprintf(stderr, "qemu_fdopen: Argument validity check failed\n"); - return NULL; - } - - s = g_malloc0(sizeof(QEMUFileStdio)); - s->stdio_file = fdopen(fd, mode); - if (!s->stdio_file) - goto fail; - - if(mode[0] == 'r') { - s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_fclose, - NULL, NULL, NULL); - } else { - s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_fclose, - NULL, NULL, NULL); - } - return s->file; - -fail: - g_free(s); - return NULL; -} - -QEMUFile *qemu_fopen_socket (int fd) -{ - QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket)); - - s->fd = fd; - s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close, - NULL, NULL, NULL); - return s->file; -} - -static int file_put_buffer (void *opaque,const uint8_t *buf, - int64_t pos, int size) -{ - QEMUFileStdio *s = opaque; - fseek(s->stdio_file, pos, SEEK_SET); - return fwrite(buf, 1, size, s->stdio_file); -} - -static int file_get_buffer (void *opaque,uint8_t *buf,int64_t pos,int size) -{ - QEMUFileStdio *s = opaque; - fseek(s->stdio_file, pos, SEEK_SET); - return fread(buf, 1, size, s->stdio_file); -} - -QEMUFile *qemu_fopen (const char *filename,const char *mode) -{ - QEMUFileStdio *s; - - if (mode == NULL || - (mode[0] != 'r' && mode[0] != 'w') || - mode[1] != 'b' || mode[2] != 0) { - fprintf(stderr, "qemu_fopen: Argument validity check failed\n"); - return NULL; - } - - s = g_malloc0(sizeof(QEMUFileStdio)); - - s->stdio_file = fopen(filename, mode); - if (!s->stdio_file) - goto fail; - - if(mode[0] == 'w') { - s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose, - NULL, NULL, NULL); - } else { - s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose, - NULL, NULL, NULL); - } - return s->file; -fail: - g_free(s); - return NULL; -} - static int block_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size) {@@ -402,278 +180,6 @@ static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable) return qemu_fopen_ops(bs, NULL, block_get_buffer, bdrv_fclose, NULL, NULL, NULL); } -QEMUFile *qemu_fopen_ops (void *opaque,QEMUFilePutBufferFunc *put_buffer, - QEMUFileGetBufferFunc *get_buffer, - QEMUFileCloseFunc *close, - QEMUFileRateLimit *rate_limit, - QEMUFileSetRateLimit *set_rate_limit, - QEMUFileGetRateLimit *get_rate_limit) -{ - QEMUFile *f; - - f = g_malloc0(sizeof(QEMUFile)); - - f->opaque = opaque; - f->put_buffer = put_buffer; - f->get_buffer = get_buffer; - f->close = close; - f->rate_limit = rate_limit; - f->set_rate_limit = set_rate_limit; - f->get_rate_limit = get_rate_limit; - f->is_write = 0; - - return f; -} - -int qemu_file_has_error (QEMUFile *f) -{ - return f->has_error; -} - -void qemu_file_set_error (QEMUFile *f) -{ - f->has_error = 1; -} - -void qemu_fflush (QEMUFile *f) -{ - if (!f->put_buffer) - return; - - if (f->is_write && f->buf_index > 0) { - int len; - - len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index); - if (len > 0) - f->buf_offset += f->buf_index; - else - f->has_error = 1; - f->buf_index = 0; - } -} - -static void qemu_fill_buffer (QEMUFile *f) -{ - int len; - - if (!f->get_buffer) - return; - - if (f->is_write) - abort(); - - len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE); - if (len > 0) { - f->buf_index = 0; - f->buf_size = len; - f->buf_offset += len; - } else if (len != -EAGAIN) - f->has_error = 1; -} - -int qemu_fclose (QEMUFile *f) -{ - int ret = 0; - qemu_fflush(f); - if (f->close) - ret = f->close(f->opaque); - g_free(f); - return ret; -} - -void qemu_file_put_notify (QEMUFile *f) -{ - f->put_buffer(f->opaque, NULL, 0, 0); -} - -void qemu_put_buffer (QEMUFile *f,const uint8_t *buf,int size) -{ - int l; - - if (!f->has_error && f->is_write == 0 && f->buf_index > 0) { - fprintf(stderr, - "Attempted to write to buffer while read buffer is not empty\n"); - abort(); - } - - while (!f->has_error && size > 0) { - l = IO_BUF_SIZE - f->buf_index; - if (l > size) - l = size; - memcpy(f->buf + f->buf_index, buf, l); - f->is_write = 1; - f->buf_index += l; - buf += l; - size -= l; - if (f->buf_index >= IO_BUF_SIZE) - qemu_fflush(f); - } -} - -void qemu_put_byte (QEMUFile *f,int v) -{ - if (!f->has_error && f->is_write == 0 && f->buf_index > 0) { - fprintf(stderr, - "Attempted to write to buffer while read buffer is not empty\n"); - abort(); - } - - f->buf[f->buf_index++] = v; - f->is_write = 1; - if (f->buf_index >= IO_BUF_SIZE) - qemu_fflush(f); -} - -int qemu_get_buffer (QEMUFile *f,uint8_t *buf,int size1) -{ - int size, l; - - if (f->is_write) - abort(); - - size = size1; - while (size > 0) { - l = f->buf_size - f->buf_index; - if (l == 0) { - qemu_fill_buffer(f); - l = f->buf_size - f->buf_index; - if (l == 0) - break; - } - if (l > size) - l = size; - memcpy(buf, f->buf + f->buf_index, l); - f->buf_index += l; - buf += l; - size -= l; - } - return size1 - size; -} - -static int qemu_peek_byte (QEMUFile *f) -{ - if (f->is_write) - abort(); - - if (f->buf_index >= f->buf_size) { - qemu_fill_buffer(f); - if (f->buf_index >= f->buf_size) - return 0; - } - return f->buf[f->buf_index]; -} - -int qemu_get_byte (QEMUFile *f) -{ - if (f->is_write) - abort(); - - if (f->buf_index >= f->buf_size) { - qemu_fill_buffer(f); - if (f->buf_index >= f->buf_size) - return 0; - } - return f->buf[f->buf_index++]; -} - -int64_t qemu_ftell (QEMUFile *f) -{ - return f->buf_offset - f->buf_size + f->buf_index; -} - -int64_t qemu_fseek (QEMUFile *f,int64_t pos,int whence) -{ - if (whence == SEEK_SET) { - /* nothing to do */ - } else if (whence == SEEK_CUR) { - pos += qemu_ftell(f); - } else { - /* SEEK_END not supported */ - return -1; - } - if (f->put_buffer) { - qemu_fflush(f); - f->buf_offset = pos; - } else { - f->buf_offset = pos; - f->buf_index = 0; - f->buf_size = 0; - } - return pos; -} - -int qemu_file_rate_limit (QEMUFile *f) -{ - if (f->rate_limit) - return f->rate_limit(f->opaque); - - return 0; -} - -int64_t qemu_file_get_rate_limit (QEMUFile *f) -{ - if (f->get_rate_limit) - return f->get_rate_limit(f->opaque); - - return 0; -} - -int64_t qemu_file_set_rate_limit (QEMUFile *f,int64_t new_rate) -{ - /* any failed or completed migration keeps its state to allow probing of - * migration data, but has no associated file anymore */ - if (f && f->set_rate_limit) - return f->set_rate_limit(f->opaque, new_rate); - - return 0; -} - -void qemu_put_be16 (QEMUFile *f,unsigned int v) -{ - qemu_put_byte(f, v >> 8); - qemu_put_byte(f, v); -} - -void qemu_put_be32 (QEMUFile *f,unsigned int v) -{ - qemu_put_byte(f, v >> 24); - qemu_put_byte(f, v >> 16); - qemu_put_byte(f, v >> 8); - qemu_put_byte(f, v); -} - -void qemu_put_be64 (QEMUFile *f,uint64_t v) -{ - qemu_put_be32(f, v >> 32); - qemu_put_be32(f, v); -} - -unsigned int qemu_get_be16 (QEMUFile *f) -{ - unsigned int v; - v = qemu_get_byte(f) << 8; - v |= qemu_get_byte(f); - return v; -} - -unsigned int qemu_get_be32 (QEMUFile *f) -{ - unsigned int v; - v = qemu_get_byte(f) << 24; - v |= qemu_get_byte(f) << 16; - v |= qemu_get_byte(f) << 8; - v |= qemu_get_byte(f); - return v; -} - -uint64_t qemu_get_be64 (QEMUFile *f) -{ - uint64_t v; - v = (uint64_t)qemu_get_be32(f) << 32; - v |= qemu_get_be32(f); - return v; -} - /* bool */ static int get_bool(QEMUFile *f, void *pv, size_t size) --1.7.0.4
Re: [Qemu-devel] [RFC 8/8] slirp: convert save/load function to visitor interface
In reply to Michael RothQuoted 84 lines · Michael Roth
Where possible common routines are used for both input and output, thus save on lines of code, theoretically. The added lines here are mostly due to extra logic for each save/load routine to manipulate strings into a unique field name for each saved field, and in some cases a few extra Visitor calls to due list/struct i/o. With some reworking we can probably optimize all of these to reduce the amount of added code.
Signed-off-by Michael Roth<[email protected]>--- slirp/slirp.c | 366 +++++++++++++++++++++++++++++++++----------------------- 1 files changed, 216 insertions(+), 150 deletions(-)slirp/slirp.c +12
-44@@ -26,6 +26,9 @@#include "qemu-char.h" #include "slirp.h" #include "hw/hw.h" +#include "qemu-error.h" + +#define SLIRP_DELIMITER 42/* used to separate slirp instances in save/load */ /* host loopback address */ struct in_addr loopback_addr;@@ -871,96 +874,171 @@ void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr, int guest_port,tcp_output (sototcpcb (so)); } -static void slirp_tcp_save (QEMUFile *f,struct tcpcb *tp) +static void slirp_tcp_visit (Visitor *v,struct tcpcb *tp,const char *pfield,Error *err) { -int i; - -qemu_put_sbe16 (f, tp->t_state); -for (i =0 ; i<TCPT_NTIMERS ; i++) -qemu_put_sbe16 (f, tp->t_timer[i]); -qemu_put_sbe16 (f, tp->t_rxtshift); -qemu_put_sbe16 (f, tp->t_rxtcur); -qemu_put_sbe16 (f, tp->t_dupacks); -qemu_put_be16 (f, tp->t_maxseg); -qemu_put_sbyte (f, tp->t_force); -qemu_put_be16 (f, tp->t_flags); -qemu_put_be32 (f, tp->snd_una); -qemu_put_be32 (f, tp->snd_nxt); -qemu_put_be32 (f, tp->snd_up); -qemu_put_be32 (f, tp->snd_wl1); -qemu_put_be32 (f, tp->snd_wl2); -qemu_put_be32 (f, tp->iss); -qemu_put_be32 (f, tp->snd_wnd); -qemu_put_be32 (f, tp->rcv_wnd); -qemu_put_be32 (f, tp->rcv_nxt); -qemu_put_be32 (f, tp->rcv_up); -qemu_put_be32 (f, tp->irs); -qemu_put_be32 (f, tp->rcv_adv); -qemu_put_be32 (f, tp->snd_max); -qemu_put_be32 (f, tp->snd_cwnd); -qemu_put_be32 (f, tp->snd_ssthresh); -qemu_put_sbe16 (f, tp->t_idle); -qemu_put_sbe16 (f, tp->t_rtt); -qemu_put_be32 (f, tp->t_rtseq); -qemu_put_sbe16 (f, tp->t_srtt); -qemu_put_sbe16 (f, tp->t_rttvar); -qemu_put_be16 (f, tp->t_rttmin); -qemu_put_be32 (f, tp->max_sndwnd); -qemu_put_byte (f, tp->t_oobflags); -qemu_put_byte (f, tp->t_iobc); -qemu_put_sbe16 (f, tp->t_softerror); -qemu_put_byte (f, tp->snd_scale); -qemu_put_byte (f, tp->rcv_scale); -qemu_put_byte (f, tp->request_r_scale); -qemu_put_byte (f, tp->requested_s_scale); -qemu_put_be32 (f, tp->ts_recent); -qemu_put_be32 (f, tp->ts_recent_age); -qemu_put_be32 (f, tp->last_ack_sent); +char f[128 ]; +int i, l =0 ; +int16_t *ptr; + +if (pfield) { +assert (strlen (pfield)<sizeof (f)); +strcpy (f, pfield); + l =strlen (pfield);
slirp/slirp.c void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr, int guest_port, · line 896
Quoted 10 lines
slirp/slirp.c +10
-0+ } + +visit_type_int16_t (v,&tp->t_state,strocat (f,".t_state" , l),&err); + ptr = tp->t_timer; +visit_start_array (v, (void **)&ptr,strocat (f,".t_timer" , l),TCPT_NTIMERS ,sizeof (*ptr),&err); +for (i =0 ; i<TCPT_NTIMERS ; ++i) { +visit_type_int16_t (v,&ptr[i],NULL ,&err); + } +visit_end_array (v,&err); +visit_type_int16_t (v,&tp->t_rxtshift,strocat (f,".t_rxtshift" , l),&err);
Hrm, you should never concat a name like this. A better approach would be:
visit_start_struct(v, NULL, f); visit_type_int16_t(v, &tp->t_rxtshift, "t_rxtshift", &err);
...visit_end_struct(v, NULL, f);
structs indicate hierarchy. They don't have to correspond to real types.
Regards,
Anthony Liguori
Quoted 388 lines
slirp/slirp.c +194
-106+visit_type_int16_t (v,&tp->t_rxtcur,strocat (f,".t_rxtcur" , l),&err); +visit_type_int16_t (v,&tp->t_dupacks,strocat (f,".t_dupacks" , l),&err); +visit_type_uint16_t (v,&tp->t_maxseg,strocat (f,".t_maxseg" , l),&err); +visit_type_uint8_t (v, (uint8_t *)&tp->t_force,strocat (f,".t_force" , l),&err); +visit_type_uint16_t (v,&tp->t_flags,strocat (f,".t_flags" , l),&err); +visit_type_uint32_t (v,&tp->snd_una,strocat (f,".snd_una" , l),&err); +visit_type_uint32_t (v,&tp->snd_nxt,strocat (f,".snd_nxt" , l),&err); +visit_type_uint32_t (v,&tp->snd_up,strocat (f,".snd_up" , l),&err); +visit_type_uint32_t (v,&tp->snd_wl1,strocat (f,".snd_wl1" , l),&err); +visit_type_uint32_t (v,&tp->snd_wl2,strocat (f,".snd_wl2" , l),&err); +visit_type_uint32_t (v,&tp->iss,strocat (f,".iss" , l),&err); +visit_type_uint32_t (v,&tp->snd_wnd,strocat (f,".snd_wnd" , l),&err); +visit_type_uint32_t (v,&tp->rcv_wnd,strocat (f,".rcv_wnd" , l),&err); +visit_type_uint32_t (v,&tp->rcv_nxt,strocat (f,".rcv_nxt" , l),&err); +visit_type_uint32_t (v,&tp->rcv_up,strocat (f,".rcv_up" , l),&err); +visit_type_uint32_t (v,&tp->irs,strocat (f,".irs" , l),&err); +visit_type_uint32_t (v,&tp->rcv_adv,strocat (f,".rcv_adv" , l),&err); +visit_type_uint32_t (v,&tp->snd_max,strocat (f,".snd_max" , l),&err); +visit_type_uint32_t (v,&tp->snd_cwnd,strocat (f,".snd_cwnd" , l),&err); +visit_type_uint32_t (v,&tp->snd_ssthresh,strocat (f,".snd_ssthresh" , l),&err); +visit_type_int16_t (v,&tp->t_idle,strocat (f,".t_idle" , l),&err); +visit_type_int16_t (v,&tp->t_rtt,strocat (f,".t_rtt" , l),&err); +visit_type_uint32_t (v,&tp->t_rtseq,strocat (f,".t_rtseq" , l),&err); +visit_type_int16_t (v,&tp->t_srtt,strocat (f,".t_srtt" , l),&err); +visit_type_int16_t (v,&tp->t_rttvar,strocat (f,".t_rttvar" , l),&err); +visit_type_uint16_t (v,&tp->t_rttmin,strocat (f,".t_rttmin" , l),&err); +visit_type_uint32_t (v,&tp->max_sndwnd,strocat (f,".max_sndwnd" , l),&err); +visit_type_uint8_t (v, (uint8_t *)&tp->t_oobflags,strocat (f,".t_oobflags" , l),&err); +visit_type_uint8_t (v, (uint8_t *)&tp->t_iobc,strocat (f,".t_iobc" , l),&err); +visit_type_int16_t (v,&tp->t_softerror,strocat (f,".t_softerror" , l),&err); +visit_type_uint8_t (v,&tp->snd_scale,strocat (f,".snd_scale" , l),&err); +visit_type_uint8_t (v,&tp->rcv_scale,strocat (f,".rcv_scale" , l),&err); +visit_type_uint8_t (v,&tp->request_r_scale,strocat (f,".request_r_scale" , l),&err); +visit_type_uint8_t (v,&tp->requested_s_scale,strocat (f,".requested_s_scale" , l),&err); +visit_type_uint32_t (v,&tp->ts_recent,strocat (f,".ts_recent" , l),&err); +visit_type_uint32_t (v,&tp->ts_recent_age,strocat (f,".ts_recent_age" , l),&err); +visit_type_uint32_t (v,&tp->last_ack_sent,strocat (f,".last_ack_sent" , l),&err); } -static void slirp_sbuf_save (QEMUFile *f,struct sbuf *sbuf) +static void slirp_tcp_save (Visitor *v,struct tcpcb *tp,const char *pfield,Error *err) { -uint32_t off; +slirp_tcp_visit (v, tp, pfield, err); +} -qemu_put_be32 (f, sbuf->sb_cc); -qemu_put_be32 (f, sbuf->sb_datalen); +static void slirp_sbuf_save (Visitor *v,struct sbuf *sbuf,const char *pfield,Error *err) +{ +int32_t off; +char f[128 ]; +int i, l =0 ; + +if (pfield) { +assert (strlen (pfield)<sizeof (f)); +strcpy (f, pfield); + l =strlen (f); + } + +visit_type_uint32_t (v,&sbuf->sb_cc,strocat (f,".sb_cc" , l),&err); +visit_type_uint32_t (v,&sbuf->sb_datalen,strocat (f,".sb_datalen" , l),&err); off = (uint32_t )(sbuf->sb_wptr - sbuf->sb_data); -qemu_put_sbe32 (f, off); +visit_type_int32_t (v,&off,strocat (f,".sb_wptr_off" , l),&err); off = (uint32_t )(sbuf->sb_rptr - sbuf->sb_data); -qemu_put_sbe32 (f, off); -qemu_put_buffer (f, (unsigned char *)sbuf->sb_data, sbuf->sb_datalen); +visit_type_int32_t (v,&off,strocat (f,".sb_rptr_off" , l),&err); + +visit_start_array (v, (void **)&sbuf->sb_data,strocat (f,".sb_data" , l), + sbuf->sb_datalen,sizeof (*sbuf->sb_data),&err); +for (i =0 ; i< sbuf->sb_datalen; i++) { +visit_type_uint8_t (v, (uint8_t *)&sbuf->sb_data[i],NULL ,&err); + } +visit_end_array (v,&err); +} + +static void slirp_socket_visit (Visitor *v,struct socket *so,const char *pfield,Error *err) +{ +char f[64 ]; +int l =0 ; + +if (pfield) { +assert (strlen (pfield)<sizeof (f)); +strcpy (f, pfield); + l =strlen (f); + } +visit_type_int32_t (v,&so->so_urgc,strocat (f,".so_urgc" , l),&err); +visit_type_uint32_t (v,&so->so_faddr.s_addr,strocat (f,".so_faddr.s_addr" , l),&err); +visit_type_uint32_t (v,&so->so_laddr.s_addr,strocat (f,".so_urgc" , l),&err); +visit_type_uint16_t (v,&so->so_fport,strocat (f,".so_fport" , l),&err); +visit_type_uint16_t (v,&so->so_lport,strocat (f,".so_lport" , l),&err); +visit_type_uint8_t (v,&so->so_iptos,strocat (f,".so_iptos" , l),&err); +visit_type_uint8_t (v,&so->so_emu,strocat (f,".so_emu" , l),&err); +visit_type_uint8_t (v,&so->so_type,strocat (f,".so_type" , l),&err); +visit_type_int32_t (v,&so->so_state,strocat (f,".so_state" , l),&err); } -static void slirp_socket_save (QEMUFile *f,struct socket *so) +static void slirp_socket_save (Visitor *v,struct socket *so,const char *pfield,Error *err) { -qemu_put_be32 (f, so->so_urgc); -qemu_put_be32 (f, so->so_faddr.s_addr); -qemu_put_be32 (f, so->so_laddr.s_addr); -qemu_put_be16 (f, so->so_fport); -qemu_put_be16 (f, so->so_lport); -qemu_put_byte (f, so->so_iptos); -qemu_put_byte (f, so->so_emu); -qemu_put_byte (f, so->so_type); -qemu_put_be32 (f, so->so_state); -slirp_sbuf_save (f,&so->so_rcv); -slirp_sbuf_save (f,&so->so_snd); -slirp_tcp_save (f, so->so_tcpcb); +char f[64 ]; +int l =0 ; + +if (pfield) { +assert (strlen (pfield)<sizeof (f)); +strcpy (f, pfield); + l =strlen (f); + } + +slirp_socket_visit (v, so, f, err); + +slirp_sbuf_save (v,&so->so_rcv,strocat (f,".so_rcv" , l), err); +slirp_sbuf_save (v,&so->so_snd,strocat (f,".so_snd" , l), err); +slirp_tcp_save (v, so->so_tcpcb,strocat (f,".so_tcpcb" , l), err); } -static void slirp_bootp_save (QEMUFile *f,Slirp *slirp) +static void slirp_bootp_visit (Visitor *v,Slirp *slirp,const char *pfield,Error *err) { -int i; +int i, j, l; +void *ptr; +char f[64 ]; + +if (pfield) { +assert (strlen (pfield)<sizeof (f)); +strcpy (f, pfield); + l =strlen (f); + } + ptr = slirp->bootp_clients; +visit_start_array (v,&ptr,strocat (f,".bootp_clients" , l),NB_BOOTP_CLIENTS ,8 ,&err);for (i =0 ; i<NB_BOOTP_CLIENTS ; i++) { -qemu_put_be16 (f, slirp->bootp_clients[i].allocated); -qemu_put_buffer (f, slirp->bootp_clients[i].macaddr,6 ); +visit_type_uint16_t (v,&slirp->bootp_clients[i].allocated,"allocated" ,&err); + ptr = slirp->bootp_clients[i].macaddr; +visit_start_array (v,&ptr,"macaddr" ,6 ,1 ,&err); +for (j =0 ; j<6 ; j++) { +visit_type_uint8_t (v, (uint8_t *)&slirp->bootp_clients[j],NULL ,&err); + } +visit_end_array (v,&err); } +visit_end_array (v,&err); }static void slirp_state_save (QEMUFile *f,void *opaque) {Slirp *slirp = opaque;struct ex_list *ex_ptr; +int i =0 ; +uint8_t padding; +Visitor *v =qemu_file_get_output_visitor (f); +Error *err =NULL ; +char id[32 ];for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)if (ex_ptr->ex_pty ==3 ) {@@ -970,70 +1048,44 @@ static void slirp_state_save(QEMUFile *f, void *opaque)if (!so)continue ; -qemu_put_byte (f,42 ); -slirp_socket_save (f, so); + padding =SLIRP_DELIMITER ; +visit_type_uint8_t (v,&padding,"padding" ,&err); +slirp_socket_save (v, so,strocat (id,"so" ,strlen (id)), err); } -qemu_put_byte (f,0 ); + padding =0 ; +visit_type_uint8_t (v,&padding,"padding" ,&err); + +visit_type_uint16_t (v,&slirp->ip_id,"slirp.ip_id" ,&err); -qemu_put_be16 (f, slirp->ip_id); +slirp_bootp_visit (v, slirp,"slirp" , err); -slirp_bootp_save (f, slirp); +if (err) { +error_report ("error saving slirp state: %s" ,error_get_pretty (err)); +error_free (err); + } } -static void slirp_tcp_load (QEMUFile *f,struct tcpcb *tp) +static void slirp_tcp_load (Visitor *v,struct tcpcb *tp,const char *pfield,Error *err) { -int i; - - tp->t_state =qemu_get_sbe16 (f); -for (i =0 ; i<TCPT_NTIMERS ; i++) - tp->t_timer[i] =qemu_get_sbe16 (f); - tp->t_rxtshift =qemu_get_sbe16 (f); - tp->t_rxtcur =qemu_get_sbe16 (f); - tp->t_dupacks =qemu_get_sbe16 (f); - tp->t_maxseg =qemu_get_be16 (f); - tp->t_force =qemu_get_sbyte (f); - tp->t_flags =qemu_get_be16 (f); - tp->snd_una =qemu_get_be32 (f); - tp->snd_nxt =qemu_get_be32 (f); - tp->snd_up =qemu_get_be32 (f); - tp->snd_wl1 =qemu_get_be32 (f); - tp->snd_wl2 =qemu_get_be32 (f); - tp->iss =qemu_get_be32 (f); - tp->snd_wnd =qemu_get_be32 (f); - tp->rcv_wnd =qemu_get_be32 (f); - tp->rcv_nxt =qemu_get_be32 (f); - tp->rcv_up =qemu_get_be32 (f); - tp->irs =qemu_get_be32 (f); - tp->rcv_adv =qemu_get_be32 (f); - tp->snd_max =qemu_get_be32 (f); - tp->snd_cwnd =qemu_get_be32 (f); - tp->snd_ssthresh =qemu_get_be32 (f); - tp->t_idle =qemu_get_sbe16 (f); - tp->t_rtt =qemu_get_sbe16 (f); - tp->t_rtseq =qemu_get_be32 (f); - tp->t_srtt =qemu_get_sbe16 (f); - tp->t_rttvar =qemu_get_sbe16 (f); - tp->t_rttmin =qemu_get_be16 (f); - tp->max_sndwnd =qemu_get_be32 (f); - tp->t_oobflags =qemu_get_byte (f); - tp->t_iobc =qemu_get_byte (f); - tp->t_softerror =qemu_get_sbe16 (f); - tp->snd_scale =qemu_get_byte (f); - tp->rcv_scale =qemu_get_byte (f); - tp->request_r_scale =qemu_get_byte (f); - tp->requested_s_scale =qemu_get_byte (f); - tp->ts_recent =qemu_get_be32 (f); - tp->ts_recent_age =qemu_get_be32 (f); - tp->last_ack_sent =qemu_get_be32 (f); +slirp_tcp_visit (v, tp, pfield, err); +tcp_template (tp); } -static int slirp_sbuf_load (QEMUFile *f,struct sbuf *sbuf) +static int slirp_sbuf_load (Visitor *v,struct sbuf *sbuf,const char *pfield,Error *err) {uint32_t off, sb_cc, sb_datalen; +int l =0 , i; +char f[64 ]; + +if (pfield) { +assert (strlen (pfield)<sizeof (f)); +strcpy (f, pfield); + l =strlen (f); + } - sb_cc =qemu_get_be32 (f); - sb_datalen =qemu_get_be32 (f); +visit_type_uint32_t (v,&sb_cc,strocat (f,".sb_cc" , l),&err); +visit_type_uint32_t (v,&sb_datalen,strocat (f,".sb_datalen" , l),&err);sbreserve (sbuf, sb_datalen);@@ -1042,61 +1094,68 @@ static int slirp_sbuf_load(QEMUFile *f, struct sbuf *sbuf) sbuf->sb_cc = sb_cc; - off =qemu_get_sbe32 (f); +visit_type_uint32_t (v,&off,strocat (f,".sb_wptr_off" , l),&err); sbuf->sb_wptr = sbuf->sb_data + off; - off =qemu_get_sbe32 (f); +visit_type_uint32_t (v,&off,strocat (f,".sb_rptr_off" , l),&err); sbuf->sb_rptr = sbuf->sb_data + off; -qemu_get_buffer (f, (unsigned char *)sbuf->sb_data, sbuf->sb_datalen); + +visit_start_array (v, (void **)&sbuf->sb_data,strocat (f,".sb_data" , l), + sbuf->sb_datalen,sizeof (*sbuf->sb_data),&err); +for (i =0 ; i< sbuf->sb_datalen; i++) { +visit_type_uint8_t (v, (uint8_t *)&sbuf->sb_data[i],NULL ,&err); + } +visit_end_array (v,&err);return 0 ; } -static int slirp_socket_load (QEMUFile *f,struct socket *so) +static int slirp_socket_load (Visitor *v,struct socket *so,const char *pfield,Error *err) { +char f[64 ]; +int l =0 ; + +if (pfield) { +assert (strlen (pfield)<sizeof (f)); +strcpy (f, pfield); + l =strlen (f); + } + +assert (v);if (tcp_attach (so)<0 )return -ENOMEM ; - so->so_urgc =qemu_get_be32 (f); - so->so_faddr.s_addr =qemu_get_be32 (f); - so->so_laddr.s_addr =qemu_get_be32 (f); - so->so_fport =qemu_get_be16 (f); - so->so_lport =qemu_get_be16 (f); - so->so_iptos =qemu_get_byte (f); - so->so_emu =qemu_get_byte (f); - so->so_type =qemu_get_byte (f); - so->so_state =qemu_get_be32 (f); -if (slirp_sbuf_load (f,&so->so_rcv)<0 ) +slirp_socket_visit (v, so, pfield, err); + +if (slirp_sbuf_load (v,&so->so_rcv,strocat (f,".so_rcv" , l), err)<0 )return -ENOMEM ; -if (slirp_sbuf_load (f,&so->so_snd)<0 ) +if (slirp_sbuf_load (v,&so->so_snd,strocat (f,".so_snd" , l), err)<0 )return -ENOMEM ; -slirp_tcp_load (f, so->so_tcpcb); +slirp_tcp_load (v, so->so_tcpcb,strocat (f,".so_tcpcb" , l), err);return 0 ; } -static void slirp_bootp_load (QEMUFile *f,Slirp *slirp) -{ -int i; - -for (i =0 ; i<NB_BOOTP_CLIENTS ; i++) { - slirp->bootp_clients[i].allocated =qemu_get_be16 (f); -qemu_get_buffer (f, slirp->bootp_clients[i].macaddr,6 ); - } -} -static int slirp_state_load (QEMUFile *f,void *opaque,int version_id) {Slirp *slirp = opaque;struct ex_list *ex_ptr; +Visitor *v =qemu_file_get_input_visitor (f); +Error *err =NULL ; +char id[32 ]; +uint8_t padding, i =0 ; -while (qemu_get_byte (f)) { +visit_type_uint8_t (v,&padding,"padding" ,&err); + +while (padding ==SLIRP_DELIMITER ) {int ret;struct socket *so =socreate (slirp);if (!so)return -ENOMEM ; - ret =slirp_socket_load (f, so); +sprintf (id,"slirp[%d]" , i++); + + ret =slirp_socket_load (v, so,strocat (id,"so" ,strlen (id)), err);if (ret<0 )return ret;@@ -1118,12 +1177,19 @@ static int slirp_state_load(QEMUFile *f, void *opaque, int version_id) so->extra = (void *)ex_ptr->ex_exec; } +if (version_id>=2 ) { - slirp->ip_id =qemu_get_be16 (f); +visit_type_uint16_t (v,&slirp->ip_id,"slirp.ip_id" ,&err); }if (version_id>=3 ) { -slirp_bootp_load (f, slirp); +slirp_bootp_visit (v, slirp,"slirp" , err); + } + +if (err) { +error_report ("error loading slirp state: %s" ,error_get_pretty (err)); +error_free (err); +return -EINVAL ; }return 0 ;
Re: [Qemu-devel] [RFC 8/8] slirp: convert save/load function to visitor interface
Quoted 99 lines · Anthony Liguori
Quoted 84 lines · Michael Roth
Where possible common routines are used for both input and output, thus save on lines of code, theoretically. The added lines here are mostly due to extra logic for each save/load routine to manipulate strings into a unique field name for each saved field, and in some cases a few extra Visitor calls to due list/struct i/o. With some reworking we can probably optimize all of these to reduce the amount of added code.
Signed-off-by Michael Roth<[email protected]>--- slirp/slirp.c | 366 +++++++++++++++++++++++++++++++++----------------------- 1 files changed, 216 insertions(+), 150 deletions(-)slirp/slirp.c +12
-44@@ -26,6 +26,9 @@#include "qemu-char.h" #include "slirp.h" #include "hw/hw.h" +#include "qemu-error.h" + +#define SLIRP_DELIMITER 42/* used to separate slirp instances in save/load */ /* host loopback address */ struct in_addr loopback_addr;@@ -871,96 +874,171 @@ void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr, int guest_port,tcp_output (sototcpcb (so)); } -static void slirp_tcp_save (QEMUFile *f,struct tcpcb *tp) +static void slirp_tcp_visit (Visitor *v,struct tcpcb *tp,const char *pfield,Error *err) { -int i; - -qemu_put_sbe16 (f, tp->t_state); -for (i =0 ; i<TCPT_NTIMERS ; i++) -qemu_put_sbe16 (f, tp->t_timer[i]); -qemu_put_sbe16 (f, tp->t_rxtshift); -qemu_put_sbe16 (f, tp->t_rxtcur); -qemu_put_sbe16 (f, tp->t_dupacks); -qemu_put_be16 (f, tp->t_maxseg); -qemu_put_sbyte (f, tp->t_force); -qemu_put_be16 (f, tp->t_flags); -qemu_put_be32 (f, tp->snd_una); -qemu_put_be32 (f, tp->snd_nxt); -qemu_put_be32 (f, tp->snd_up); -qemu_put_be32 (f, tp->snd_wl1); -qemu_put_be32 (f, tp->snd_wl2); -qemu_put_be32 (f, tp->iss); -qemu_put_be32 (f, tp->snd_wnd); -qemu_put_be32 (f, tp->rcv_wnd); -qemu_put_be32 (f, tp->rcv_nxt); -qemu_put_be32 (f, tp->rcv_up); -qemu_put_be32 (f, tp->irs); -qemu_put_be32 (f, tp->rcv_adv); -qemu_put_be32 (f, tp->snd_max); -qemu_put_be32 (f, tp->snd_cwnd); -qemu_put_be32 (f, tp->snd_ssthresh); -qemu_put_sbe16 (f, tp->t_idle); -qemu_put_sbe16 (f, tp->t_rtt); -qemu_put_be32 (f, tp->t_rtseq); -qemu_put_sbe16 (f, tp->t_srtt); -qemu_put_sbe16 (f, tp->t_rttvar); -qemu_put_be16 (f, tp->t_rttmin); -qemu_put_be32 (f, tp->max_sndwnd); -qemu_put_byte (f, tp->t_oobflags); -qemu_put_byte (f, tp->t_iobc); -qemu_put_sbe16 (f, tp->t_softerror); -qemu_put_byte (f, tp->snd_scale); -qemu_put_byte (f, tp->rcv_scale); -qemu_put_byte (f, tp->request_r_scale); -qemu_put_byte (f, tp->requested_s_scale); -qemu_put_be32 (f, tp->ts_recent); -qemu_put_be32 (f, tp->ts_recent_age); -qemu_put_be32 (f, tp->last_ack_sent); +char f[128 ]; +int i, l =0 ; +int16_t *ptr; + +if (pfield) { +assert (strlen (pfield)<sizeof (f)); +strcpy (f, pfield); + l =strlen (pfield);slirp/slirp.c void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr, int guest_port, · line 896
Quoted 10 lines
slirp/slirp.c +10
-0+ } + +visit_type_int16_t (v,&tp->t_state,strocat (f,".t_state" , l),&err); + ptr = tp->t_timer; +visit_start_array (v, (void **)&ptr,strocat (f,".t_timer" , l),TCPT_NTIMERS ,sizeof (*ptr),&err); +for (i =0 ; i<TCPT_NTIMERS ; ++i) { +visit_type_int16_t (v,&ptr[i],NULL ,&err); + } +visit_end_array (v,&err); +visit_type_int16_t (v,&tp->t_rxtshift,strocat (f,".t_rxtshift" , l),&err);Hrm, you should never concat a name like this. A better approach would be:
Quoted 6 lines
visit_start_struct(v, NULL, f);visit_type_int16_t(v, &tp->t_rxtshift, "t_rxtshift", &err);
...visit_end_struct(v, NULL, f);structs indicate hierarchy. They don't have to correspond to real types.
Yeah, I thought a lot about using visit_start_struct purely for namespacing, but wasn't sure if that was too hackish. I like it a whole lot better than manipulating the field names though... I'll rework it and see.
Quoted 394 lines
Regards,
Anthony Liguori
Quoted 388 lines
slirp/slirp.c +194
-106+visit_type_int16_t (v,&tp->t_rxtcur,strocat (f,".t_rxtcur" , l),&err); +visit_type_int16_t (v,&tp->t_dupacks,strocat (f,".t_dupacks" , l),&err); +visit_type_uint16_t (v,&tp->t_maxseg,strocat (f,".t_maxseg" , l),&err); +visit_type_uint8_t (v, (uint8_t *)&tp->t_force,strocat (f,".t_force" , l),&err); +visit_type_uint16_t (v,&tp->t_flags,strocat (f,".t_flags" , l),&err); +visit_type_uint32_t (v,&tp->snd_una,strocat (f,".snd_una" , l),&err); +visit_type_uint32_t (v,&tp->snd_nxt,strocat (f,".snd_nxt" , l),&err); +visit_type_uint32_t (v,&tp->snd_up,strocat (f,".snd_up" , l),&err); +visit_type_uint32_t (v,&tp->snd_wl1,strocat (f,".snd_wl1" , l),&err); +visit_type_uint32_t (v,&tp->snd_wl2,strocat (f,".snd_wl2" , l),&err); +visit_type_uint32_t (v,&tp->iss,strocat (f,".iss" , l),&err); +visit_type_uint32_t (v,&tp->snd_wnd,strocat (f,".snd_wnd" , l),&err); +visit_type_uint32_t (v,&tp->rcv_wnd,strocat (f,".rcv_wnd" , l),&err); +visit_type_uint32_t (v,&tp->rcv_nxt,strocat (f,".rcv_nxt" , l),&err); +visit_type_uint32_t (v,&tp->rcv_up,strocat (f,".rcv_up" , l),&err); +visit_type_uint32_t (v,&tp->irs,strocat (f,".irs" , l),&err); +visit_type_uint32_t (v,&tp->rcv_adv,strocat (f,".rcv_adv" , l),&err); +visit_type_uint32_t (v,&tp->snd_max,strocat (f,".snd_max" , l),&err); +visit_type_uint32_t (v,&tp->snd_cwnd,strocat (f,".snd_cwnd" , l),&err); +visit_type_uint32_t (v,&tp->snd_ssthresh,strocat (f,".snd_ssthresh" , l),&err); +visit_type_int16_t (v,&tp->t_idle,strocat (f,".t_idle" , l),&err); +visit_type_int16_t (v,&tp->t_rtt,strocat (f,".t_rtt" , l),&err); +visit_type_uint32_t (v,&tp->t_rtseq,strocat (f,".t_rtseq" , l),&err); +visit_type_int16_t (v,&tp->t_srtt,strocat (f,".t_srtt" , l),&err); +visit_type_int16_t (v,&tp->t_rttvar,strocat (f,".t_rttvar" , l),&err); +visit_type_uint16_t (v,&tp->t_rttmin,strocat (f,".t_rttmin" , l),&err); +visit_type_uint32_t (v,&tp->max_sndwnd,strocat (f,".max_sndwnd" , l),&err); +visit_type_uint8_t (v, (uint8_t *)&tp->t_oobflags,strocat (f,".t_oobflags" , l),&err); +visit_type_uint8_t (v, (uint8_t *)&tp->t_iobc,strocat (f,".t_iobc" , l),&err); +visit_type_int16_t (v,&tp->t_softerror,strocat (f,".t_softerror" , l),&err); +visit_type_uint8_t (v,&tp->snd_scale,strocat (f,".snd_scale" , l),&err); +visit_type_uint8_t (v,&tp->rcv_scale,strocat (f,".rcv_scale" , l),&err); +visit_type_uint8_t (v,&tp->request_r_scale,strocat (f,".request_r_scale" , l),&err); +visit_type_uint8_t (v,&tp->requested_s_scale,strocat (f,".requested_s_scale" , l),&err); +visit_type_uint32_t (v,&tp->ts_recent,strocat (f,".ts_recent" , l),&err); +visit_type_uint32_t (v,&tp->ts_recent_age,strocat (f,".ts_recent_age" , l),&err); +visit_type_uint32_t (v,&tp->last_ack_sent,strocat (f,".last_ack_sent" , l),&err); } -static void slirp_sbuf_save (QEMUFile *f,struct sbuf *sbuf) +static void slirp_tcp_save (Visitor *v,struct tcpcb *tp,const char *pfield,Error *err) { -uint32_t off; +slirp_tcp_visit (v, tp, pfield, err); +} -qemu_put_be32 (f, sbuf->sb_cc); -qemu_put_be32 (f, sbuf->sb_datalen); +static void slirp_sbuf_save (Visitor *v,struct sbuf *sbuf,const char *pfield,Error *err) +{ +int32_t off; +char f[128 ]; +int i, l =0 ; + +if (pfield) { +assert (strlen (pfield)<sizeof (f)); +strcpy (f, pfield); + l =strlen (f); + } + +visit_type_uint32_t (v,&sbuf->sb_cc,strocat (f,".sb_cc" , l),&err); +visit_type_uint32_t (v,&sbuf->sb_datalen,strocat (f,".sb_datalen" , l),&err); off = (uint32_t )(sbuf->sb_wptr - sbuf->sb_data); -qemu_put_sbe32 (f, off); +visit_type_int32_t (v,&off,strocat (f,".sb_wptr_off" , l),&err); off = (uint32_t )(sbuf->sb_rptr - sbuf->sb_data); -qemu_put_sbe32 (f, off); -qemu_put_buffer (f, (unsigned char *)sbuf->sb_data, sbuf->sb_datalen); +visit_type_int32_t (v,&off,strocat (f,".sb_rptr_off" , l),&err); + +visit_start_array (v, (void **)&sbuf->sb_data,strocat (f,".sb_data" , l), + sbuf->sb_datalen,sizeof (*sbuf->sb_data),&err); +for (i =0 ; i< sbuf->sb_datalen; i++) { +visit_type_uint8_t (v, (uint8_t *)&sbuf->sb_data[i],NULL ,&err); + } +visit_end_array (v,&err); +} + +static void slirp_socket_visit (Visitor *v,struct socket *so,const char *pfield,Error *err) +{ +char f[64 ]; +int l =0 ; + +if (pfield) { +assert (strlen (pfield)<sizeof (f)); +strcpy (f, pfield); + l =strlen (f); + } +visit_type_int32_t (v,&so->so_urgc,strocat (f,".so_urgc" , l),&err); +visit_type_uint32_t (v,&so->so_faddr.s_addr,strocat (f,".so_faddr.s_addr" , l),&err); +visit_type_uint32_t (v,&so->so_laddr.s_addr,strocat (f,".so_urgc" , l),&err); +visit_type_uint16_t (v,&so->so_fport,strocat (f,".so_fport" , l),&err); +visit_type_uint16_t (v,&so->so_lport,strocat (f,".so_lport" , l),&err); +visit_type_uint8_t (v,&so->so_iptos,strocat (f,".so_iptos" , l),&err); +visit_type_uint8_t (v,&so->so_emu,strocat (f,".so_emu" , l),&err); +visit_type_uint8_t (v,&so->so_type,strocat (f,".so_type" , l),&err); +visit_type_int32_t (v,&so->so_state,strocat (f,".so_state" , l),&err); } -static void slirp_socket_save (QEMUFile *f,struct socket *so) +static void slirp_socket_save (Visitor *v,struct socket *so,const char *pfield,Error *err) { -qemu_put_be32 (f, so->so_urgc); -qemu_put_be32 (f, so->so_faddr.s_addr); -qemu_put_be32 (f, so->so_laddr.s_addr); -qemu_put_be16 (f, so->so_fport); -qemu_put_be16 (f, so->so_lport); -qemu_put_byte (f, so->so_iptos); -qemu_put_byte (f, so->so_emu); -qemu_put_byte (f, so->so_type); -qemu_put_be32 (f, so->so_state); -slirp_sbuf_save (f,&so->so_rcv); -slirp_sbuf_save (f,&so->so_snd); -slirp_tcp_save (f, so->so_tcpcb); +char f[64 ]; +int l =0 ; + +if (pfield) { +assert (strlen (pfield)<sizeof (f)); +strcpy (f, pfield); + l =strlen (f); + } + +slirp_socket_visit (v, so, f, err); + +slirp_sbuf_save (v,&so->so_rcv,strocat (f,".so_rcv" , l), err); +slirp_sbuf_save (v,&so->so_snd,strocat (f,".so_snd" , l), err); +slirp_tcp_save (v, so->so_tcpcb,strocat (f,".so_tcpcb" , l), err); } -static void slirp_bootp_save (QEMUFile *f,Slirp *slirp) +static void slirp_bootp_visit (Visitor *v,Slirp *slirp,const char *pfield,Error *err) { -int i; +int i, j, l; +void *ptr; +char f[64 ]; + +if (pfield) { +assert (strlen (pfield)<sizeof (f)); +strcpy (f, pfield); + l =strlen (f); + } + ptr = slirp->bootp_clients; +visit_start_array (v,&ptr,strocat (f,".bootp_clients" , l),NB_BOOTP_CLIENTS ,8 ,&err);for (i =0 ; i<NB_BOOTP_CLIENTS ; i++) { -qemu_put_be16 (f, slirp->bootp_clients[i].allocated); -qemu_put_buffer (f, slirp->bootp_clients[i].macaddr,6 ); +visit_type_uint16_t (v,&slirp->bootp_clients[i].allocated,"allocated" ,&err); + ptr = slirp->bootp_clients[i].macaddr; +visit_start_array (v,&ptr,"macaddr" ,6 ,1 ,&err); +for (j =0 ; j<6 ; j++) { +visit_type_uint8_t (v, (uint8_t *)&slirp->bootp_clients[j],NULL ,&err); + } +visit_end_array (v,&err); } +visit_end_array (v,&err); }static void slirp_state_save (QEMUFile *f,void *opaque) {Slirp *slirp = opaque;struct ex_list *ex_ptr; +int i =0 ; +uint8_t padding; +Visitor *v =qemu_file_get_output_visitor (f); +Error *err =NULL ; +char id[32 ];for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)if (ex_ptr->ex_pty ==3 ) {@@ -970,70 +1048,44 @@ static void slirp_state_save(QEMUFile *f, void *opaque)if (!so)continue ; -qemu_put_byte (f,42 ); -slirp_socket_save (f, so); + padding =SLIRP_DELIMITER ; +visit_type_uint8_t (v,&padding,"padding" ,&err); +slirp_socket_save (v, so,strocat (id,"so" ,strlen (id)), err); } -qemu_put_byte (f,0 ); + padding =0 ; +visit_type_uint8_t (v,&padding,"padding" ,&err); + +visit_type_uint16_t (v,&slirp->ip_id,"slirp.ip_id" ,&err); -qemu_put_be16 (f, slirp->ip_id); +slirp_bootp_visit (v, slirp,"slirp" , err); -slirp_bootp_save (f, slirp); +if (err) { +error_report ("error saving slirp state: %s" ,error_get_pretty (err)); +error_free (err); + } } -static void slirp_tcp_load (QEMUFile *f,struct tcpcb *tp) +static void slirp_tcp_load (Visitor *v,struct tcpcb *tp,const char *pfield,Error *err) { -int i; - - tp->t_state =qemu_get_sbe16 (f); -for (i =0 ; i<TCPT_NTIMERS ; i++) - tp->t_timer[i] =qemu_get_sbe16 (f); - tp->t_rxtshift =qemu_get_sbe16 (f); - tp->t_rxtcur =qemu_get_sbe16 (f); - tp->t_dupacks =qemu_get_sbe16 (f); - tp->t_maxseg =qemu_get_be16 (f); - tp->t_force =qemu_get_sbyte (f); - tp->t_flags =qemu_get_be16 (f); - tp->snd_una =qemu_get_be32 (f); - tp->snd_nxt =qemu_get_be32 (f); - tp->snd_up =qemu_get_be32 (f); - tp->snd_wl1 =qemu_get_be32 (f); - tp->snd_wl2 =qemu_get_be32 (f); - tp->iss =qemu_get_be32 (f); - tp->snd_wnd =qemu_get_be32 (f); - tp->rcv_wnd =qemu_get_be32 (f); - tp->rcv_nxt =qemu_get_be32 (f); - tp->rcv_up =qemu_get_be32 (f); - tp->irs =qemu_get_be32 (f); - tp->rcv_adv =qemu_get_be32 (f); - tp->snd_max =qemu_get_be32 (f); - tp->snd_cwnd =qemu_get_be32 (f); - tp->snd_ssthresh =qemu_get_be32 (f); - tp->t_idle =qemu_get_sbe16 (f); - tp->t_rtt =qemu_get_sbe16 (f); - tp->t_rtseq =qemu_get_be32 (f); - tp->t_srtt =qemu_get_sbe16 (f); - tp->t_rttvar =qemu_get_sbe16 (f); - tp->t_rttmin =qemu_get_be16 (f); - tp->max_sndwnd =qemu_get_be32 (f); - tp->t_oobflags =qemu_get_byte (f); - tp->t_iobc =qemu_get_byte (f); - tp->t_softerror =qemu_get_sbe16 (f); - tp->snd_scale =qemu_get_byte (f); - tp->rcv_scale =qemu_get_byte (f); - tp->request_r_scale =qemu_get_byte (f); - tp->requested_s_scale =qemu_get_byte (f); - tp->ts_recent =qemu_get_be32 (f); - tp->ts_recent_age =qemu_get_be32 (f); - tp->last_ack_sent =qemu_get_be32 (f); +slirp_tcp_visit (v, tp, pfield, err); +tcp_template (tp); } -static int slirp_sbuf_load (QEMUFile *f,struct sbuf *sbuf) +static int slirp_sbuf_load (Visitor *v,struct sbuf *sbuf,const char *pfield,Error *err) {uint32_t off, sb_cc, sb_datalen; +int l =0 , i; +char f[64 ]; + +if (pfield) { +assert (strlen (pfield)<sizeof (f)); +strcpy (f, pfield); + l =strlen (f); + } - sb_cc =qemu_get_be32 (f); - sb_datalen =qemu_get_be32 (f); +visit_type_uint32_t (v,&sb_cc,strocat (f,".sb_cc" , l),&err); +visit_type_uint32_t (v,&sb_datalen,strocat (f,".sb_datalen" , l),&err);sbreserve (sbuf, sb_datalen);@@ -1042,61 +1094,68 @@ static int slirp_sbuf_load(QEMUFile *f, struct sbuf *sbuf) sbuf->sb_cc = sb_cc; - off =qemu_get_sbe32 (f); +visit_type_uint32_t (v,&off,strocat (f,".sb_wptr_off" , l),&err); sbuf->sb_wptr = sbuf->sb_data + off; - off =qemu_get_sbe32 (f); +visit_type_uint32_t (v,&off,strocat (f,".sb_rptr_off" , l),&err); sbuf->sb_rptr = sbuf->sb_data + off; -qemu_get_buffer (f, (unsigned char *)sbuf->sb_data, sbuf->sb_datalen); + +visit_start_array (v, (void **)&sbuf->sb_data,strocat (f,".sb_data" , l), + sbuf->sb_datalen,sizeof (*sbuf->sb_data),&err); +for (i =0 ; i< sbuf->sb_datalen; i++) { +visit_type_uint8_t (v, (uint8_t *)&sbuf->sb_data[i],NULL ,&err); + } +visit_end_array (v,&err);return 0 ; } -static int slirp_socket_load (QEMUFile *f,struct socket *so) +static int slirp_socket_load (Visitor *v,struct socket *so,const char *pfield,Error *err) { +char f[64 ]; +int l =0 ; + +if (pfield) { +assert (strlen (pfield)<sizeof (f)); +strcpy (f, pfield); + l =strlen (f); + } + +assert (v);if (tcp_attach (so)<0 )return -ENOMEM ; - so->so_urgc =qemu_get_be32 (f); - so->so_faddr.s_addr =qemu_get_be32 (f); - so->so_laddr.s_addr =qemu_get_be32 (f); - so->so_fport =qemu_get_be16 (f); - so->so_lport =qemu_get_be16 (f); - so->so_iptos =qemu_get_byte (f); - so->so_emu =qemu_get_byte (f); - so->so_type =qemu_get_byte (f); - so->so_state =qemu_get_be32 (f); -if (slirp_sbuf_load (f,&so->so_rcv)<0 ) +slirp_socket_visit (v, so, pfield, err); + +if (slirp_sbuf_load (v,&so->so_rcv,strocat (f,".so_rcv" , l), err)<0 )return -ENOMEM ; -if (slirp_sbuf_load (f,&so->so_snd)<0 ) +if (slirp_sbuf_load (v,&so->so_snd,strocat (f,".so_snd" , l), err)<0 )return -ENOMEM ; -slirp_tcp_load (f, so->so_tcpcb); +slirp_tcp_load (v, so->so_tcpcb,strocat (f,".so_tcpcb" , l), err);return 0 ; } -static void slirp_bootp_load (QEMUFile *f,Slirp *slirp) -{ -int i; - -for (i =0 ; i<NB_BOOTP_CLIENTS ; i++) { - slirp->bootp_clients[i].allocated =qemu_get_be16 (f); -qemu_get_buffer (f, slirp->bootp_clients[i].macaddr,6 ); - } -} -static int slirp_state_load (QEMUFile *f,void *opaque,int version_id) {Slirp *slirp = opaque;struct ex_list *ex_ptr; +Visitor *v =qemu_file_get_input_visitor (f); +Error *err =NULL ; +char id[32 ]; +uint8_t padding, i =0 ; -while (qemu_get_byte (f)) { +visit_type_uint8_t (v,&padding,"padding" ,&err); + +while (padding ==SLIRP_DELIMITER ) {int ret;struct socket *so =socreate (slirp);if (!so)return -ENOMEM ; - ret =slirp_socket_load (f, so); +sprintf (id,"slirp[%d]" , i++); + + ret =slirp_socket_load (v, so,strocat (id,"so" ,strlen (id)), err);if (ret<0 )return ret;@@ -1118,12 +1177,19 @@ static int slirp_state_load(QEMUFile *f, void *opaque, int version_id) so->extra = (void *)ex_ptr->ex_exec; } +if (version_id>=2 ) { - slirp->ip_id =qemu_get_be16 (f); +visit_type_uint16_t (v,&slirp->ip_id,"slirp.ip_id" ,&err); }if (version_id>=3 ) { -slirp_bootp_load (f, slirp); +slirp_bootp_visit (v, slirp,"slirp" , err); + } + +if (err) { +error_report ("error loading slirp state: %s" ,error_get_pretty (err)); +error_free (err); +return -EINVAL ; }return 0 ;
Signature · 3 lines
Sincerely, Mike Roth IBM Linux Technology Center
Quoted 6 lines
4) Implement the BERVisitor and make this the default migration protocol.
Most of the work will be in 1), though with the implementation in this series we should be able to do it incrementally. I'm not sure if the best approach is doing the mechanical phase 1 conversion, then doing phase 2 sometime after 4), doing phase 1 + 2 as part of 1), or just doing VMState conversions which gives basically the same capabilities as phase 1 + 2.
Thoughts?
Is anyone working on this? If not I may give it a shot (tomorrow++) for at least some of the primitives... for enabling vNVRAM metadata of course. Indefinite length encoding of constructed data types I suppose won't be used otherwise the visitor interface seems wrong for parsing and skipping of extra data towards the end of a structure if version n wrote the stream and appended some of its version n data and now version m < n is trying to read the struct and needs to skip the version [m+1, n ] data fields ... in that case the de-serialization of the stream should probably be stream-driven rather than structure-driven.
Stefan
Quoted 17 lines · Stefan Berger
Quoted 6 lines
4) Implement the BERVisitor and make this the default migration protocol.
Most of the work will be in 1), though with the implementation in this series we should be able to do it incrementally. I'm not sure if the best approach is doing the mechanical phase 1 conversion, then doing phase 2 sometime after 4), doing phase 1 + 2 as part of 1), or just doing VMState conversions which gives basically the same capabilities as phase 1 + 2.
Thoughts?
Is anyone working on this? If not I may give it a shot (tomorrow++) for at least some of the primitives... for enabling vNVRAM metadata of course. Indefinite length encoding of constructed data types I suppose won't be used otherwise the visitor interface seems wrong for parsing and skipping of extra data towards the end of a structure if version n wrote the stream and appended some of its version n data and now version m < n is trying to read the struct and needs to skip the version [m+1, n ] data fields ... in that case the de-serialization of the stream should probably be stream-driven rather than structure-driven.
Quoted 1 line
Stefan
Yes I've been struggling with that exactly.
Anthony, any thoughts?
Signature · 1 line
MST
Quoted 52 lines · Michael Roth
OVERVIEW
This patch series implements a QEMUFile Visitor class that's intended to abstract away direct calls to qemu_put_*/qemu_get_* for save/load functions. Currently this is done by always creating a QEMUFileInputVisitor/QEMUFileOutputVisitor pair with each call to
qemu_fopen_ops()and maintaining a QEMUFile->I/O Visitor mapping. save/load functions that are to be converted would them simply use lookup routines to get a Visitor based on their QEMUFile arguments. Once these save/load functions are all converted, we can then change the interfaces in bulk and switch over to passing in the Visitor directly.An example conversion of Slirp, which still uses old-style save/load routines, is included as part of this series. Anthony I believe has VMState converted over to using Visitors, so theoretically all VMStatified devices are good to go (Anthony: if that's the case feel free to rebase on this or point me to the repo to pull in, and I'll work off the resulting branch).
PLANS
Ultimately, the goal is to implement a new migration protocol that is self-describing, such that incompatibilities or migration errors can be more easilly detected. Currently, a simple change in data types for a particular device can introduce subtle bugs that won't be detected by the target, since the target interprets the data according to it's own expectation of what those data types are. Currently the plan is to use ASN.1 BER in place of QEMUFile.
By using a Visitor interface for migration, we also gain the ability to generate a migration schema (via, say, a JSONSchemaVisitor). This is similar to the VMState schema generator in Anthony's "Add live migration unit tests" series (http://thread.gmane.org/gmane.comp.emulators.qemu/97754/focus=97754), except that it is applicable to both VMState and old-style save/load functions.
There is still quite a bit a work to get to that point. Anthony addressed some of the issues on the VMState side of things in the aforementioned series, and I believe he already has some patches that convert VMState over to using visitors insteads of qemu_put_*/qemu_get_* directly. That being the case, what's left is:
1) Convert old-style save/load functions over to using visitors. I'm not sure what the best approach is for this. We basically have 2 options: either by converting them to VMState, or converting the functions over to using visitors, such as with the example slirp conversion. Even if we do option 2 it would likely need to be done in 2 phases:
phase 1: a mechanical conversion where we basically replace each qemu_put*/qemu_get* with a visit_*. This allows use to plop in a new, say BERVisitor to switch to using the new migration protocol. It should also be possible to do this without breaking migration compatibility with older versions. It does not however result in a well-specified schema if we were to plop in, say, a JSONSchemaVisitor. This is where we need phase 2.
phase 2: Currently, runtime state modifies our "schema" the way things are currently done. Slirp instance and virtqueue enumeration, for instance:
slirp/slirp.c:for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) if (ex_ptr->ex_pty == 3) { struct socket *so; so = slirp_find_ctl_socket(slirp, ex_ptr->ex_addr, ntohs(ex_ptr->ex_fport)); if (!so) continue; qemu_put_byte(f, 42); slirp_socket_save(f, so); }
hw/virtio.c:for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) { if (vdev->vq[i].vring.num == 0) break; qemu_put_be32(f, vdev->vq[i].vring.num); qemu_put_be64(f, vdev->vq[i].pa); qemu_put_be16s(f, &vdev->vq[i].last_avail_idx); if (vdev->binding->save_queue) vdev->binding->save_queue(vdev->binding_opaque, i, f); }To be able to build a schema around this we'd need to do a
visit_start_array()orvisit_start_list(), to denote a list of entries, and ideally we'd also describe structure of these entries. But often there is no structure of these entries...they may just be values taken from various data structures. So, either we do hacky things like using visitor intefaces to create "fake" structs (we say we're dealing with a struct when we're really not), or we actually put these into an intermediate struct which we then use to assign to store/load migration fields from.phase 2 sounds a whole lot like converting over to VMState. So I think we'll only want to go with option 2 in places where converting to VMState is not practical/planned. Or we can hold off on the phase 2 stuff and just focus on getting the new protocol in place ASAP. After which, we can rework things to support generating a well-specified schema.
2) Once the conversion stuff is done, we can modify the save/load interfaces in bulk to accept a Visitor in place of a QEMUFile (but continue using the old protocol/QEMUFile*Visitor for the time being)
Quoted 7 lines
3) Implement migration capabilities negotiation. This was discussed to some extent in the XBZRLE v4 thread (http://lists.gnu.org/archive/html/qemu-devel/2011-08/msg01155.html), basically having a set of migration capabilities that can be queried via QMP. Management tools would then choose the optimal migration settings from the intersection of these.
4) Implement the BERVisitor and make this the default migration protocol.
Most of the work will be in 1), though with the implementation in this series we should be able to do it incrementally. I'm not sure if the best approach is doing the mechanical phase 1 conversion, then doing phase 2 sometime after 4), doing phase 1 + 2 as part of 1), or just doing VMState conversions which gives basically the same capabilities as phase 1 + 2.
Thoughts?
Yes, I think this creates much more work than an upfront conversion of the protocol to BER, then cleaning up interfaces, would be.
Here are some suggestions:
- Let's make the protocol be BER directly. As a first step, use a single octet string for the whole of data. Next, start splitting this up.
- Don't try to use arrays at all. They don't make sense in either jason or asn. Instead, use sets for unordered data and sequences for ordered data.
- Most data should be unordered: we don't normally care in which order we get device attributes.
So we would have set of sequences, and
each sequence is attribute name (ascii string) + attribute data
- This does not discuss how we will handle cross version migration in a lot of detail. Presumably you expect capability magic to work for this. However this does not work for migration to file. A self describing format makes a much simpler solution possible:
separating attributes which destination must parse from those it can skip safely. If destination sees an attribute
that was marked as must parse and does not recognize it, migration fails. If not, just skip the attribute.
Signature · 1 line
MST
Quoted 61 lines · Michael S. Tsirkin
Quoted 52 lines · Michael Roth
OVERVIEW
This patch series implements a QEMUFile Visitor class that's intended to abstract away direct calls to qemu_put_*/qemu_get_* for save/load functions. Currently this is done by always creating a QEMUFileInputVisitor/QEMUFileOutputVisitor pair with each call to
qemu_fopen_ops()and maintaining a QEMUFile->I/O Visitor mapping. save/load functions that are to be converted would them simply use lookup routines to get a Visitor based on their QEMUFile arguments. Once these save/load functions are all converted, we can then change the interfaces in bulk and switch over to passing in the Visitor directly.An example conversion of Slirp, which still uses old-style save/load routines, is included as part of this series. Anthony I believe has VMState converted over to using Visitors, so theoretically all VMStatified devices are good to go (Anthony: if that's the case feel free to rebase on this or point me to the repo to pull in, and I'll work off the resulting branch).
PLANS
Ultimately, the goal is to implement a new migration protocol that is self-describing, such that incompatibilities or migration errors can be more easilly detected. Currently, a simple change in data types for a particular device can introduce subtle bugs that won't be detected by the target, since the target interprets the data according to it's own expectation of what those data types are. Currently the plan is to use ASN.1 BER in place of QEMUFile.
By using a Visitor interface for migration, we also gain the ability to generate a migration schema (via, say, a JSONSchemaVisitor). This is similar to the VMState schema generator in Anthony's "Add live migration unit tests" series (http://thread.gmane.org/gmane.comp.emulators.qemu/97754/focus=97754), except that it is applicable to both VMState and old-style save/load functions.
There is still quite a bit a work to get to that point. Anthony addressed some of the issues on the VMState side of things in the aforementioned series, and I believe he already has some patches that convert VMState over to using visitors insteads of qemu_put_*/qemu_get_* directly. That being the case, what's left is:
1) Convert old-style save/load functions over to using visitors. I'm not sure what the best approach is for this. We basically have 2 options: either by converting them to VMState, or converting the functions over to using visitors, such as with the example slirp conversion. Even if we do option 2 it would likely need to be done in 2 phases:
phase 1: a mechanical conversion where we basically replace each qemu_put*/qemu_get* with a visit_*. This allows use to plop in a new, say BERVisitor to switch to using the new migration protocol. It should also be possible to do this without breaking migration compatibility with older versions. It does not however result in a well-specified schema if we were to plop in, say, a JSONSchemaVisitor. This is where we need phase 2.
phase 2: Currently, runtime state modifies our "schema" the way things are currently done. Slirp instance and virtqueue enumeration, for instance:
slirp/slirp.c:for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) if (ex_ptr->ex_pty == 3) { struct socket *so; so = slirp_find_ctl_socket(slirp, ex_ptr->ex_addr, ntohs(ex_ptr->ex_fport)); if (!so) continue; qemu_put_byte(f, 42); slirp_socket_save(f, so); }
hw/virtio.c:for (i = 0; i< VIRTIO_PCI_QUEUE_MAX; i++) { if (vdev->vq[i].vring.num == 0) break; qemu_put_be32(f, vdev->vq[i].vring.num); qemu_put_be64(f, vdev->vq[i].pa); qemu_put_be16s(f,&vdev->vq[i].last_avail_idx); if (vdev->binding->save_queue) vdev->binding->save_queue(vdev->binding_opaque, i, f); }To be able to build a schema around this we'd need to do a
visit_start_array()orvisit_start_list(), to denote a list of entries, and ideally we'd also describe structure of these entries. But often there is no structure of these entries...they may just be values taken from various data structures. So, either we do hacky things like using visitor intefaces to create "fake" structs (we say we're dealing with a struct when we're really not), or we actually put these into an intermediate struct which we then use to assign to store/load migration fields from.phase 2 sounds a whole lot like converting over to VMState. So I think we'll only want to go with option 2 in places where converting to VMState is not practical/planned. Or we can hold off on the phase 2 stuff and just focus on getting the new protocol in place ASAP. After which, we can rework things to support generating a well-specified schema.
2) Once the conversion stuff is done, we can modify the save/load interfaces in bulk to accept a Visitor in place of a QEMUFile (but continue using the old protocol/QEMUFile*Visitor for the time being)
Quoted 7 lines
3) Implement migration capabilities negotiation. This was discussed to some extent in the XBZRLE v4 thread (http://lists.gnu.org/archive/html/qemu-devel/2011-08/msg01155.html), basically having a set of migration capabilities that can be queried via QMP. Management tools would then choose the optimal migration settings from the intersection of these.
4) Implement the BERVisitor and make this the default migration protocol.
Most of the work will be in 1), though with the implementation in this series we should be able to do it incrementally. I'm not sure if the best approach is doing the mechanical phase 1 conversion, then doing phase 2 sometime after 4), doing phase 1 + 2 as part of 1), or just doing VMState conversions which gives basically the same capabilities as phase 1 + 2.
Thoughts?
Quoted 8 lines
Yes, I think this creates much more work than an upfront conversion of the protocol to BER, then cleaning up interfaces, would be.
Here are some suggestions:
- Let's make the protocol be BER directly. As a first step, use a single octet string for the whole of data. Next, start splitting this up.
This can't be done without breaking the old style migration protocol. I don't have a problem with that but I do have a problem with repeatedly breaking migration protocol.
The Visitor interface is very little abstraction over a native BER interface. It lets us introduce BER incrementally while still supporting the old protocol.
Quoted 4 lines
- Don't try to use arrays at all. They don't make sense in either jason or asn. Instead, use sets for unordered data and sequences for ordered data.
sequences map to visit_start_array() pretty well. You would do something like:
visit_start_array(v, "entries", errp);
for (int i = 0; i < s->size; i++) {
visit_type_int(v, NULL, &s->entry[i], errp);
}visit_end_array(v, errp);
Quoted 4 lines
- Most data should be unordered: we don't normally care in which order we get device attributes.
So we would have set of sequences, and
each sequence is attribute name (ascii string) + attribute data
I'd actually be inclined to make everything ordered by default and not encode explicit field names. I'm afraid that encoding field names on the wire is going to easily increase the amount of device state by an order of magnitude.
Quoted 8 lines
- This does not discuss how we will handle cross version migration in a lot of detail. Presumably you expect capability magic to work for this. However this does not work for migration to file. A self describing format makes a much simpler solution possible:
separating attributes which destination must parse from those it can skip safely. If destination sees an attribute
that was marked as must parse and does not recognize it, migration fails. If not, just skip the attribute.
Compatibility and the wire format are more or less orthogonal. A self describing format helps eliminate a class of bugs but getting compatibility right requires quite a bit more.
Regards,
Anthony Liguori
Quoted 18 lines · Michael S. Tsirkin
Quoted 17 lines · Stefan Berger
Quoted 6 lines
4) Implement the BERVisitor and make this the default migration protocol.
Most of the work will be in 1), though with the implementation in this series we should be able to do it incrementally. I'm not sure if the best approach is doing the mechanical phase 1 conversion, then doing phase 2 sometime after 4), doing phase 1 + 2 as part of 1), or just doing VMState conversions which gives basically the same capabilities as phase 1 + 2.
Thoughts?
Is anyone working on this? If not I may give it a shot (tomorrow++) for at least some of the primitives... for enabling vNVRAM metadata of course. Indefinite length encoding of constructed data types I suppose won't be used otherwise the visitor interface seems wrong for parsing and skipping of extra data towards the end of a structure if version n wrote the stream and appended some of its version n data and now version m< n is trying to read the struct and needs to skip the version [m+1, n ] data fields ... in that case the de-serialization of the stream should probably be stream-driven rather than structure-driven.
Quoted 4 lines
Quoted 1 line
Stefan
Yes I've been struggling with that exactly.
Anthony, any thoughts?
It just depends on how you write your visitor. If you used sequences, you'd probably do something like this:
start_struct ->
check for sequence tag, push starting offset and size onto stack increment offset to next tag
type_int (et al) ->
check for explicit type, parse data
increment offset to next tag
end_struct ->
pop starting offset and size to temp variables
set offset to starting offset + size
This is roughly how the QMP input marshaller works FWIW.
Regards,
Anthony Liguori
Quoted 34 lines · Anthony Liguori
Quoted 18 lines · Michael S. Tsirkin
Quoted 17 lines · Stefan Berger
Quoted 6 lines
4) Implement the BERVisitor and make this the default migration protocol.
Most of the work will be in 1), though with the implementation in this series we should be able to do it incrementally. I'm not sure if the best approach is doing the mechanical phase 1 conversion, then doing phase 2 sometime after 4), doing phase 1 + 2 as part of 1), or just doing VMState conversions which gives basically the same capabilities as phase 1 + 2.
Thoughts?
Is anyone working on this? If not I may give it a shot (tomorrow++) for at least some of the primitives... for enabling vNVRAM metadata of course. Indefinite length encoding of constructed data types I suppose won't be used otherwise the visitor interface seems wrong for parsing and skipping of extra data towards the end of a structure if version n wrote the stream and appended some of its version n data and now version m< n is trying to read the struct and needs to skip the version [m+1, n ] data fields ... in that case the de-serialization of the stream should probably be stream-driven rather than structure-driven.
Quoted 4 lines
Quoted 1 line
Stefan
Yes I've been struggling with that exactly.
Anthony, any thoughts?It just depends on how you write your visitor. If you used sequences, you'd probably do something like this:
start_struct->
check for sequence tag, push starting offset and size onto stack increment offset to next tag
type_int(et al) ->
check for explicit type, parse data
increment offset to next tag
Quoted 6 lines
end_struct->
pop starting offset and size to temp variables
set offset to starting offset + sizeThis is roughly how the QMP input marshaller works FWIW.
I am doing that. Indefinite length encoding *would* be a problem because you cannot push the size onto the stack so that you could skip to the end of the structure.
Stefan
Quoted 3 lines · Stefan Berger
I am doing that. Indefinite length encoding *would* be a problem because you cannot push the size onto the stack so that you could skip to the end of the structure.
For an indefinite length encoding, you just have to keep reading the stream at end_struct until you hit the canary tag ignoring anything you encounter.
Regards,
Anthony Liguori
Quoted 3 lines
Stefan
Quoted 9 lines · Anthony Liguori
Quoted 5 lines
Here are some suggestions:
- Let's make the protocol be BER directly. As a first step, use a single octet string for the whole of data. Next, start splitting this up.
This can't be done without breaking the old style migration protocol. I don't have a problem with that but I do have a problem with repeatedly breaking migration protocol.
As long as this is within a release cycle, is this a real problem?
I think switching to BER then imediately releasing will be a mistake. Let's switch, iron out bugs and add features.
Quoted 9 lines
The Visitor interface is very little abstraction over a native BER interface. It lets us introduce BER incrementally while still supporting the old protocol.
Quoted 4 lines
- Don't try to use arrays at all. They don't make sense in either jason or asn. Instead, use sets for unordered data and sequences for ordered data.
Quoted 7 lines
sequences map to
visit_start_array()pretty well. You would do something like:
visit_start_array(v, "entries", errp);for (int i = 0; i < s->size; i++) { visit_type_int(v, NULL, &s->entry[i], errp); }
visit_end_array(v, errp);
Sequences can encode structures not just arrays.
How would you encode this for example:
SEQUENCE OF { VQN: INTEGER, SEQUENCE { OPTIONAL VECTOR: INTEGER} }
This is a natural description of a virtio device.
Yes we can unwrap this to a structure of arrays but this is tweaking protocol to match implementation.
Quoted 9 lines
Quoted 4 lines
- Most data should be unordered: we don't normally care in which order we get device attributes.
So we would have set of sequences, and
each sequence is attribute name (ascii string) + attribute dataI'd actually be inclined to make everything ordered by default and not encode explicit field names. I'm afraid that encoding field names on the wire is going to easily increase the amount of device state by an order of magnitude.
Assuming we worry about space used by name tags, let's use content-specific tags.
Quoted 9 lines
Quoted 8 lines
- This does not discuss how we will handle cross version migration in a lot of detail. Presumably you expect capability magic to work for this. However this does not work for migration to file. A self describing format makes a much simpler solution possible:
separating attributes which destination must parse from those it can skip safely. If destination sees an attribute
that was marked as must parse and does not recognize it, migration fails. If not, just skip the attribute.
Quoted 7 lines
Compatibility and the wire format are more or less orthogonal. A self describing format helps eliminate a class of bugs but getting compatibility right requires quite a bit more.
Regards,
Anthony Liguori
So I think we do care about compatibility.
I think this is by far the most interesting part of the problem. And making that robust IMO pretty much requires not relying on field order whenever we are not absolutely sure
no other order makes sense.
Content-specific tags as suggested above let us do unordered. We lose the ability to easily examine
input by external tools but without field names
they won't be able to parse it so it's not all that valuable anyway.
Signature · 1 line
MST
Quoted 5 lines · Anthony Liguori
Quoted 3 lines · Stefan Berger
I am doing that. Indefinite length encoding *would* be a problem because you cannot push the size onto the stack so that you could skip to the end of the structure.
Quoted 7 lines
For an indefinite length encoding, you just have to keep reading the stream at
end_structuntil you hit the canary tag ignoring anything you encounter.Regards,
Anthony Liguori
That's not exactly right: one indefinite length encoding can be nested within the other. So what we must do is keep reading, read out, decode and skip regular length encodings, and count the nesting of indefinite length encodings. When we see bit 7 set, we increase nesting. When we see end of content, we descrease nesting. Stop when nesting reaches 0.
Signature · 1 line
MST
Quoted 39 lines · Anthony Liguori
Quoted 18 lines · Michael S. Tsirkin
Quoted 17 lines · Stefan Berger
Quoted 6 lines
4) Implement the BERVisitor and make this the default migration protocol.
Most of the work will be in 1), though with the implementation in this series we should be able to do it incrementally. I'm not sure if the best approach is doing the mechanical phase 1 conversion, then doing phase 2 sometime after 4), doing phase 1 + 2 as part of 1), or just doing VMState conversions which gives basically the same capabilities as phase 1 + 2.
Thoughts?
Is anyone working on this? If not I may give it a shot (tomorrow++) for at least some of the primitives... for enabling vNVRAM metadata of course. Indefinite length encoding of constructed data types I suppose won't be used otherwise the visitor interface seems wrong for parsing and skipping of extra data towards the end of a structure if version n wrote the stream and appended some of its version n data and now version m< n is trying to read the struct and needs to skip the version [m+1, n ] data fields ... in that case the de-serialization of the stream should probably be stream-driven rather than structure-driven.
Quoted 4 lines
Quoted 1 line
Stefan
Yes I've been struggling with that exactly.
Anthony, any thoughts?It just depends on how you write your visitor. If you used sequences, you'd probably do something like this:
start_struct->
check for sequence tag, push starting offset and size onto stack increment offset to next tag
type_int(et al) ->
check for explicit type, parse data
increment offset to next tag
end_struct->
pop starting offset and size to temp variables
set offset to starting offset + size
Quoted 5 lines
This is roughly how the QMP input marshaller works FWIW.
Regards,
Anthony Liguori
One thing I worry about is enabling zero copy for large string types (e.g. memory migration).
So we need to be able to see a tag for memory page + address, read that from socket directly at the correct virtual address.
Probably, we can avoid using visitors for memory, and hope everything else can stand an extra copy since it's small.
But then, why do we worry about the size of
encoded device state as Anthony seems to do?
Signature · 1 line
MST
Quoted 7 lines · Michael S. Tsirkin
Quoted 6 lines · Anthony Liguori
Quoted 5 lines
Here are some suggestions:
- Let's make the protocol be BER directly. As a first step, use a single octet string for the whole of data. Next, start splitting this up.
Quoted 5 lines
Quoted 3 lines
This can't be done without breaking the old style migration protocol. I don't have a problem with that but I do have a problem with repeatedly breaking migration protocol.
As long as this is within a release cycle, is this a real problem?
I think if we try to fit it within a release we'll either end up with a 2 year long release or a half-broken conversion.
I'd rather buy ourselves time by supporting both formats. That way we can remove the old format when we're satisfied with the ASN.1 encoding.
Quoted 17 lines
Quoted 9 lines
The Visitor interface is very little abstraction over a native BER interface. It lets us introduce BER incrementally while still supporting the old protocol.
Quoted 4 lines
- Don't try to use arrays at all. They don't make sense in either jason or asn. Instead, use sets for unordered data and sequences for ordered data.
Quoted 7 lines
sequences map to
visit_start_array()pretty well. You would do something like:
visit_start_array(v, "entries", errp);for (int i = 0; i< s->size; i++) { visit_type_int(v, NULL,&s->entry[i], errp); }
visit_end_array(v, errp);
Quoted 4 lines
Sequences can encode structures not just arrays.
How would you encode this for example:SEQUENCE OF { VQN: INTEGER, SEQUENCE { OPTIONAL VECTOR: INTEGER} }
visit_start_array(v, "vqs", errp);
for (i = 0; i < s->n_vqs; i++) {
// Array elements never have a name, hence NULL name
visit_start_struct(v, "VirtQueue", NULL, errp);
visit_type_int(v, &s->vq[i].num, "vqn", errp);
// Given this sub-struct an arbitrary name. It could also be anonymous.
visit_start_struct(v, "MsixInfo", "msix_info", errp);
if (s->vq[i].msix_enabled) {
visit_type_int(v, &s->vq[i].vector, "vector", errp);
}
visit_end_struct(v, errp);
visit_end_struct(v, errp);
}visit_end_array(v, errp);
This would also generate JSON of:
'vqs': [ { 'vqn': 2, 'msix_info': { 'vector': 3 } } ]
Quoted 4 lines
This is a natural description of a virtio device.
Yes we can unwrap this to a structure of arrays but this is tweaking protocol to match implementation.
There's no need to from a Visitor perspective. It doesn't make it any easier.
Quoted 29 lines
Quoted 10 lines
Quoted 5 lines
- Most data should be unordered: we don't normally care in which order we get device attributes.
So we would have set of sequences, and
each sequence is attribute name (ascii string) + attribute dataI'd actually be inclined to make everything ordered by default and not encode explicit field names. I'm afraid that encoding field names on the wire is going to easily increase the amount of device state by an order of magnitude.
Assuming we worry about space used by name tags, let's use content-specific tags.
Quoted 15 lines
Quoted 8 lines
- This does not discuss how we will handle cross version migration in a lot of detail. Presumably you expect capability magic to work for this. However this does not work for migration to file. A self describing format makes a much simpler solution possible:
separating attributes which destination must parse from those it can skip safely. If destination sees an attribute
that was marked as must parse and does not recognize it, migration fails. If not, just skip the attribute.Compatibility and the wire format are more or less orthogonal. A self describing format helps eliminate a class of bugs but getting compatibility right requires quite a bit more.
Regards,
Quoted 7 lines
Quoted 1 line
Anthony Liguori
So I think we do care about compatibility.
I think this is by far the most interesting part of the problem. And making that robust IMO pretty much requires not relying on field order whenever we are not absolutely sure
no other order makes sense.
There are multiple things to consider with compatibility:
1) Creating compatible device models. This is a qdev problem and can't be solved in the protocol.
2) Ensuring we are sending all the data we need to. I think we solve this problem by autogenerating Visitors from the C definitions of the device structures.
3) Having the flexibility to morph what's sent over the wire into something that we matches our internal state. I don't think the wire format solves this. I think what solves this the ability to read the protocol into a data structure such that we can manipulate the data structure in memory.
Having the ability to ignore some fields is not enough. We need to also be able to split a single field into multiple fields, and event split a single device into multiple devices. If we're dealing with a tree data structure in memory, we have limitless ability to fudge things.
Visitors buy us this. Right now, we're talking about:
void virtio_marshal(QEMUFile *f, VirtioDevice *vdev);
If you just do BER encoding, that doesn't help us. We still can only read from the wire to a VirtioDevice. If we do:
void virtio_marshal(Visitor *v, VirtioDevice *vdev);
We can still do BER encoding. We can also do QEMUFile encoding in the current format.
But we also can Visit from a QObject. That means we can write something that reads in BER off the wire and generates QObjects corresponding to whatever is sent. We can then manipulate those QObjects in memory as much as we'd like before handing a QObject Visitor to virtio_marshal.
Separating parsing off the wire from handing something to the device models and allowing transformation in between is the key to addressing compatibility in a robust fashion.
Regards,
Anthony Liguori
Quoted 6 lines
Content-specific tags as suggested above let us do unordered. We lose the ability to easily examine
input by external tools but without field names
they won't be able to parse it so it's not all that valuable anyway.
Quoted 12 lines · Michael S. Tsirkin
Quoted 11 lines · Anthony Liguori
Quoted 3 lines · Stefan Berger
I am doing that. Indefinite length encoding *would* be a problem because you cannot push the size onto the stack so that you could skip to the end of the structure.
For an indefinite length encoding, you just have to keep reading the stream at
end_structuntil you hit the canary tag ignoring anything you encounter.Regards,
Quoted 8 lines
Quoted 1 line
Anthony Liguori
That's not exactly right: one indefinite length encoding can be nested within the other. So what we must do is keep reading, read out, decode and skip regular length encodings, and count the nesting of indefinite length encodings. When we see bit 7 set, we increase nesting. When we see end of content, we descrease nesting. Stop when nesting reaches 0.
Yes, you basically have a skip field in the Visitor. If skip is set, then don't actually marshal to anything. So:
type_int (et al.)
if (!skip) {
store to passed in int
}
increment offset
Then when you want to skip to the end of the indefinite, you do something like:
skip_indefinite:
while tag != CANARY:
if tag == INT:
visit_type_int(v, NULL, NULL, errp);
elif tag == STRING:
visit_type_str(v, NULL, NULL, errp);
elif tag == INDEFINITE:
visit_start_struct(v, NULL, NULL, errp);
skip_indefinite(v, errp);
visit_end_struct(v, NULL, NULL, errp);end_struct:
v->skip = true;
skip_indefinite(v, errp);
v->skip = false;
Regards,
Anthony Liguori
Quoted 1 line
Quoted 40 lines · Michael S. Tsirkin
Quoted 39 lines · Anthony Liguori
Quoted 18 lines · Michael S. Tsirkin
Quoted 17 lines · Stefan Berger
Quoted 6 lines
4) Implement the BERVisitor and make this the default migration protocol.
Most of the work will be in 1), though with the implementation in this series we should be able to do it incrementally. I'm not sure if the best approach is doing the mechanical phase 1 conversion, then doing phase 2 sometime after 4), doing phase 1 + 2 as part of 1), or just doing VMState conversions which gives basically the same capabilities as phase 1 + 2.
Thoughts?
Is anyone working on this? If not I may give it a shot (tomorrow++) for at least some of the primitives... for enabling vNVRAM metadata of course. Indefinite length encoding of constructed data types I suppose won't be used otherwise the visitor interface seems wrong for parsing and skipping of extra data towards the end of a structure if version n wrote the stream and appended some of its
version n data and now version m< n is trying to read the struct
and needs to skip the version [m+1, n ] data fields ... in that case the de-serialization of the stream should probably be stream-driven rather than structure-driven.Quoted 4 lines
Quoted 1 line
Stefan
Yes I've been struggling with that exactly.
Anthony, any thoughts?It just depends on how you write your visitor. If you used sequences, you'd probably do something like this:
start_struct->
check for sequence tag, push starting offset and size onto stack increment offset to next tag
type_int(et al) ->
check for explicit type, parse data
increment offset to next tag
end_struct->
pop starting offset and size to temp variables
set offset to starting offset + size
Quoted 8 lines
Quoted 5 lines
This is roughly how the QMP input marshaller works FWIW.
Regards,
Anthony Liguori
One thing I worry about is enabling zero copy for large string types (e.g. memory migration).
Memory shouldn't be done through Visitors. It should be handled as a special case.
Quoted 8 lines
So we need to be able to see a tag for memory page + address, read that from socket directly at the correct virtual address.
Probably, we can avoid using visitors for memory, and hope everything else can stand an extra copy since it's small.
But then, why do we worry about the size of
encoded device state as Anthony seems to do?
There's a significant difference between the cost of something on the wire and the cost of doing a memcpy. The cost of the data on the wire is directly proportional to downtime. So if we increase the size of the device state by a factor of 10, we increase the minimum downtime by a factor of 10.
Of course, *if* the size of device state is already negligible with respect to the minimum downtime, then it doesn't matter. This is easy to quantify though.
For a normal migration session today, what's the total size of the device
state in relation to the calculated bandwidth of the minimum downtime?
If it's very small, then we can add names and not worry about it.
Regards,
Anthony Liguori
Quoted 12 lines · Anthony Liguori
Quoted 10 lines
Quoted 5 lines
visit_start_array(v, "entries", errp);for (int i = 0; i< s->size; i++) { visit_type_int(v, NULL,&s->entry[i], errp); }
visit_end_array(v, errp);Sequences can encode structures not just arrays.
How would you encode this for example:SEQUENCE OF { VQN: INTEGER, SEQUENCE { OPTIONAL VECTOR: INTEGER} }
visit_start_array(v, "vqs", errp);
Quoted 9 lines
for (i = 0; i < s->n_vqs; i++) {
// Array elements never have a name, hence NULL name
visit_start_struct(v, "VirtQueue", NULL, errp);
visit_type_int(v, &s->vq[i].num, "vqn", errp);
// Given this sub-struct an arbitrary name. It could also be anonymous.
visit_start_struct(v, "MsixInfo", "msix_info", errp);
if (s->vq[i].msix_enabled) {
visit_type_int(v, &s->vq[i].vector, "vector", errp);Why is this a pointer to vector, btw?
Quoted 10 lines
} visit_end_struct(v, errp); visit_end_struct(v, errp); }
visit_end_array(v, errp);This would also generate JSON of:
'vqs': [ { 'vqn': 2, '
msix_info': { 'vector': 3 } } ]
How would optional fields be handled? Specifically the case where first field in a sequence tells you the meaning of the following ones?
Signature · 1 line
MST
Quoted 1 line · Anthony Liguori
Having the ability to ignore some fields is not enough.
But it is also really required.
Quoted 24 lines
We need to
also be able to split a single field into multiple fields, and event split a single device into multiple devices. If we're dealing with a tree data structure in memory, we have limitless ability to fudge things.Visitors buy us this. Right now, we're talking about:
void
virtio_marshal(QEMUFile *f, VirtioDevice *vdev);If you just do BER encoding, that doesn't help us. We still can
only read from the wire to a VirtioDevice. If we do:void
virtio_marshal(Visitor *v, VirtioDevice *vdev);We can still do BER encoding. We can also do QEMUFile encoding in the current format.
But we also can Visit from a QObject. That means we can write something that reads in BER off the wire and generates QObjects corresponding to whatever is sent. We can then manipulate those QObjects in memory as much as we'd like before handing a QObject Visitor to
virtio_marshal.
Quoted 3 lines
Separating parsing off the wire from handing something to the device models and allowing transformation in between is the key to addressing compatibility in a robust fashion.
Yes. I agree it is required, and doing this with visitors does looks like a good way to handle that.
However the ability to skip fields at the protocol level
is also required, and it needs to be handled at the protocol level. For example, I am worried about
invalid input with a huge array size making us allocate
and read a gigabyte into memory on the assumption that we will want to manipulate it later. Yes we could introduce artificial buffer size limits but these might then come to bite us.
If instead we can see that no one needs the array
up front, it will be easy to discard.
Signature · 1 line
MST
Quoted 44 lines · Anthony Liguori
Quoted 12 lines · Michael S. Tsirkin
Quoted 11 lines · Anthony Liguori
Quoted 3 lines · Stefan Berger
I am doing that. Indefinite length encoding *would* be a problem because you cannot push the size onto the stack so that you could skip to the end of the structure.
For an indefinite length encoding, you just have to keep reading the stream at
end_structuntil you hit the canary tag ignoring anything you encounter.Regards,
Quoted 8 lines
Quoted 1 line
Anthony Liguori
That's not exactly right: one indefinite length encoding can be nested within the other. So what we must do is keep reading, read out, decode and skip regular length encodings, and count the nesting of indefinite length encodings. When we see bit 7 set, we increase nesting. When we see end of content, we descrease nesting. Stop when nesting reaches 0.
Yes, you basically have a skip field in the Visitor. If skip is set, then don't actually marshal to anything. So:
type_int(et al.)
if (!skip) {
store to passed in int
}
increment offsetThen when you want to skip to the end of the indefinite, you do something like:
skip_indefinite:
while tag != CANARY:if tag == INT: visit_type_int(v, NULL, NULL, errp); elif tag == STRING: visit_type_str(v, NULL, NULL, errp); elif tag == INDEFINITE: visit_start_struct(v, NULL, NULL, errp); skip_indefinite(v, errp); visit_end_struct(v, NULL, NULL, errp);
Quoted 8 lines
end_struct:
v->skip= true;
skip_indefinite(v, errp);
v->skip= false;Regards,
Anthony Liguori
This implementation is recursive, which has the problem of wasting unlimited resources to skip bad input. And we really do not need to look at tag at all. Just the length.
Signature · 1 line
MST
Quoted 43 lines · Anthony Liguori
Quoted 42 lines · Michael S. Tsirkin
Quoted 41 lines · Anthony Liguori
Quoted 18 lines · Michael S. Tsirkin
Quoted 17 lines · Stefan Berger
Quoted 6 lines
4) Implement the BERVisitor and make this the default migration protocol.
Most of the work will be in 1), though with the implementation in this series we should be able to do it incrementally. I'm not sure if the best approach is doing the mechanical phase 1 conversion, then doing phase 2 sometime after 4), doing phase 1 + 2 as part of 1), or just doing VMState conversions which gives basically the same capabilities as phase 1 + 2.
Thoughts?
Is anyone working on this? If not I may give it a shot (tomorrow++) for at least some of the primitives... for enabling vNVRAM metadata of course. Indefinite length encoding of constructed data types I suppose won't be used otherwise the visitor interface seems wrong for parsing and skipping of extra data towards the end of a structure if version n wrote the stream and appended some of its
version n data and now version m< n is trying to read the struct
and needs to skip the version [m+1, n ] data fields ... in that case the de-serialization of the stream should probably be stream-driven rather than structure-driven.Quoted 4 lines
Quoted 1 line
Stefan
Yes I've been struggling with that exactly.
Anthony, any thoughts?It just depends on how you write your visitor. If you used sequences, you'd probably do something like this:
start_struct->
check for sequence tag, push starting offset and size onto stack increment offset to next tag
type_int(et al) ->
check for explicit type, parse data
increment offset to next tag
end_struct->
pop starting offset and size to temp variables
set offset to starting offset + sizeThis is roughly how the QMP input marshaller works FWIW.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Regards,
Anthony Liguori
One thing I worry about is enabling zero copy for large string types (e.g. memory migration).
Memory shouldn't be done through Visitors. It should be handled as a special case.
OK, that's fine then.
Quoted 21 lines
Quoted 8 lines
So we need to be able to see a tag for memory page + address, read that from socket directly at the correct virtual address.
Probably, we can avoid using visitors for memory, and hope everything else can stand an extra copy since it's small.
But then, why do we worry about the size of
encoded device state as Anthony seems to do?There's a significant difference between the cost of something on the wire and the cost of doing a memcpy. The cost of the data on the wire is directly proportional to downtime. So if we increase the size of the device state by a factor of 10, we increase the minimum downtime by a factor of 10.
Of course, *if* the size of device state is already negligible with respect to the minimum downtime, then it doesn't matter. This is easy to quantify though. For a normal migration session today, what's the total size of the device state in relation to the calculated bandwidth of the minimum downtime?
Quoted 5 lines
If it's very small, then we can add names and not worry about it.
Regards,
Anthony Liguori
Yes, it's easy to quantify. I think the following gives us the offset before and after, so the difference is the size we seek, right?
savevm.c +5 -0
@@ -1543,6 +1543,7 @@ int qemu_savevm_state_iterate(Monitor *mon, QEMUFile *f)int qemu_savevm_state_complete (Monitor *mon,QEMUFile *f) {SaveStateEntry *se; +unsigned long long vm_state_size;cpu_synchronize_all_states ();
@@ -1557,6 +1558,8 @@ int qemu_savevm_state_complete(Monitor *mon, QEMUFile *f) se->save_live_state (mon, f,QEMU_VM_SECTION_END , se->opaque); } + vm_state_size =qemu_ftell (f); +fprintf (stderr,"start size: %lld\n" , vm_state_size); QTAILQ_FOREACH(se, &savevm_handlers, entry) {int len;
@@ -1577,6 +1580,8 @@ int qemu_savevm_state_complete(Monitor *mon, QEMUFile *f)vmstate_save (f, se); } + vm_state_size =qemu_ftell (f); +fprintf (stderr,"end size: %lld\n" , vm_state_size);qemu_put_byte (f,QEMU_VM_EOF );
Signature · 1 line
MST
Quoted 18 lines · Michael S. Tsirkin
Quoted 17 lines · Anthony Liguori
Quoted 10 lines
Quoted 5 lines
visit_start_array(v, "entries", errp);for (int i = 0; i< s->size; i++) { visit_type_int(v, NULL,&s->entry[i], errp); }
visit_end_array(v, errp);Sequences can encode structures not just arrays.
How would you encode this for example:SEQUENCE OF { VQN: INTEGER, SEQUENCE { OPTIONAL VECTOR: INTEGER} }
visit_start_array(v, "vqs", errp);for (i = 0; i< s->n_vqs; i++) { // Array elements never have a name, hence NULL name visit_start_struct(v, "VirtQueue", NULL, errp); visit_type_int(v,&s->vq[i].num, "vqn", errp);
Quoted 6 lines
Quoted 4 lines
// Given this sub-struct an arbitrary name. It could also be anonymous. visit_start_struct(v, "MsixInfo", "msix_info", errp); if (s->vq[i].msix_enabled) { visit_type_int(v,&s->vq[i].vector, "vector", errp);Why is this a pointer to vector, btw?
So you can write a single visit function that works for input or output.
Think of the simple case like:
void visit_simple_type(Visitor *v, SimpleType *t, const char *name, Error **errp) {
visit_start_struct(v, "SimpleType", name, errp); visit_type_int(v, &t->a, "a", errp);
visit_type_int(v, &t->b, "b", errp);
visit_end_struct(v, errp);
}
For complex types like Virtio, you need to do a bit more. You wouldn't do a simple for () {} loop but instead use the Visitor list mechanism. That would eliminate the need to have to marshal n_vqs.
Quoted 8 lines
Quoted 8 lines
} visit_end_struct(v, errp); visit_end_struct(v, errp); }
visit_end_array(v, errp);
Quoted 5 lines
Quoted 3 lines
This would also generate JSON of:
'vqs': [ { 'vqn': 2, '
msix_info': { 'vector': 3 } } ]How would optional fields be handled?
As far as the Visitor goes, if something is optional you just don't encode it. If you need to key off the presence of a field, presumably you could just check to see whether it succeeded or failed to visit that field. I'm not 100% sure if you can do a single input/output visitor when you have optional fields.
My rough thinking is that each device would have a input/output visitor callback that took the same signature. That gives the flexibility of having two separate interfaces but in the common case, you just pass the same function for both.
Quoted 3 lines
Specifically
the case where first field in a sequence tells
you the meaning of the following ones?
Can you give me the example in ASN.1?
Regards,
Anthony Liguori
Quoted 16 lines · Michael S. Tsirkin
Quoted 16 lines
skip_indefinite:while tag != CANARY: if tag == INT: visit_type_int(v, NULL, NULL, errp); elif tag == STRING: visit_type_str(v, NULL, NULL, errp); elif tag == INDEFINITE: visit_start_struct(v, NULL, NULL, errp); skip_indefinite(v, errp); visit_end_struct(v, NULL, NULL, errp);
end_struct:v->skip = true; skip_indefinite(v, errp); v->skip = false;
Quoted 8 lines
Quoted 3 lines
Regards,
Anthony Liguori
This implementation is recursive, which has the problem of wasting unlimited resources to skip bad input. And we really do not need to look at tag at all. Just the length.
That's just one implementation. You could certainly do a non-recursive implementation.
Regards,
Anthony Liguori
Quoted 1 line
Quoted 78 lines · Michael S. Tsirkin
Quoted 43 lines · Anthony Liguori
Quoted 42 lines · Michael S. Tsirkin
Quoted 41 lines · Anthony Liguori
Quoted 18 lines · Michael S. Tsirkin
Quoted 17 lines · Stefan Berger
Quoted 6 lines
4) Implement the BERVisitor and make this the default migration protocol.
Most of the work will be in 1), though with the implementation in this series we should be able to do it incrementally. I'm not sure if the best approach is doing the mechanical phase 1 conversion, then doing phase 2 sometime after 4), doing phase 1 + 2 as part of 1), or just doing VMState conversions which gives basically the same capabilities as phase 1 + 2.
Thoughts?
Is anyone working on this? If not I may give it a shot (tomorrow++) for at least some of the primitives... for enabling vNVRAM metadata of course. Indefinite length encoding of constructed data types I suppose won't be used otherwise the visitor interface seems wrong for parsing and skipping of extra data towards the end of a structure if version n wrote the stream and appended some of its
version n data and now version m< n is trying to read the struct
and needs to skip the version [m+1, n ] data fields ... in that case the de-serialization of the stream should probably be stream-driven rather than structure-driven.Quoted 4 lines
Quoted 1 line
Stefan
Yes I've been struggling with that exactly.
Anthony, any thoughts?It just depends on how you write your visitor. If you used sequences, you'd probably do something like this:
start_struct->
check for sequence tag, push starting offset and size onto stack increment offset to next tag
type_int(et al) ->
check for explicit type, parse data
increment offset to next tag
end_struct->
pop starting offset and size to temp variables
set offset to starting offset + sizeThis is roughly how the QMP input marshaller works FWIW.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Regards,
Anthony Liguori
One thing I worry about is enabling zero copy for large string types (e.g. memory migration).
Memory shouldn't be done through Visitors. It should be handled as a special case.
OK, that's fine then.
Quoted 23 lines
Quoted 8 lines
So we need to be able to see a tag for memory page + address, read that from socket directly at the correct virtual address.
Probably, we can avoid using visitors for memory, and hope everything else can stand an extra copy since it's small.
But then, why do we worry about the size of
encoded device state as Anthony seems to do?There's a significant difference between the cost of something on the wire and the cost of doing a memcpy. The cost of the data on the wire is directly proportional to downtime. So if we increase the size of the device state by a factor of 10, we increase the minimum downtime by a factor of 10.
Of course, *if* the size of device state is already negligible with respect to the minimum downtime, then it doesn't matter. This is easy to quantify though. For a normal migration session today, what's the total size of the device state in relation to the calculated bandwidth of the minimum downtime?
If it's very small, then we can add names and not worry about it.
Quoted 7 lines
Quoted 3 lines
Regards,
Anthony Liguori
Yes, it's easy to quantify. I think the following gives us the offset before and after, so the difference is the size we seek, right?
Yeah, you'll also want:
arch_init.c +4 -0
@@ -334,6 +334,10 @@ int ram_save_live(Monitor *mon, QEMUFile *f, int stage, voi expected_time =ram_save_remaining () *TARGET_PAGE_SIZE / bwidth; +if (stage ==2 && expected_time <=migrate_max_downtime ()) { +fprintf (stderr,"max bwidth: %lld\n" , (long )(expected_time * bwidth)); + } +return (stage ==2 ) && (expected_time <=migrate_max_downtime ()); }
You'll want to compare the size to max bwidth.
BTW, putting this info properly into migration stats would probably be pretty useful.
Regards,
Anthony Liguori
Quoted 30 lines · Anthony Liguori
Quoted 18 lines · Michael S. Tsirkin
Quoted 17 lines · Anthony Liguori
Quoted 10 lines
Quoted 5 lines
visit_start_array(v, "entries", errp);for (int i = 0; i< s->size; i++) { visit_type_int(v, NULL,&s->entry[i], errp); }
visit_end_array(v, errp);Sequences can encode structures not just arrays.
How would you encode this for example:SEQUENCE OF { VQN: INTEGER, SEQUENCE { OPTIONAL VECTOR: INTEGER} }
visit_start_array(v, "vqs", errp);for (i = 0; i< s->n_vqs; i++) { // Array elements never have a name, hence NULL name visit_start_struct(v, "VirtQueue", NULL, errp); visit_type_int(v,&s->vq[i].num, "vqn", errp);Quoted 6 lines
Quoted 4 lines
// Given this sub-struct an arbitrary name. It could also be anonymous. visit_start_struct(v, "MsixInfo", "msix_info", errp); if (s->vq[i].msix_enabled) { visit_type_int(v,&s->vq[i].vector, "vector", errp);Why is this a pointer to vector, btw?
So you can write a single visit function that works for input or output.
Think of the simple case like:
Quoted 7 lines
void
visit_simple_type(Visitor *v, SimpleType *t, const char *name, Error **errp){ visit_start_struct(v, "SimpleType", name, errp); visit_type_int(v, &t->a, "a", errp); visit_type_int(v, &t->b, "b", errp); visit_end_struct(v, errp); }
Okay, so this actually stores the pointer to the integer somewhere? So what is the lifetime requirement for this memory?
For how long must it stay around?
Quoted 3 lines
For complex types like Virtio, you need to do a bit more. You wouldn't do a simple for () {} loop but instead use the Visitor list mechanism. That would eliminate the need to have to marshal
n_vqs.
Example?
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
} visit_end_struct(v, errp); visit_end_struct(v, errp); }
visit_end_array(v, errp);This would also generate JSON of:
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
'vqs': [ { 'vqn': 2, '
msix_info': { 'vector': 3 } } ]How would optional fields be handled?
As far as the Visitor goes, if something is optional you just don't encode it. If you need to key off the presence of a field, presumably you could just check to see whether it succeeded or failed to visit that field.
It would typically depend on the value.
Quoted 12 lines
I'm not 100% sure if you can do a
single input/output visitor when you have optional fields.My rough thinking is that each device would have a input/output visitor callback that took the same signature. That gives the flexibility of having two separate interfaces but in the common case, you just pass the same function for both.
Quoted 3 lines
Specifically
the case where first field in a sequence tells
you the meaning of the following ones?
Quoted 5 lines
Can you give me the example in ASN.1?
Regards,
Anthony Liguori
That would be a selection from CHOICE.
Note that CHOICE doesn't affect encoding on the wire:
BER just uses the underlying type.
Quoted 15 lines · Anthony Liguori
How I see this all evolving in the future is that we would have a formal protocol specification. From that spec, we would generate Visitors. This would handle taking what's on the wire and building an in-memory tree. If an ASN.1 sequence that isn't part of the spec is passed, we should just silently discard it.
I think the scenario above is that we have an array in the spec, but the implementation no longer uses that array. Your concern is that its inefficient to keep it in memory?
I think there are two cases. The first is that that array is required in the spec, but optional in our implementation and that decision is made at run time. In this case, it needs to get entered into memory.
Quoted 5 lines
If the array is no longer needed at all in our implementation, we can certainly annotate the protocol spec to skip it and not bring it into memory. Maybe it's an internal spec annotation or something.
All that said, this doesn't seem to be a terribly important problem to solve IMHO.
This is not exactly the concern.
Consider that when we see an optional field we do not recognize, we need to discard it.
Now, the reason we get a field we do not recognize might be because someone is feeding us malformed data (to DOS us, or by mistake). If we just trust input we will allocate insane amounts of memory.
This is why we should not rely on fields to have reasonable size in the input, we need to validate them.
Signature · 1 line
MST
Quoted 26 lines · Anthony Liguori
Quoted 16 lines · Michael S. Tsirkin
Quoted 16 lines
skip_indefinite:
while tag != CANARY:if tag == INT: visit_type_int(v, NULL, NULL, errp); elif tag == STRING: visit_type_str(v, NULL, NULL, errp); elif tag == INDEFINITE: visit_start_struct(v, NULL, NULL, errp); skip_indefinite(v, errp); visit_end_struct(v, NULL, NULL, errp);
end_struct:
v->skip= true;
skip_indefinite(v, errp);
v->skip= false;Quoted 8 lines
Quoted 3 lines
Regards,
Anthony Liguori
This implementation is recursive, which has the problem of wasting unlimited resources to skip bad input. And we really do not need to look at tag at all. Just the length.
Quoted 6 lines
That's just one implementation. You could certainly do a non-recursive implementation.
Regards,
Anthony Liguori
What I was saying exactly: we do not need to maintain a stack to skip an indefinite length field, just a nesting counter will do.
Quoted 1 line
Quoted 1 line
Quoted 39 lines · Michael S. Tsirkin
Quoted 30 lines · Anthony Liguori
Quoted 18 lines · Michael S. Tsirkin
Quoted 17 lines · Anthony Liguori
Quoted 10 lines
Quoted 5 lines
visit_start_array(v, "entries", errp);for (int i = 0; i< s->size; i++) { visit_type_int(v, NULL,&s->entry[i], errp); }
visit_end_array(v, errp);Sequences can encode structures not just arrays.
How would you encode this for example:SEQUENCE OF { VQN: INTEGER, SEQUENCE { OPTIONAL VECTOR: INTEGER} }
visit_start_array(v, "vqs", errp);for (i = 0; i< s->n_vqs; i++) { // Array elements never have a name, hence NULL name visit_start_struct(v, "VirtQueue", NULL, errp); visit_type_int(v,&s->vq[i].num, "vqn", errp);Quoted 6 lines
Quoted 4 lines
// Given this sub-struct an arbitrary name. It could also be anonymous. visit_start_struct(v, "MsixInfo", "msix_info", errp); if (s->vq[i].msix_enabled) { visit_type_int(v,&s->vq[i].vector, "vector", errp);Why is this a pointer to vector, btw?
So you can write a single visit function that works for input or output.
Think of the simple case like:
Quoted 7 lines
void
visit_simple_type(Visitor *v, SimpleType *t, const char *name, Error **errp){ visit_start_struct(v, "SimpleType", name, errp); visit_type_int(v,&t->a, "a", errp); visit_type_int(v,&t->b, "b", errp); visit_end_struct(v, errp); }
Quoted 2 lines
Okay, so this actually stores the pointer to the integer somewhere? So what is the lifetime requirement for this memory?
typedef struct SimpleType
{
int a;
int b;
} SimpleType;
So the Input visitor (reading off of the wire), essentially is:
type_int(Visitor *v, int *value, Error **errp)
{
*value = read_int_off_wire;
}
The Output visitor (writing to the wire), is:
type_int(Visitor *v, int *value, Error **errp)
{
write_int_to_write(*value);
}
Quoted 1 line
For how long must it stay around?
There are two interest cases where life cycle matters: pointers to structs and lists.
Right now, Visitors model all structures as pointers to structs. I've been thinking of having two separate interfaces, but for now, it takes:
start_struct(Visitor *v, void **value, size_t size, const char *name, Error **errp) {
}
The output visitor ignores size, but the input visitor essentially does:
{
*value = g_malloc0(size);
}
For QAPI, we actually provide automatic free functions that can free structures that were allocated by a Visitor.
Quoted 5 lines
Quoted 3 lines
For complex types like Virtio, you need to do a bit more. You wouldn't do a simple for () {} loop but instead use the Visitor list mechanism. That would eliminate the need to have to marshal
n_vqs.Example?
For a list, you need to make your list type compatible with GenericList. Once you've done that, you can do:
GenericList *i;
visit_start_list(m, name, errp);
for (i = visit_next_list(m, (GenericList **)obj, errp);
i;
i = visit_next_list(m, &i, errp)) {
TestStructList *native_i = (TestStructList *)i;
visit_type_TestStruct(m, &native_i->value, NULL, errp);
}
visit_end_list(m, errp);I won't rewrite the virtio code, but hopefully it's clear how you would modify it to work this way.
Quoted 37 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
} visit_end_struct(v, errp); visit_end_struct(v, errp); }
visit_end_array(v, errp);This would also generate JSON of:
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
'vqs': [ { 'vqn': 2, '
msix_info': { 'vector': 3 } } ]How would optional fields be handled?
As far as the Visitor goes, if something is optional you just don't encode it. If you need to key off the presence of a field, presumably you could just check to see whether it succeeded or failed to visit that field.
It would typically depend on the value.
Quoted 14 lines
I'm not 100% sure if you can do a
single input/output visitor when you have optional fields.My rough thinking is that each device would have a input/output visitor callback that took the same signature. That gives the flexibility of having two separate interfaces but in the common case, you just pass the same function for both.
Quoted 3 lines
Specifically
the case where first field in a sequence tells
you the meaning of the following ones?Can you give me the example in ASN.1?
Quoted 7 lines
Quoted 3 lines
Regards,
Anthony Liguori
That would be a selection from CHOICE.
Note that CHOICE doesn't affect encoding on the wire:
BER just uses the underlying type.
Ah, so that's a union value. I think we would have to decide how we wanted to model unions. Presumably, the selected CHOICE is indicated by some other field?
I would think it's a good idea to use the type information to identify which
selection of a CHOICE was chosen.
I had implemented a union type in the old glib branch but that was based on a struct with an enum element and then the appropriate value element.
Regards,
Anthony Liguori
Quoted 3 lines
Quoted 104 lines · Anthony Liguori
Quoted 78 lines · Michael S. Tsirkin
Quoted 43 lines · Anthony Liguori
Quoted 42 lines · Michael S. Tsirkin
Quoted 41 lines · Anthony Liguori
Quoted 18 lines · Michael S. Tsirkin
Quoted 17 lines · Stefan Berger
Quoted 6 lines
4) Implement the BERVisitor and make this the default migration protocol.
Most of the work will be in 1), though with the implementation in this series we should be able to do it incrementally. I'm not sure if the best approach is doing the mechanical phase 1 conversion, then doing phase 2 sometime after 4), doing phase 1 + 2 as part of 1), or just doing VMState conversions which gives basically the same capabilities as phase 1 + 2.
Thoughts?
Is anyone working on this? If not I may give it a shot (tomorrow++) for at least some of the primitives... for enabling vNVRAM metadata of course. Indefinite length encoding of constructed data types I suppose won't be used otherwise the visitor interface seems wrong for parsing and skipping of extra data towards the end of a structure if version n wrote the stream and appended some of its
version n data and now version m< n is trying to read the struct
and needs to skip the version [m+1, n ] data fields ... in that case the de-serialization of the stream should probably be stream-driven rather than structure-driven.Quoted 4 lines
Quoted 1 line
Stefan
Yes I've been struggling with that exactly.
Anthony, any thoughts?It just depends on how you write your visitor. If you used sequences, you'd probably do something like this:
start_struct->
check for sequence tag, push starting offset and size onto stack increment offset to next tag
type_int(et al) ->
check for explicit type, parse data
increment offset to next tag
end_struct->
pop starting offset and size to temp variables
set offset to starting offset + sizeThis is roughly how the QMP input marshaller works FWIW.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Regards,
Anthony Liguori
One thing I worry about is enabling zero copy for large string types (e.g. memory migration).
Memory shouldn't be done through Visitors. It should be handled as a special case.
OK, that's fine then.
Quoted 23 lines
Quoted 8 lines
So we need to be able to see a tag for memory page + address, read that from socket directly at the correct virtual address.
Probably, we can avoid using visitors for memory, and hope everything else can stand an extra copy since it's small.
But then, why do we worry about the size of
encoded device state as Anthony seems to do?There's a significant difference between the cost of something on the wire and the cost of doing a memcpy. The cost of the data on the wire is directly proportional to downtime. So if we increase the size of the device state by a factor of 10, we increase the minimum downtime by a factor of 10.
Of course, *if* the size of device state is already negligible with respect to the minimum downtime, then it doesn't matter. This is easy to quantify though. For a normal migration session today, what's the total size of the device state in relation to the calculated bandwidth of the minimum downtime?
If it's very small, then we can add names and not worry about it.
Quoted 7 lines
Quoted 3 lines
Regards,
Anthony Liguori
Yes, it's easy to quantify. I think the following gives us the offset before and after, so the difference is the size we seek, right?
Yeah, you'll also want:
arch_init.c +4
-0@@ -334,6 +334,10 @@ int ram_save_live(Monitor *mon, QEMUFile *f, int stage, voi expected_time =ram_save_remaining () *TARGET_PAGE_SIZE / bwidth; +if (stage ==2 && expected_time <=migrate_max_downtime ()) { +fprintf (stderr,"max bwidth: %lld\n" , (long )(expected_time * bwidth)); + } +return (stage ==2 ) && (expected_time <=migrate_max_downtime ()); }
Quoted 1 line
You'll want to compare the size to max bwidth.
Well that depends on how guest behaves etc. I'm guessing just full memory size is a sane thing to compare against.
I don't have a problem sticking this fprintf in as well.
Quoted 6 lines
BTW, putting this info properly into migration stats would probably be pretty useful.
Regards,
Anthony Liguori
Problem is adding anything to monitor makes me worry about future compatibility so much I usually just give up. IMO we really need a namespace for in-development experimental commands, like "unsupported-XXX", this would belong.
Signature · 1 line
MST
Quoted 8 lines · Anthony Liguori
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Specifically
the case where first field in a sequence tells
you the meaning of the following ones?Can you give me the example in ASN.1?
Regards,
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Anthony Liguori
That would be a selection from CHOICE.
Note that CHOICE doesn't affect encoding on the wire:
BER just uses the underlying type.Ah, so that's a union value. I think we would have to decide how we wanted to model unions.
IMHO it's easier to use if a visitor for the right type is called, not a top level one for the union.
Quoted 2 lines
Presumably, the selected CHOICE is
indicated by some other field?
Exactly. Some function of some other field :)
Quoted 3 lines
I would think it's a good idea to
use the type information to identify which selection of a CHOICE was chosen.
Not sure what this means. We really need to look at the serialized field to know which selection was chose. In theory the field that dictates the meaning might come after the choice, but I think we don't limit outselves too much if we require it to come before in a sequence.
Quoted 7 lines
I had implemented a union type in the old glib branch but that was based on a struct with an enum element and then the appropriate value element.
Regards,
Anthony Liguori
Yes, this seems limiting.
Signature · 1 line
MST
Quoted 30 lines · Michael S. Tsirkin
Quoted 8 lines · Anthony Liguori
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Specifically
the case where first field in a sequence tells
you the meaning of the following ones?Can you give me the example in ASN.1?
Regards,
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Anthony Liguori
That would be a selection from CHOICE.
Note that CHOICE doesn't affect encoding on the wire:
BER just uses the underlying type.Ah, so that's a union value. I think we would have to decide how we wanted to model unions.
IMHO it's easier to use if a visitor for the right type is called, not a top level one for the union.
Quoted 2 lines
Presumably, the selected CHOICE is
indicated by some other field?Exactly. Some function of some other field :)
Quoted 3 lines
I would think it's a good idea to
use the type information to identify which selection of a CHOICE was chosen.
Quoted 5 lines
Not sure what this means. We really need to look at the serialized field to know which selection was chose. In theory the field that dictates the meaning might come after the choice, but I think we don't limit outselves too much if we require it to come before in a sequence.
Yeah, you can certainly open code it..
if (s->some_field == FOO) {
visit_type_int(...);
} else if (s->some_field == BAR) {
visit_type_str(...);
}
Personally, I'm inclined to just go with that strategy unless it becomes pervasive. If so, I think it's worth abstracting to a higher level concept.
What that means for having a multi-Visitor system is that the protocol layer would accept any valid choice and the Device layer would validate that the selected choice is valid based on the field contents.
Regards,
Anthony Liguori
Quoted 12 lines
Quoted 9 lines
I had implemented a union type in the old glib branch but that was based on a struct with an enum element and then the appropriate value element.
Regards,
Anthony Liguori
Yes, this seems limiting.
Quoted 11 lines · Michael S. Tsirkin
Quoted 6 lines
BTW, putting this info properly into migration stats would probably be pretty useful.
Regards,
Anthony Liguori
Problem is adding anything to monitor makes me worry about future compatibility so much I usually just give up. IMO we really need a namespace for in-development experimental commands, like "unsupported-XXX", this would belong.
Or just make all of QMP unsupported across any given version. I'm not kidding about that actually.
If we document what the protocol is for any given version, then a layer like libvirt can deal with providing a consistent interface.
I often wonder who we're trying to preserve compatibility for. Part of libvirt's mission statement is providing a stable API so why not leverage that mission and get us out of the compatibility business.
Regards,
Anthony Liguori
Quoted 1 line
Quoted 4 lines · Michael S. Tsirkin
Quoted 1 line · Anthony Liguori
Having the ability to ignore some fields is not enough.
But it is also really required.
I agree. It's the principle of being conservative in what you send and liberal in what you accept.
Quoted 31 lines
Quoted 24 lines
We need to
also be able to split a single field into multiple fields, and event split a single device into multiple devices. If we're dealing with a tree data structure in memory, we have limitless ability to fudge things.Visitors buy us this. Right now, we're talking about:
void
virtio_marshal(QEMUFile *f, VirtioDevice *vdev);If you just do BER encoding, that doesn't help us. We still can
only read from the wire to a VirtioDevice. If we do:void
virtio_marshal(Visitor *v, VirtioDevice *vdev);We can still do BER encoding. We can also do QEMUFile encoding in the current format.
But we also can Visit from a QObject. That means we can write something that reads in BER off the wire and generates QObjects corresponding to whatever is sent. We can then manipulate those QObjects in memory as much as we'd like before handing a QObject Visitor to
virtio_marshal.Quoted 3 lines
Separating parsing off the wire from handing something to the device models and allowing transformation in between is the key to addressing compatibility in a robust fashion.
Yes. I agree it is required, and doing this with visitors does looks like a good way to handle that.
Quoted 9 lines
However the ability to skip fields at the protocol level
is also required, and it needs to be handled at the protocol level. For example, I am worried about
invalid input with a huge array size making us allocate
and read a gigabyte into memory on the assumption that we will want to manipulate it later. Yes we could introduce artificial buffer size limits but these might then come to bite us.
If instead we can see that no one needs the array
up front, it will be easy to discard.
Yes, we just were covering in the other parts of the thread. When you end_struct, end_array, or end_list, the visitor can discard whatevers left.
How I see this all evolving in the future is that we would have a formal protocol specification. From that spec, we would generate Visitors. This would handle taking what's on the wire and building an in-memory tree. If an ASN.1 sequence that isn't part of the spec is passed, we should just silently discard it.
I think the scenario above is that we have an array in the spec, but the implementation no longer uses that array. Your concern is that its inefficient to keep it in memory?
I think there are two cases. The first is that that array is required in the spec, but optional in our implementation and that decision is made at run time.
In this case, it needs to get entered into memory.
If the array is no longer needed at all in our implementation, we can certainly annotate the protocol spec to skip it and not bring it into memory. Maybe it's an internal spec annotation or something.
All that said, this doesn't seem to be a terribly important problem to solve IMHO.
Regards,
Anthony Liguori
Quoted 1 line
Quoted 16 lines · Anthony Liguori
Quoted 11 lines · Michael S. Tsirkin
Quoted 6 lines
BTW, putting this info properly into migration stats would probably be pretty useful.
Regards,
Anthony Liguori
Problem is adding anything to monitor makes me worry about future compatibility so much I usually just give up. IMO we really need a namespace for in-development experimental commands, like "unsupported-XXX", this would belong.
Or just make all of QMP unsupported across any given version. I'm not kidding about that actually.
Quoted 6 lines
If we document what the protocol is for any given version, then a layer like libvirt can deal with providing a consistent interface.
I often wonder who we're trying to preserve compatibility for. Part of libvirt's mission statement is providing a stable API so why not leverage that mission and get us out of the compatibility business.
You are correct, that ultimately libvirt should be isolating applications from any changes in QEMU's monitor protocol that take place across versions. We just ask that QEMU try not to make too many gratuitous changes, if it is reasonable to avoid them without negative impact on QEMU. The more time we have to spend adding support for different commands across each QEMU release, the less time we have to spend on adding useful features elsewhere.
To me the biggest benefit of QMP is not that the commands are to be supported long term, but rather than it is a sanely parsable format with detectable error reporting and asynchronous events.
The important thing to us, is not to make semantic changes to any *existing* commands. If an existing command is flawed, either leave it alone & introduce a new one, or if compatibility can't be maintained for some reason remove the old one & introduce a new one with a *new* name to avoid any confusion.
So if you have to deprecate some existing QMP commands and introduce new ones to replace them across releases we can & will cope with that in libvirt.
Daniel
Signature · 4 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
Quoted 13 lines · Anthony Liguori
Quoted 11 lines · Michael S. Tsirkin
Quoted 6 lines
BTW, putting this info properly into migration stats would probably be pretty useful.
Regards,
Anthony Liguori
Problem is adding anything to monitor makes me worry about future compatibility so much I usually just give up. IMO we really need a namespace for in-development experimental commands, like "unsupported-XXX", this would belong.
Quoted 5 lines
Or just make all of QMP unsupported across any given version. I'm not kidding about that actually.
If we document what the protocol is for any given version, then a layer like libvirt can deal with providing a consistent interface.
Once distros ship qemu, people will use it. Once they use it in some way for long, it becomes the supposted interface. No one reads documentation, obviously :) - do you expect people to re-read qemu documentation *with every release*?
Quoted 8 lines
I often wonder who we're trying to preserve compatibility for. Part of libvirt's mission statement is providing a stable API so why not leverage that mission and get us out of the compatibility business.
Regards,
Anthony Liguori
you can't force everyone to use libvirt.
Part of QMP mission statement was a stable interface. If it's not that I don't know what it's for.
Signature · 1 line
MST
Quoted 12 lines · Anthony Liguori
Quoted 11 lines · Michael S. Tsirkin
Quoted 9 lines · Anthony Liguori
Quoted 5 lines
Here are some suggestions:
- Let's make the protocol be BER directly. As a first step, use a single octet string for the whole of data. Next, start splitting this up.
This can't be done without breaking the old style migration protocol. I don't have a problem with that but I do have a problem with repeatedly breaking migration protocol.
Quoted 5 lines
Quoted 1 line
As long as this is within a release cycle, is this a real problem?
I think if we try to fit it within a release we'll either end up with a 2 year long release or a half-broken conversion.
I'd rather buy ourselves time by supporting both formats. That way we can remove the old format when we're satisfied with the ASN.1 encoding.
Hm, if backwards compatibility is what we want to achieve (migrating from Qemu 1.1 to Qemu 1.0) then at least the ASN.1 decoder / encoder should be all done in 1.0, no? Otherwise what would it mean to if 1.0 just skipped types 1.1 sends and doesn't understand?
Quoted 6 lines
There are multiple things to consider with compatibility:
1) Creating compatible device models. This is a qdev problem and can't be solved in the protocol.
2) Ensuring we are sending all the data we need to. I think we solve this problem by autogenerating Visitors from the C definitions of the device structures.
I would have thought that we would write a function that takes the VMStateDescription as an argument and write ASN.1 BER or CER comprising:
- a header containing the version of the device data
- the minimum version required to read the device data
- walk the array of VMStateFields and encode the the device data
and similarly a function for walking the fields for decoding of each device state.
So at least I am surprised to hear 'autogeneration' for this particular case...
Stefan
Quoted 3 lines · Anthony Liguori
Quoted 3 lines
Yes, it's easy to quantify. I think the following gives us the offset before and after, so the difference is the size we seek, right?
OK, Orit (Cc'd) did some research - this is a booting while still in grub, size probably does not get much less than that. windows is said to be much more aggressive in allocating memory.
start offset: 9600673
end offset: 9614933
So we get 15K out of 9M.
By the way, most of the memory here is pretty much all uniform I guess, because compressing it gets us:
gzip: 1934169
bzip2 -9: 1462551
So even with aggressive compression, we probably won't be able to get below 1.5M for memory, two orders of magnitude above device state.
Sounds convincing?
Signature · 1 line
MST
Quoted 5 lines · Michael S. Tsirkin
Quoted 3 lines · Anthony Liguori
Quoted 3 lines
Yes, it's easy to quantify. I think the following gives us the offset before and after, so the difference is the size we seek, right?
Quoted 8 lines
OK, Orit (Cc'd) did some research - this is a booting while still in grub, size probably does not get much less than that. windows is said to be much more aggressive in allocating memory.
start offset: 9600673
end offset: 9614933So we get 15K out of 9M.
So, let's do some napkin math.
Assume that it works out that most of that 15k are 4 byte integers. If we assume an average name length of 6 characters, a string should be encoded in 8 bytes. That means for every 4 bytes, we add 8 bytes which means we're increasing by 200%.
That means 45k.
A 1gbit link can xmit at max, 128k in 1ms. So that extra 30k is going to cost ~250us in transmit time if we can get 1gbit. A the default rate limit, it should cost us right around 1ms.
I guess it's liveable although with 30 network cards, I suspect it gets a heck of a lot worse.
Regards,
Anthony Liguori
Quoted 10 lines
By the way, most of the memory here is pretty much all uniform I guess, because compressing it gets us:
gzip: 1934169
bzip2 -9: 1462551So even with aggressive compression, we probably won't be able to get below 1.5M for memory, two orders of magnitude above device state.
Sounds convincing?
Quoted 18 lines · Stefan Berger
Quoted 17 lines · Anthony Liguori
Quoted 7 lines · Michael S. Tsirkin
Quoted 6 lines · Anthony Liguori
Quoted 5 lines
Here are some suggestions:
- Let's make the protocol be BER directly.
As a first step, use a single octet string for
the whole of data. Next, start splitting this up.Quoted 5 lines
Quoted 3 lines
This can't be done without breaking the old style migration protocol. I don't have a problem with that but I do have a problem with repeatedly breaking migration protocol.
As long as this is within a release cycle, is this a real problem?
I think if we try to fit it within a release we'll either end up with a 2 year long release or a half-broken conversion.
Quoted 6 lines
Quoted 2 lines
I'd rather buy ourselves time by supporting both formats. That way we can remove the old format when we're satisfied with the ASN.1 encoding.
Hm, if backwards compatibility is what we want to achieve (migrating from Qemu 1.1 to Qemu 1.0) then at least the ASN.1 decoder / encoder should be all done in 1.0, no? Otherwise what would it mean to if 1.0 just skipped types 1.1 sends and doesn't understand?
Before we introduce ASN1, we ought to introduce migration capabilities. Migration capabilities would be used to negotation ASN.1 over the wire. That means that 1.1 would use the existing protocol to talk to 1.0.
Quoted 9 lines
Quoted 9 lines
There are multiple things to consider with compatibility:
1) Creating compatible device models. This is a qdev problem and can't be solved in the protocol.
2) Ensuring we are sending all the data we need to. I think we solve this problem by autogenerating Visitors from the C definitions of the device structures.
Quoted 5 lines
I would have thought that we would write a function that takes the VMStateDescription as an argument and write ASN.1 BER or CER comprising:
- a header containing the version of the device data
- the minimum version required to read the device data
- walk the array of VMStateFields and encode the the device data
Sort of. You modify VMStateInfo to accept a visitor and name parameter in load and put. Then you write an ASN.1 BER Visitor and pass that visitor to VMStateInfo->load/put.
Regards,
Anthony Liguori
Quoted 6 lines
and similarly a function for walking the fields for decoding of each device state.
So at least I am surprised to hear 'autogeneration' for this particular case...
Stefan
Quoted 21 lines · Anthony Liguori
Quoted 20 lines · Stefan Berger
Quoted 19 lines · Anthony Liguori
Quoted 7 lines · Michael S. Tsirkin
Quoted 6 lines · Anthony Liguori
Quoted 5 lines
Here are some suggestions:
- Let's make the protocol be BER directly.
As a first step, use a single octet string for
the whole of data. Next, start splitting this up.Quoted 5 lines
Quoted 3 lines
This can't be done without breaking the old style migration protocol. I don't have a problem with that but I do have a problem with repeatedly breaking migration protocol.
As long as this is within a release cycle, is this a real problem?
I think if we try to fit it within a release we'll either end up with a 2 year long release or a half-broken conversion.
I'd rather buy ourselves time by supporting both formats. That way we can remove the old format when we're satisfied with the ASN.1 encoding.
Quoted 7 lines
Quoted 4 lines
Hm, if backwards compatibility is what we want to achieve (migrating from Qemu 1.1 to Qemu 1.0) then at least the ASN.1 decoder / encoder should be all done in 1.0, no? Otherwise what would it mean to if 1.0 just skipped types 1.1 sends and doesn't understand?
Before we introduce ASN1, we ought to introduce migration capabilities.
FWIW we introduce it for tpm as first step.
Quoted 2 lines
Migration capabilities would be used to negotation ASN.1 over the wire.
I'm not yet really convinced we need capabilities at all. Would be sad to make asn depend on that.
Quoted 2 lines
That means that 1.1 would use the existing
protocol to talk to 1.0.
We basically never had qemu that can talk
across versions 100%. The compability we carry around is so much dead code. And no wonder: there's no way to parse the protocol without being bug for bug compatible.
IMO let's just switch to a sane protocol first. Being compatible with that will be much easier.
Quoted 29 lines
Quoted 9 lines
Quoted 9 lines
There are multiple things to consider with compatibility:
1) Creating compatible device models. This is a qdev problem and can't be solved in the protocol.
2) Ensuring we are sending all the data we need to. I think we solve this problem by autogenerating Visitors from the C definitions of the device structures.
Quoted 5 lines
I would have thought that we would write a function that takes the VMStateDescription as an argument and write ASN.1 BER or CER comprising:
- a header containing the version of the device data
- the minimum version required to read the device data
- walk the array of VMStateFields and encode the the device dataSort of. You modify VMStateInfo to accept a visitor and name parameter in load and put. Then you write an ASN.1 BER Visitor and pass that visitor to VMStateInfo->load/put.
Regards,
Anthony Liguori
Quoted 6 lines
and similarly a function for walking the fields for decoding of each device state.
So at least I am surprised to hear 'autogeneration' for this particular case...
Stefan
Re: [Qemu-devel] [RFC 3/8] qapi: add QemuFileInputVisitor
In reply to Michael RothQuoted 137 lines · Michael Roth
Visitor interfaces to read values from a QEMUFile
Signed-off-by Michael Roth<[email protected]>--- Makefile.objs | 1 + qapi/qemu-file-input-visitor.c | 350 ++++++++++++++++++++++++++++++++++++++++ qapi/qemu-file-input-visitor.h | 26 +++ 3 files changed, 377 insertions(+), 0 deletions(-)create mode 100644
qapi/qemu-file-input-visitor.ccreate mode 100644qapi/qemu-file-input-visitor.hMakefile.objs +1
-0@@ -82,6 +82,7 @@ fsdev-obj-$(CONFIG_VIRTFS) += $(addprefix fsdev/, $(fsdev-nested-y)) common-obj-y = $(block-obj-y) blockdev.o common-obj-y += $(qapi-obj-y) common-obj-y += qapi/qemu-file-output-visitor.o +common-obj-y += qapi/qemu-file-input-visitor.o common-obj-y += $(net-obj-y) common-obj-y += $(qobject-obj-y) common-obj-$(CONFIG_LINUX) += $(fsdev-obj-$(CONFIG_LINUX))qapi/qemu-file-input-visitor.c new file +108
-0@@ -0,0 +1,350 @@ +/* +* QEMUFile Output 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. +* +*/ + +#include "qemu-file-input-visitor.h" +#include "qemu-queue.h" +#include "qemu-common.h" +#include "qemu-objects.h" +#include "hw/hw.h" +#include "qerror.h" + +typedef struct { +size_t elem_count; +size_t elem_size; +size_t pos; +}ArrayInfo ; + +typedef struct StackEntry +{ +enum { +QFIV_ARRAY , +QFIV_LIST , +QFIV_STRUCT , + } type; +ArrayInfo array_info; +QTAILQ_ENTRY (StackEntry) node; +}StackEntry ; + +struct QemuFileInputVisitor +{ +Visitor visitor; + QTAILQ_HEAD(, StackEntry) stack; +QEMUFile *file; +}; + +static QemuFileInputVisitor *to_iv (Visitor *v) +{ +return container_of (v, QemuFileInputVisitor, visitor); +} + +static void qemu_file_input_push (QemuFileInputVisitor *iv,StackEntry *e) +{ +QTAILQ_INSERT_HEAD (&iv->stack, e, node); +} + +static void qemu_file_input_push_array (QemuFileInputVisitor *iv,ArrayInfo ai) +{ +StackEntry *e =g_malloc0 (sizeof (*e)); + e->type =QFIV_ARRAY ; + e->array_info = ai; +qemu_file_input_push (iv, e); +} + +static void qemu_file_input_push_list (QemuFileInputVisitor *iv) +{ +StackEntry *e =g_malloc0 (sizeof (*e)); + e->type =QFIV_LIST ; +qemu_file_input_push (iv, e); +} + +static void qemu_file_input_push_struct (QemuFileInputVisitor *iv) +{ +StackEntry *e =g_malloc0 (sizeof (*e)); + e->type =QFIV_STRUCT ; +qemu_file_input_push (iv, e); +} + +static void *qemu_file_input_pop (QemuFileInputVisitor *iv) +{ +StackEntry *e =QTAILQ_FIRST (&iv->stack); +QTAILQ_REMOVE (&iv->stack, e, node); +return e; +} + +static bool qemu_file_input_is_array (QemuFileInputVisitor *iv) +{ +StackEntry *e =QTAILQ_FIRST (&iv->stack); +return e->type ==QFIV_ARRAY ; +} + +static bool qemu_file_input_is_list (QemuFileInputVisitor *ov) +{ +StackEntry *e =QTAILQ_FIRST (&ov->stack); +return e&& e->type ==QFIV_LIST ; +} + +static void qemu_file_input_start_struct (Visitor *v,void **obj, +const char *kind, +const char *name,size_t size, +Error **errp) +{ +QemuFileInputVisitor *iv =to_iv (v); + +if (obj&& *obj ==NULL ) { + *obj =g_malloc0 (size); + } +qemu_file_input_push_struct (iv); +} +
qapi/qemu-file-input-visitor.c line 118
Quoted 10 lines
qapi/qemu-file-input-visitor.c new file +10
-0+static void qemu_file_input_end_struct (Visitor *v,Error **errp) +{ +QemuFileInputVisitor *iv =to_iv (v); +StackEntry *e =qemu_file_input_pop (iv); + +if (!e || e->type !=QFIV_STRUCT ) { +error_set (errp,QERR_UNDEFINED_ERROR ); +return ; + } +g_free (e);
Hello Michael,
I was looking at the code again to see what (private) comment I had made the first time I read the code, and now I see additional issues.
The error test above and below will leak the e pointer when the type is wrong.
Quoted 24 lines
qapi/qemu-file-input-visitor.c new file +24
-0+} + +static void qemu_file_input_start_list (Visitor *v,const char *name, +Error **errp) +{ +QemuFileInputVisitor *iv =to_iv (v); +qemu_file_input_push_list (iv); +} + +static GenericList *qemu_file_input_next_list (Visitor *v,GenericList **list, +Error **errp) +{ +QemuFileInputVisitor *iv =to_iv (v); +GenericList *entry; + +if (!qemu_file_input_is_list (iv)) { +error_set (errp,QERR_UNDEFINED_ERROR ); + } + + entry =g_malloc0 (sizeof (*entry)); +if (*list) { + (*list)->next = entry; + } +
qapi/qemu-file-input-visitor.c line 152
Quoted 10 lines
qapi/qemu-file-input-visitor.c new file +10
-0+ *list = entry; +return entry; +} + +static void qemu_file_input_end_list (Visitor *v,Error **errp) +{ +QemuFileInputVisitor *iv =to_iv (v); +StackEntry *e =qemu_file_input_pop (iv); +if (!e || e->type !=QFIV_LIST ) { +error_set (errp,QERR_UNDEFINED_ERROR );
and here
Quoted 32 lines
qapi/qemu-file-input-visitor.c new file +32
-0+return ; + } +g_free (e); +} + +static void qemu_file_input_start_array (Visitor *v,void **obj, +const char *name, +size_t elem_count, +size_t elem_size, +Error **errp) +{ +QemuFileInputVisitor *iv =to_iv (v); +ArrayInfo ai = { + .elem_count = elem_count, + .elem_size = elem_size, + .pos =0 + }; +if (*obj ==NULL ) { + *obj =g_malloc0 (elem_count * elem_size); + } +qemu_file_input_push_array (iv, ai); +} + +static void qemu_file_input_next_array (Visitor *v,Error **errp) +{ +QemuFileInputVisitor *iv =to_iv (v); +StackEntry *e =QTAILQ_FIRST (&iv->stack); + +if (!qemu_file_input_is_array (iv) || + e->array_info.pos>= e->array_info.elem_count) { +error_set (errp,QERR_UNDEFINED_ERROR ); + }
qapi/qemu-file-input-visitor.c line 194
Quoted 10 lines
qapi/qemu-file-input-visitor.c new file +10
-0+ + e->array_info.pos++; +} + +static void qemu_file_input_end_array (Visitor *v,Error **errp) +{ +QemuFileInputVisitor *iv =to_iv (v); +StackEntry *e =qemu_file_input_pop (iv); +if (!e || e->type !=QFIV_ARRAY ) { +error_set (errp,QERR_UNDEFINED_ERROR );
and here.
Thanks
Chris
Quoted 188 lines
qapi/qemu-file-input-visitor.c new file +156
-0+return ; + } +g_free (e); +} + +static void qemu_file_input_type_str (Visitor *v,char **obj,const char *name, +Error **errp) +{ +if (obj) { +g_free (*obj); + } +} + +static void qemu_file_input_type_uint8_t (Visitor *v,uint8_t *obj, +const char *name,Error **errp) +{ +QemuFileInputVisitor *ov =container_of (v, QemuFileInputVisitor, visitor); + *obj =qemu_get_byte (ov->file); +} + +static void qemu_file_input_type_uint16_t (Visitor *v,uint16_t *obj, +const char *name,Error **errp) +{ +QemuFileInputVisitor *ov =container_of (v, QemuFileInputVisitor, visitor); + *obj =qemu_get_byte (ov->file)<<8 ; + *obj |=qemu_get_byte (ov->file); +} + +static void qemu_file_input_type_uint32_t (Visitor *v,uint32_t *obj, +const char *name,Error **errp) +{ +QemuFileInputVisitor *ov =container_of (v, QemuFileInputVisitor, visitor); + *obj =qemu_get_byte (ov->file)<<24 ; + *obj |=qemu_get_byte (ov->file)<<16 ; + *obj |=qemu_get_byte (ov->file)<<8 ; + *obj |=qemu_get_byte (ov->file); +} + +static void qemu_file_input_type_uint64_t (Visitor *v,uint64_t *obj, +const char *name,Error **errp) +{ +QemuFileInputVisitor *ov =container_of (v, QemuFileInputVisitor, visitor); + *obj = (uint64_t )qemu_get_byte (ov->file)<<56 ; + *obj |= (uint64_t )qemu_get_byte (ov->file)<<48 ; + *obj |= (uint64_t )qemu_get_byte (ov->file)<<40 ; + *obj |= (uint64_t )qemu_get_byte (ov->file)<<32 ; + *obj |=qemu_get_byte (ov->file)<<24 ; + *obj |=qemu_get_byte (ov->file)<<16 ; + *obj |=qemu_get_byte (ov->file)<<8 ; + *obj |=qemu_get_byte (ov->file); +} + +static void qemu_file_input_type_int8_t (Visitor *v,int8_t *obj, +const char *name,Error **errp) +{ +QemuFileInputVisitor *ov =container_of (v, QemuFileInputVisitor, visitor); + *obj =qemu_get_sbyte (ov->file); +} + +static void qemu_file_input_type_int16_t (Visitor *v,int16_t *obj, +const char *name,Error **errp) +{ +QemuFileInputVisitor *ov =container_of (v, QemuFileInputVisitor, visitor); + *obj =qemu_get_sbyte (ov->file)<<8 ; + *obj |=qemu_get_sbyte (ov->file); +} + +static void qemu_file_input_type_int32_t (Visitor *v,int32_t *obj, +const char *name,Error **errp) +{ +QemuFileInputVisitor *ov =container_of (v, QemuFileInputVisitor, visitor); + *obj =qemu_get_sbyte (ov->file)<<24 ; + *obj |=qemu_get_sbyte (ov->file)<<16 ; + *obj |=qemu_get_sbyte (ov->file)<<8 ; + *obj |=qemu_get_sbyte (ov->file); +} + +static void qemu_file_input_type_int64_t (Visitor *v,int64_t *obj, +const char *name,Error **errp) +{ +QemuFileInputVisitor *ov =container_of (v, QemuFileInputVisitor, visitor); + *obj = (int64_t )qemu_get_sbyte (ov->file)<<56 ; + *obj |= (int64_t )qemu_get_sbyte (ov->file)<<48 ; + *obj |= (int64_t )qemu_get_sbyte (ov->file)<<40 ; + *obj |= (int64_t )qemu_get_sbyte (ov->file)<<32 ; + *obj |=qemu_get_sbyte (ov->file)<<24 ; + *obj |=qemu_get_sbyte (ov->file)<<16 ; + *obj |=qemu_get_sbyte (ov->file)<<8 ; + *obj |=qemu_get_sbyte (ov->file); +} + +static void qemu_file_input_type_bool (Visitor *v,bool *obj,const char *name, +Error **errp) +{ +uint8_t val; +qemu_file_input_type_uint8_t (v,&val, name, errp); + *obj = val; +} + +static void qemu_file_input_type_number (Visitor *v,double *obj, +const char *name,Error **errp) +{ +uint64_t *val = (uint64_t *)obj; +qemu_file_input_type_uint64_t (v, val, name, errp); +} + +static void qemu_file_input_type_enum (Visitor *v,int *obj, +const char *strings[],const char *kind, +const char *name,Error **errp) +{ +} + +Visitor *qemu_file_input_get_visitor (QemuFileInputVisitor *ov) +{ +return &ov->visitor; +} + +void qemu_file_input_visitor_cleanup (QemuFileInputVisitor *ov) +{ +g_free (ov); +} + +QemuFileInputVisitor *qemu_file_input_visitor_new (QEMUFile *f) +{ +QemuFileInputVisitor *v; + + v =g_malloc0 (sizeof (*v)); + + v->file = f; + + v->visitor.start_struct = qemu_file_input_start_struct; + v->visitor.end_struct = qemu_file_input_end_struct; + v->visitor.start_list = qemu_file_input_start_list; + v->visitor.next_list = qemu_file_input_next_list; + v->visitor.end_list = qemu_file_input_end_list; + v->visitor.start_array = qemu_file_input_start_array; + v->visitor.next_array = qemu_file_input_next_array; + v->visitor.end_array = qemu_file_input_end_array; + v->visitor.type_enum = qemu_file_input_type_enum; + v->visitor.type_int = qemu_file_input_type_int64_t; + v->visitor.type_uint8_t = qemu_file_input_type_uint8_t; + v->visitor.type_uint16_t = qemu_file_input_type_uint16_t; + v->visitor.type_uint32_t = qemu_file_input_type_uint32_t; + v->visitor.type_uint64_t = qemu_file_input_type_uint64_t; + v->visitor.type_int8_t = qemu_file_input_type_int8_t; + v->visitor.type_int16_t = qemu_file_input_type_int16_t; + v->visitor.type_int32_t = qemu_file_input_type_int32_t; + v->visitor.type_int64_t = qemu_file_input_type_int64_t; + v->visitor.type_bool = qemu_file_input_type_bool; + v->visitor.type_str = qemu_file_input_type_str; + v->visitor.type_number = qemu_file_input_type_number; + +QTAILQ_INIT (&v->stack); + +return v; +}qapi/qemu-file-input-visitor.h new file +26
-0@@ -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_INPUT_VISITOR_H +#define QEMU_FILE_INPUT_VISITOR_H + +#include "qapi-visit-core.h" + +typedef struct QemuFileInputVisitor QemuFileInputVisitor ; + +QemuFileInputVisitor *qemu_file_input_visitor_new (QEMUFile *f); +void qemu_file_input_visitor_cleanup (QemuFileInputVisitor *d); + +Visitor *qemu_file_input_get_visitor (QemuFileInputVisitor *v); + +#endif