9ae3a8
From 09e0354d2f25424042427ba8875a84f365a45ba9 Mon Sep 17 00:00:00 2001
9ae3a8
From: Amit Shah <amit.shah@redhat.com>
9ae3a8
Date: Fri, 11 Jul 2014 10:08:59 -0500
9ae3a8
Subject: [CHANGE 03/29] vmstate-static-checker: script to validate vmstate
9ae3a8
 changes
9ae3a8
To: rhvirt-patches@redhat.com,
9ae3a8
    jen@redhat.com
9ae3a8
9ae3a8
RH-Author: Amit Shah <amit.shah@redhat.com>
9ae3a8
Message-id: <d4c957267c3ed3a78279b6f1a4bc1a755785f032.1405072585.git.amit.shah@redhat.com>
9ae3a8
Patchwork-id: 59783
9ae3a8
O-Subject: [RHEL7.1 qemu-kvm PATCH 02/18] vmstate-static-checker: script to validate vmstate changes
9ae3a8
Bugzilla: 1118707
9ae3a8
RH-Acked-by: Juan Quintela <quintela@redhat.com>
9ae3a8
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
9ae3a8
RH-Acked-by: Dr. David Alan Gilbert (git) <dgilbert@redhat.com>
9ae3a8
9ae3a8
This script compares the vmstate dumps in JSON format as output by QEMU
9ae3a8
with the -dump-vmstate option.
9ae3a8
9ae3a8
It flags various errors, like version mismatch, sections going away,
9ae3a8
size mismatches, etc.
9ae3a8
9ae3a8
This script is tolerant of a few changes that do not change the on-wire
9ae3a8
format, like embedding a few fields within substructs.
9ae3a8
9ae3a8
The script takes -s/--src and -d/--dest parameters, to which filenames
9ae3a8
are given as arguments.
9ae3a8
9ae3a8
Example:
9ae3a8
9ae3a8
(in a qemu 2.0 tree):
9ae3a8
./x86_64-softmmu/qemu-system-x86_64 -dump-vmstate qemu-2.0.json
9ae3a8
9ae3a8
(in a qemu 2.2 tree:)
9ae3a8
./x86_64-softmmu/qemu-system-x86_64 -dump-vmstate -M pc-i440fx-2.0 \
9ae3a8
   qemu-2.2-m2.0.json
9ae3a8
9ae3a8
./scripts/vmstate-static-checker.py -s qemu-2.0.json -d qemu-2.2-m2.0.json
9ae3a8
9ae3a8
The script also takes a --reverse parameter to switch the src and dest
9ae3a8
jsons.  This is just a shorthand for reversing the src and dest.
9ae3a8
9ae3a8
The --help parameter shows usage information.
9ae3a8
9ae3a8
Signed-off-by: Amit Shah <amit.shah@redhat.com>
9ae3a8
Signed-off-by: Juan Quintela <quintela@redhat.com>
9ae3a8
(cherry picked from commit 426d1d016a494c978a513afcd03aa000fcbd5b3c)
9ae3a8
Signed-off-by: Amit Shah <amit.shah@redhat.com>
9ae3a8
Signed-off-by: jen <jen@redhat.com>
9ae3a8
---
9ae3a8
 scripts/vmstate-static-checker.py | 345 ++++++++++++++++++++++++++++++++++++++
9ae3a8
 1 file changed, 345 insertions(+)
9ae3a8
 create mode 100755 scripts/vmstate-static-checker.py
