From patchwork Wed Apr 23 16:37:48 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Dr. David Alan Gilbert" X-Patchwork-Id: 341939 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3C2A51400F6 for ; Thu, 24 Apr 2014 03:27:54 +1000 (EST) Received: from localhost ([::1]:34033 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wd0xb-0001yx-R3 for incoming@patchwork.ozlabs.org; Wed, 23 Apr 2014 13:27:51 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51394) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wd0Ct-0005DM-7J for qemu-devel@nongnu.org; Wed, 23 Apr 2014 12:39:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Wd0Cn-0008TE-2J for qemu-devel@nongnu.org; Wed, 23 Apr 2014 12:39:35 -0400 Received: from mx1.redhat.com ([209.132.183.28]:28707) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wd0Cm-0008T7-Qy for qemu-devel@nongnu.org; Wed, 23 Apr 2014 12:39:29 -0400 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s3NGcQFN020505 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 23 Apr 2014 12:38:26 -0400 Received: from dgilbert-t530.home.treblig.org (vpn1-7-213.ams2.redhat.com [10.36.7.213]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s3NGboL5024273; Wed, 23 Apr 2014 12:38:24 -0400 From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org Date: Wed, 23 Apr 2014 17:37:48 +0100 Message-Id: <1398271069-22057-16-git-send-email-dgilbert@redhat.com> In-Reply-To: <1398271069-22057-1-git-send-email-dgilbert@redhat.com> References: <1398271069-22057-1-git-send-email-dgilbert@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: stefanb@linux.vnet.ibm.com, quintela@redhat.com, mdroth@linux.vnet.ibm.com, agraf@suse.de, mst@redhat.com, aliguori@amazon.com, afaerber@suse.de Subject: [Qemu-devel] [RFC PATCH v2 15/16] Wire in BER visitors X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: "Dr. David Alan Gilbert" BER output visitor to be selected with env BER input visitor recognized by file header Signed-off-by: Dr. David Alan Gilbert --- include/migration/migration.h | 2 + savevm.c | 105 +++++++++++++++++++++++++++++++----------- 2 files changed, 80 insertions(+), 27 deletions(-) diff --git a/include/migration/migration.h b/include/migration/migration.h index 8111125..5d500ec 100644 --- a/include/migration/migration.h +++ b/include/migration/migration.h @@ -27,6 +27,8 @@ #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002 #define QEMU_VM_FILE_VERSION 0x00000003 +#define QEMU_VM_BER_FILE_MAGIC 0x7fcdc551 + #define QEMU_VM_EOF 0x00 #define QEMU_VM_SECTION_START 0x01 #define QEMU_VM_SECTION_PART 0x02 diff --git a/savevm.c b/savevm.c index 515dd77..2bbbcdc 100644 --- a/savevm.c +++ b/savevm.c @@ -24,6 +24,8 @@ #include "config-host.h" #include "qemu-common.h" +#include "qapi/qemu-file-ber-output-visitor.h" +#include "qapi/qemu-file-ber-input-visitor.h" #include "qapi/qemu-file-binary-input-visitor.h" #include "qapi/qemu-file-binary-output-visitor.h" #include "hw/hw.h" @@ -527,6 +529,34 @@ bool qemu_savevm_state_blocked(Error **errp) return false; } +/* Return a visitor for use on the QEMUFile; visit_destroy should be + * called on it to clean it up. + */ +static Visitor *qemu_savevm_get_visitor(QEMUFile *f) +{ + char *formatvar = getenv("QEMUMIGFORMAT"); + + if (formatvar && (!strcmp(formatvar, "BER"))) { + QemuFileBEROutputVisitor *qfberov = qemu_file_ber_output_visitor_new(f); + Visitor *v = qemu_file_ber_output_get_visitor(qfberov); + + qemu_file_set_tmp_visitor(f, v); + return v; + } + + if (formatvar) { + error_report("QEMUMIGFORMAT set to unknown value '%s'", formatvar); + assert(0); + } + + QemuFileBinOutputVisitor *qfbov = qemu_file_bin_output_visitor_new(f); + Visitor *v = qemu_file_bin_output_get_visitor(qfbov); + + qemu_file_set_tmp_visitor(f, v); + + return v; +} + void qemu_savevm_state_begin(QEMUFile *f, const MigrationParams *params) { @@ -534,10 +564,7 @@ void qemu_savevm_state_begin(QEMUFile *f, int ret; Error *local_err = NULL; SectionHeader sh; - - QemuFileBinOutputVisitor *qfbov = qemu_file_bin_output_visitor_new(f); - Visitor *v = qemu_file_bin_output_get_visitor(qfbov); - qemu_file_set_tmp_visitor(f, v); + Visitor *v = qemu_savevm_get_visitor(f); trace_savevm_state_begin(); QTAILQ_FOREACH(se, &savevm_handlers, entry) { @@ -789,13 +816,11 @@ static int qemu_savevm_state(QEMUFile *f) static int qemu_save_device_state(QEMUFile *f) { + Visitor *v = qemu_savevm_get_visitor(f); SaveStateEntry *se; Error *local_err; SectionHeader sh; - QemuFileBinOutputVisitor *qfbov = qemu_file_bin_output_visitor_new(f); - qemu_file_set_tmp_visitor(f, qemu_file_bin_output_get_visitor(qfbov)); - Visitor *v = qemu_file_bin_output_get_visitor(qfbov); int32_t section_type; cpu_synchronize_all_states(); @@ -861,41 +886,65 @@ typedef struct LoadStateEntry { int version_id; } LoadStateEntry; +/* Given a just opened input QEMUFile, peak at the header and figure + * out which visitor should be used for it. + * Return the visitor ready for use. + */ +static Visitor *pick_input_visitor(QEMUFile *f) +{ + uint32_t tmp32; + + if (qemu_peek_buffer(f, (uint8_t *)&tmp32, 4, 0) != 4) { + fprintf(stderr, "Failed to read SaveVM header word\n"); + return NULL; + } + + tmp32 = be32_to_cpu(tmp32); + + switch (tmp32) { + case QEMU_VM_FILE_MAGIC: { + QemuFileBinInputVisitor *qfbiv = qemu_file_bin_input_visitor_new(f); + Visitor *v = qemu_file_bin_input_get_visitor(qfbiv); + qemu_file_set_tmp_visitor(f, v); + + return v; + } + + case QEMU_VM_BER_FILE_MAGIC: { + QemuFileBERInputVisitor *qfberiv = qemu_file_ber_input_visitor_new(f); + Visitor *v = qemu_file_ber_input_get_visitor(qfberiv); + qemu_file_set_tmp_visitor(f, v); + + return v; + } + + default: + fprintf(stderr, "Invalid SaveVM header (0x%x)\n", tmp32); + return NULL; + } +} + int qemu_loadvm_state(QEMUFile *f) { QLIST_HEAD(, LoadStateEntry) loadvm_handlers = QLIST_HEAD_INITIALIZER(loadvm_handlers); LoadStateEntry *le, *new_le; + Visitor *v; Error *local_err = NULL; int32_t section_type; - unsigned int tmp; int ret; if (qemu_savevm_state_blocked(NULL)) { return -EINVAL; } - tmp = qemu_get_be32(f); - if (tmp != QEMU_VM_FILE_MAGIC) { - return -EINVAL; - } - - tmp = qemu_get_be32(f); - if (tmp == QEMU_VM_FILE_VERSION_COMPAT) { - error_report("SaveVM v2 format is obsolete and don't work anymore"); + v = pick_input_visitor(f); + if (!v) { return -ENOTSUP; } - if (tmp != QEMU_VM_FILE_VERSION) { - return -ENOTSUP; - } - - /* TODO: Here we should be able to figure out if it's a binary file - * or what and open the right type of visitor - */ - QemuFileBinInputVisitor *qfbiv = qemu_file_bin_input_visitor_new(f); - Visitor *v = qemu_file_bin_input_get_visitor(qfbiv); - qemu_file_set_tmp_visitor(f, v); + visit_start_sequence_compat(v, "file", VISIT_SEQ_COMPAT_FILE, NULL, + &local_err); visit_start_sequence_compat(v, "top", VISIT_SEQ_COMPAT_BYTE0TERM, NULL, &local_err); while (visit_get_next_type(v, §ion_type, NULL, "section", &local_err), @@ -912,6 +961,7 @@ int qemu_loadvm_state(QEMUFile *f) "secstart" : "secfull", VISIT_SEQ_COMPAT_SECTION_HEADER, &sh, &local_err); if (local_err) { + error_report("%s", error_get_pretty(local_err)); ret = -EINVAL; goto out; } @@ -1001,6 +1051,7 @@ out: } visit_end_sequence_compat(v, "top", VISIT_SEQ_COMPAT_BYTE0TERM, &local_err); + visit_end_sequence_compat(v, "file", VISIT_SEQ_COMPAT_FILE, &local_err); if (local_err) { error_report("%s", error_get_pretty(local_err)); ret = -EINVAL; @@ -1010,7 +1061,7 @@ out: ret = qemu_file_get_error(f); } - qemu_file_bin_input_visitor_cleanup(qfbiv); + visit_destroy(v, &local_err); return ret; }