cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-vmstate-static-checker-script-to-validate-vmstate-ch.patch

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