9ae3a8
9ae3a8
diff --git a/scripts/vmstate-static-checker.py b/scripts/vmstate-static-checker.py
9ae3a8
new file mode 100755
9ae3a8
index 0000000..1604e68
9ae3a8
--- /dev/null
9ae3a8
+++ b/scripts/vmstate-static-checker.py
9ae3a8
@@ -0,0 +1,345 @@
9ae3a8
+#!/usr/bin/python
9ae3a8
+#
9ae3a8
+# Compares vmstate information stored in JSON format, obtained from
9ae3a8
+# the -dump-vmstate QEMU command.
9ae3a8
+#
9ae3a8
+# Copyright 2014 Amit Shah <amit.shah@redhat.com>
9ae3a8
+# Copyright 2014 Red Hat, Inc.
9ae3a8
+#
9ae3a8
+# This program is free software; you can redistribute it and/or modify
9ae3a8
+# it under the terms of the GNU General Public License as published by
9ae3a8
+# the Free Software Foundation; either version 2 of the License, or
9ae3a8
+# (at your option) any later version.
9ae3a8
+#
9ae3a8
+# This program is distributed in the hope that it will be useful,
9ae3a8
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
9ae3a8
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9ae3a8
+# GNU General Public License for more details.
9ae3a8
+#
9ae3a8
+# You should have received a copy of the GNU General Public License along
9ae3a8
+# with this program; if not, see <http://www.gnu.org/licenses/>.
9ae3a8
+
9ae3a8
+import argparse
9ae3a8
+import json
9ae3a8
+import sys
9ae3a8
+
9ae3a8
+# Count the number of errors found
9ae3a8
+taint = 0
9ae3a8
+
9ae3a8
+def bump_taint():
9ae3a8
+    global taint
9ae3a8
+
9ae3a8
+    # Ensure we don't wrap around or reset to 0 -- the shell only has
9ae3a8
+    # an 8-bit return value.
9ae3a8
+    if taint < 255:
9ae3a8
+        taint = taint + 1
9ae3a8
+
9ae3a8
+
9ae3a8
+def check_fields_match(name, s_field, d_field):
9ae3a8
+    if s_field == d_field:
9ae3a8
+        return True
9ae3a8
+
9ae3a8
+    # Some fields changed names between qemu versions.  This list
9ae3a8
+    # is used to whitelist such changes in each section / description.
9ae3a8
+    changed_names = {
9ae3a8
+        'e1000': ['dev', 'parent_obj'],
9ae3a8
+        'ehci': ['dev', 'pcidev'],
9ae3a8
+        'I440FX': ['dev', 'parent_obj'],
9ae3a8
+        'ich9_ahci': ['card', 'parent_obj'],
9ae3a8
+        'ioh-3240-express-root-port': ['port.br.dev',
9ae3a8
+                                       'parent_obj.parent_obj.parent_obj',
9ae3a8
+                                       'port.br.dev.exp.aer_log',
9ae3a8
+                                'parent_obj.parent_obj.parent_obj.exp.aer_log'],
9ae3a8
+        'mch': ['d', 'parent_obj'],
9ae3a8
+        'pci_bridge': ['bridge.dev', 'parent_obj', 'bridge.dev.shpc', 'shpc'],
9ae3a8
+        'pcnet': ['pci_dev', 'parent_obj'],
9ae3a8
+        'PIIX3': ['pci_irq_levels', 'pci_irq_levels_vmstate'],
9ae3a8
+        'piix4_pm': ['dev', 'parent_obj', 'pci0_status',
9ae3a8
+                     'acpi_pci_hotplug.acpi_pcihp_pci_status[0x0]'],
9ae3a8
+        'rtl8139': ['dev', 'parent_obj'],
9ae3a8
+        'qxl': ['num_surfaces', 'ssd.num_surfaces'],
9ae3a8
+        'usb-host': ['dev', 'parent_obj'],
9ae3a8
+        'usb-mouse': ['usb-ptr-queue', 'HIDPointerEventQueue'],
9ae3a8
+        'usb-tablet': ['usb-ptr-queue', 'HIDPointerEventQueue'],
9ae3a8
+        'xhci': ['pci_dev', 'parent_obj'],
9ae3a8
+        'xio3130-express-downstream-port': ['port.br.dev',
9ae3a8
+                                            'parent_obj.parent_obj.parent_obj',
9ae3a8
+                                            'port.br.dev.exp.aer_log',
9ae3a8
+                                'parent_obj.parent_obj.parent_obj.exp.aer_log'],
9ae3a8
+        'xio3130-express-upstream-port': ['br.dev', 'parent_obj.parent_obj',
9ae3a8
+                                          'br.dev.exp.aer_log',
9ae3a8
+                                          'parent_obj.parent_obj.exp.aer_log'],
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    if not name in changed_names:
9ae3a8
+        return False
9ae3a8
+
9ae3a8
+    if s_field in changed_names[name] and d_field in changed_names[name]:
9ae3a8
+        return True
9ae3a8
+
9ae3a8
+    return False
9ae3a8
+
9ae3a8
+
9ae3a8
+def exists_in_substruct(fields, item):
9ae3a8
+    # Some QEMU versions moved a few fields inside a substruct.  This
9ae3a8
+    # kept the on-wire format the same.  This function checks if
9ae3a8
+    # something got shifted inside a substruct.  For example, the
9ae3a8
+    # change in commit 1f42d22233b4f3d1a2933ff30e8d6a6d9ee2d08f
9ae3a8
+
9ae3a8
+    if not "Description" in fields:
9ae3a8
+        return False
9ae3a8
+
9ae3a8
+    if not "Fields" in fields["Description"]:
9ae3a8
+        return False
9ae3a8
+
9ae3a8
+    substruct_fields = fields["Description"]["Fields"]
9ae3a8
+
9ae3a8
+    if substruct_fields == []:
9ae3a8
+        return False
9ae3a8
+
9ae3a8
+    return check_fields_match(fields["Description"]["name"],
9ae3a8
+                              substruct_fields[0]["field"], item)
9ae3a8
+
9ae3a8
+
9ae3a8
+def check_fields(src_fields, dest_fields, desc, sec):
9ae3a8
+    # This function checks for all the fields in a section.  If some
9ae3a8
+    # fields got embedded into a substruct, this function will also
9ae3a8
+    # attempt to check inside the substruct.
9ae3a8
+
9ae3a8
+    d_iter = iter(dest_fields)
9ae3a8
+    s_iter = iter(src_fields)
9ae3a8
+
9ae3a8
+    # Using these lists as stacks to store previous value of s_iter
9ae3a8
+    # and d_iter, so that when time comes to exit out of a substruct,
9ae3a8
+    # we can go back one level up and continue from where we left off.
9ae3a8
+
9ae3a8
+    s_iter_list = []
9ae3a8
+    d_iter_list = []
9ae3a8
+
9ae3a8
+    advance_src = True
9ae3a8
+    advance_dest = True
9ae3a8
+
9ae3a8
+    while True:
9ae3a8
+        if advance_src:
9ae3a8
+            try:
9ae3a8
+                s_item = s_iter.next()
9ae3a8
+            except StopIteration:
9ae3a8
+                if s_iter_list == []:
9ae3a8
+                    break
9ae3a8
+
9ae3a8
+                s_iter = s_iter_list.pop()
9ae3a8
+                continue
9ae3a8
+        else:
9ae3a8
+            # We want to avoid advancing just once -- when entering a
9ae3a8
+            # dest substruct, or when exiting one.
9ae3a8
+            advance_src = True
9ae3a8
+
9ae3a8
+        if advance_dest:
9ae3a8
+            try:
9ae3a8
+                d_item = d_iter.next()
9ae3a8
+            except StopIteration:
9ae3a8
+                if d_iter_list == []:
9ae3a8
+                    # We were not in a substruct
9ae3a8
+                    print "Section \"" + sec + "\",",
9ae3a8
+                    print "Description " + "\"" + desc + "\":",
9ae3a8
+                    print "expected field \"" + s_item["field"] + "\",",
9ae3a8
+                    print "while dest has no further fields"
9ae3a8
+                    bump_taint()
9ae3a8
+                    break
9ae3a8
+
9ae3a8
+                d_iter = d_iter_list.pop()
9ae3a8
+                advance_src = False
9ae3a8
+                continue
9ae3a8
+        else:
9ae3a8
+            advance_dest = True
9ae3a8
+
9ae3a8
+        if not check_fields_match(desc, s_item["field"], d_item["field"]):
9ae3a8
+            # Some fields were put in substructs, keeping the
9ae3a8
+            # on-wire format the same, but breaking static tools
9ae3a8
+            # like this one.
9ae3a8
+
9ae3a8
+            # First, check if dest has a new substruct.
9ae3a8
+            if exists_in_substruct(d_item, s_item["field"]):
9ae3a8
+                # listiterators don't have a prev() function, so we
9ae3a8
+                # have to store our current location, descend into the
9ae3a8
+                # substruct, and ensure we come out as if nothing
9ae3a8
+                # happened when the substruct is over.
9ae3a8
+                #
9ae3a8
+                # Essentially we're opening the substructs that got
9ae3a8
+                # added which didn't change the wire format.
9ae3a8
+                d_iter_list.append(d_iter)
9ae3a8
+                substruct_fields = d_item["Description"]["Fields"]
9ae3a8
+                d_iter = iter(substruct_fields)
9ae3a8
+                advance_src = False
9ae3a8
+                continue
9ae3a8
+
9ae3a8
+            # Next, check if src has substruct that dest removed
9ae3a8
+            # (can happen in backward migration: 2.0 -> 1.5)
9ae3a8
+            if exists_in_substruct(s_item, d_item["field"]):
9ae3a8
+                s_iter_list.append(s_iter)
9ae3a8
+                substruct_fields = s_item["Description"]["Fields"]
9ae3a8
+                s_iter = iter(substruct_fields)
9ae3a8
+                advance_dest = False
9ae3a8
+                continue
9ae3a8
+
9ae3a8
+            print "Section \"" + sec + "\",",
9ae3a8
+            print "Description \"" + desc + "\":",
9ae3a8
+            print "expected field \"" + s_item["field"] + "\",",
9ae3a8
+            print "got \"" + d_item["field"] + "\"; skipping rest"
9ae3a8
+            bump_taint()
9ae3a8
+            break
9ae3a8
+
9ae3a8
+        check_version(s_item, d_item, sec, desc)
9ae3a8
+
9ae3a8
+        if not "Description" in s_item:
9ae3a8
+            # Check size of this field only if it's not a VMSTRUCT entry
9ae3a8
+            check_size(s_item, d_item, sec, desc, s_item["field"])
9ae3a8
+
9ae3a8
+        check_description_in_list(s_item, d_item, sec, desc)
9ae3a8
+
9ae3a8
+
9ae3a8
+def check_subsections(src_sub, dest_sub, desc, sec):
9ae3a8
+    for s_item in src_sub:
9ae3a8
+        found = False
9ae3a8
+        for d_item in dest_sub:
9ae3a8
+            if s_item["name"] != d_item["name"]:
9ae3a8
+                continue
9ae3a8
+
9ae3a8
+            found = True
9ae3a8
+            check_descriptions(s_item, d_item, sec)
9ae3a8
+
9ae3a8
+        if not found:
9ae3a8
+            print "Section \"" + sec + "\", Description \"" + desc + "\":",
9ae3a8
+            print "Subsection \"" + s_item["name"] + "\" not found"
9ae3a8
+            bump_taint()
9ae3a8
+
9ae3a8
+
9ae3a8
+def check_description_in_list(s_item, d_item, sec, desc):
9ae3a8
+    if not "Description" in s_item:
9ae3a8
+        return
9ae3a8
+
9ae3a8
+    if not "Description" in d_item:
9ae3a8
+        print "Section \"" + sec + "\", Description \"" + desc + "\",",
9ae3a8
+        print "Field \"" + s_item["field"] + "\": missing description"
9ae3a8
+        bump_taint()
9ae3a8
+        return
9ae3a8
+
9ae3a8
+    check_descriptions(s_item["Description"], d_item["Description"], sec)
9ae3a8
+
9ae3a8
+
9ae3a8
+def check_descriptions(src_desc, dest_desc, sec):
9ae3a8
+    check_version(src_desc, dest_desc, sec, src_desc["name"])
9ae3a8
+
9ae3a8
+    if not check_fields_match(sec, src_desc["name"], dest_desc["name"]):
9ae3a8
+        print "Section \"" + sec + "\":",
9ae3a8
+        print "Description \"" + src_desc["name"] + "\"",
9ae3a8
+        print "missing, got \"" + dest_desc["name"] + "\" instead; skipping"
9ae3a8
+        bump_taint()
9ae3a8
+        return
9ae3a8
+
9ae3a8
+    for f in src_desc:
9ae3a8
+        if not f in dest_desc:
9ae3a8
+            print "Section \"" + sec + "\"",
9ae3a8
+            print "Description \"" + src_desc["name"] + "\":",
9ae3a8
+            print "Entry \"" + f + "\" missing"
9ae3a8
+            bump_taint()
9ae3a8
+            continue
9ae3a8
+
9ae3a8
+        if f == 'Fields':
9ae3a8
+            check_fields(src_desc[f], dest_desc[f], src_desc["name"], sec)
9ae3a8
+
9ae3a8
+        if f == 'Subsections':
9ae3a8
+            check_subsections(src_desc[f], dest_desc[f], src_desc["name"], sec)
9ae3a8
+
9ae3a8
+
9ae3a8
+def check_version(s, d, sec, desc=None):
9ae3a8
+    if s["version_id"] > d["version_id"]:
9ae3a8
+        print "Section \"" + sec + "\"",
9ae3a8
+        if desc:
9ae3a8
+            print "Description \"" + desc + "\":",
9ae3a8
+        print "version error:", s["version_id"], ">", d["version_id"]
9ae3a8
+        bump_taint()
9ae3a8
+
9ae3a8
+    if not "minimum_version_id" in d:
9ae3a8
+        return
9ae3a8
+
9ae3a8
+    if s["version_id"] < d["minimum_version_id"]:
9ae3a8
+        print "Section \"" + sec + "\"",
9ae3a8
+        if desc:
9ae3a8
+            print "Description \"" + desc + "\":",
9ae3a8
+            print "minimum version error:", s["version_id"], "<",
9ae3a8
+            print d["minimum_version_id"]
9ae3a8
+            bump_taint()
9ae3a8
+
9ae3a8
+
9ae3a8
+def check_size(s, d, sec, desc=None, field=None):
9ae3a8
+    if s["size"] != d["size"]:
9ae3a8
+        print "Section \"" + sec + "\"",
9ae3a8
+        if desc:
9ae3a8
+            print "Description \"" + desc + "\"",
9ae3a8
+        if field:
9ae3a8
+            print "Field \"" + field + "\"",
9ae3a8
+        print "size mismatch:", s["size"], ",", d["size"]
9ae3a8
+        bump_taint()
9ae3a8
+
9ae3a8
+
9ae3a8
+def check_machine_type(s, d):
9ae3a8
+    if s["Name"] != d["Name"]:
9ae3a8
+        print "Warning: checking incompatible machine types:",
9ae3a8
+        print "\"" + s["Name"] + "\", \"" + d["Name"] + "\""
9ae3a8
+    return
9ae3a8
+
9ae3a8
+
9ae3a8
+def main():
9ae3a8
+    help_text = "Parse JSON-formatted vmstate dumps from QEMU in files SRC and DEST.  Checks whether migration from SRC to DEST QEMU versions would break based on the VMSTATE information contained within the JSON outputs.  The JSON output is created from a QEMU invocation with the -dump-vmstate parameter and a filename argument to it.  Other parameters to QEMU do not matter, except the -M (machine type) parameter."
9ae3a8
+
9ae3a8
+    parser = argparse.ArgumentParser(description=help_text)
9ae3a8
+    parser.add_argument('-s', '--src', type=file, required=True,
9ae3a8
+                        help='json dump from src qemu')
9ae3a8
+    parser.add_argument('-d', '--dest', type=file, required=True,
9ae3a8
+                        help='json dump from dest qemu')
9ae3a8
+    parser.add_argument('--reverse', required=False, default=False,
9ae3a8
+                        action='store_true',
9ae3a8
+                        help='reverse the direction')
9ae3a8
+    args = parser.parse_args()
9ae3a8
+
9ae3a8
+    src_data = json.load(args.src)
9ae3a8
+    dest_data = json.load(args.dest)
9ae3a8
+    args.src.close()
9ae3a8
+    args.dest.close()
9ae3a8
+
9ae3a8
+    if args.reverse:
9ae3a8
+        temp = src_data
9ae3a8
+        src_data = dest_data
9ae3a8
+        dest_data = temp
9ae3a8
+
9ae3a8
+    for sec in src_data:
9ae3a8
+        if not sec in dest_data:
9ae3a8
+            print "Section \"" + sec + "\" does not exist in dest"
9ae3a8
+            bump_taint()
9ae3a8
+            continue
9ae3a8
+
9ae3a8
+        s = src_data[sec]
9ae3a8
+        d = dest_data[sec]
9ae3a8
+
9ae3a8
+        if sec == "vmschkmachine":
9ae3a8
+            check_machine_type(s, d)
9ae3a8
+            continue
9ae3a8
+
9ae3a8
+        check_version(s, d, sec)
9ae3a8
+
9ae3a8
+        for entry in s:
9ae3a8
+            if not entry in d:
9ae3a8
+                print "Section \"" + sec + "\": Entry \"" + entry + "\"",
9ae3a8
+                print "missing"
9ae3a8
+                bump_taint()
9ae3a8
+                continue
9ae3a8
+
9ae3a8
+            if entry == "Description":
9ae3a8
+                check_descriptions(s[entry], d[entry], sec)
9ae3a8
+
9ae3a8
+    return taint
9ae3a8
+
9ae3a8
+
9ae3a8
+if __name__ == '__main__':
9ae3a8
+    sys.exit(main())
9ae3a8
-- 
9ae3a8
1.9.3
9ae3a8