vmstate-static-checker.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #!/usr/bin/python
  2. #
  3. # Compares vmstate information stored in JSON format, obtained from
  4. # the -dump-vmstate QEMU command.
  5. #
  6. # Copyright 2014 Amit Shah <amit.shah@redhat.com>
  7. # Copyright 2014 Red Hat, Inc.
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, see <http://www.gnu.org/licenses/>.
  21. import argparse
  22. import json
  23. import sys
  24. # Count the number of errors found
  25. taint = 0
  26. def bump_taint():
  27. global taint
  28. # Ensure we don't wrap around or reset to 0 -- the shell only has
  29. # an 8-bit return value.
  30. if taint < 255:
  31. taint = taint + 1
  32. def check_fields_match(name, s_field, d_field):
  33. if s_field == d_field:
  34. return True
  35. # Some fields changed names between qemu versions. This list
  36. # is used to whitelist such changes in each section / description.
  37. changed_names = {
  38. 'apic': ['timer', 'timer_expiry'],
  39. 'e1000': ['dev', 'parent_obj'],
  40. 'ehci': ['dev', 'pcidev'],
  41. 'I440FX': ['dev', 'parent_obj'],
  42. 'ich9_ahci': ['card', 'parent_obj'],
  43. 'ich9-ahci': ['ahci', 'ich9_ahci'],
  44. 'ioh3420': ['PCIDevice', 'PCIEDevice'],
  45. 'ioh-3240-express-root-port': ['port.br.dev',
  46. 'parent_obj.parent_obj.parent_obj',
  47. 'port.br.dev.exp.aer_log',
  48. 'parent_obj.parent_obj.parent_obj.exp.aer_log'],
  49. 'lsiscsi': ['dev', 'parent_obj'],
  50. 'mch': ['d', 'parent_obj'],
  51. 'pci_bridge': ['bridge.dev', 'parent_obj', 'bridge.dev.shpc', 'shpc'],
  52. 'pcnet': ['pci_dev', 'parent_obj'],
  53. 'PIIX3': ['pci_irq_levels', 'pci_irq_levels_vmstate'],
  54. 'piix4_pm': ['dev', 'parent_obj', 'pci0_status',
  55. 'acpi_pci_hotplug.acpi_pcihp_pci_status[0x0]',
  56. 'pm1a.sts', 'ar.pm1.evt.sts', 'pm1a.en', 'ar.pm1.evt.en',
  57. 'pm1_cnt.cnt', 'ar.pm1.cnt.cnt',
  58. 'tmr.timer', 'ar.tmr.timer',
  59. 'tmr.overflow_time', 'ar.tmr.overflow_time',
  60. 'gpe', 'ar.gpe'],
  61. 'rtl8139': ['dev', 'parent_obj'],
  62. 'qxl': ['num_surfaces', 'ssd.num_surfaces'],
  63. 'usb-ccid': ['abProtocolDataStructure', 'abProtocolDataStructure.data'],
  64. 'usb-host': ['dev', 'parent_obj'],
  65. 'usb-mouse': ['usb-ptr-queue', 'HIDPointerEventQueue'],
  66. 'usb-tablet': ['usb-ptr-queue', 'HIDPointerEventQueue'],
  67. 'vmware_vga': ['card', 'parent_obj'],
  68. 'vmware_vga_internal': ['depth', 'new_depth'],
  69. 'xhci': ['pci_dev', 'parent_obj'],
  70. 'x3130-upstream': ['PCIDevice', 'PCIEDevice'],
  71. 'xio3130-express-downstream-port': ['port.br.dev',
  72. 'parent_obj.parent_obj.parent_obj',
  73. 'port.br.dev.exp.aer_log',
  74. 'parent_obj.parent_obj.parent_obj.exp.aer_log'],
  75. 'xio3130-downstream': ['PCIDevice', 'PCIEDevice'],
  76. 'xio3130-express-upstream-port': ['br.dev', 'parent_obj.parent_obj',
  77. 'br.dev.exp.aer_log',
  78. 'parent_obj.parent_obj.exp.aer_log'],
  79. }
  80. if not name in changed_names:
  81. return False
  82. if s_field in changed_names[name] and d_field in changed_names[name]:
  83. return True
  84. return False
  85. def get_changed_sec_name(sec):
  86. # Section names can change -- see commit 292b1634 for an example.
  87. changes = {
  88. "ICH9 LPC": "ICH9-LPC",
  89. }
  90. for item in changes:
  91. if item == sec:
  92. return changes[item]
  93. if changes[item] == sec:
  94. return item
  95. return ""
  96. def exists_in_substruct(fields, item):
  97. # Some QEMU versions moved a few fields inside a substruct. This
  98. # kept the on-wire format the same. This function checks if
  99. # something got shifted inside a substruct. For example, the
  100. # change in commit 1f42d22233b4f3d1a2933ff30e8d6a6d9ee2d08f
  101. if not "Description" in fields:
  102. return False
  103. if not "Fields" in fields["Description"]:
  104. return False
  105. substruct_fields = fields["Description"]["Fields"]
  106. if substruct_fields == []:
  107. return False
  108. return check_fields_match(fields["Description"]["name"],
  109. substruct_fields[0]["field"], item)
  110. def check_fields(src_fields, dest_fields, desc, sec):
  111. # This function checks for all the fields in a section. If some
  112. # fields got embedded into a substruct, this function will also
  113. # attempt to check inside the substruct.
  114. d_iter = iter(dest_fields)
  115. s_iter = iter(src_fields)
  116. # Using these lists as stacks to store previous value of s_iter
  117. # and d_iter, so that when time comes to exit out of a substruct,
  118. # we can go back one level up and continue from where we left off.
  119. s_iter_list = []
  120. d_iter_list = []
  121. advance_src = True
  122. advance_dest = True
  123. while True:
  124. if advance_src:
  125. try:
  126. s_item = s_iter.next()
  127. except StopIteration:
  128. if s_iter_list == []:
  129. break
  130. s_iter = s_iter_list.pop()
  131. continue
  132. else:
  133. # We want to avoid advancing just once -- when entering a
  134. # dest substruct, or when exiting one.
  135. advance_src = True
  136. if advance_dest:
  137. try:
  138. d_item = d_iter.next()
  139. except StopIteration:
  140. if d_iter_list == []:
  141. # We were not in a substruct
  142. print "Section \"" + sec + "\",",
  143. print "Description " + "\"" + desc + "\":",
  144. print "expected field \"" + s_item["field"] + "\",",
  145. print "while dest has no further fields"
  146. bump_taint()
  147. break
  148. d_iter = d_iter_list.pop()
  149. advance_src = False
  150. continue
  151. else:
  152. advance_dest = True
  153. if not check_fields_match(desc, s_item["field"], d_item["field"]):
  154. # Some fields were put in substructs, keeping the
  155. # on-wire format the same, but breaking static tools
  156. # like this one.
  157. # First, check if dest has a new substruct.
  158. if exists_in_substruct(d_item, s_item["field"]):
  159. # listiterators don't have a prev() function, so we
  160. # have to store our current location, descend into the
  161. # substruct, and ensure we come out as if nothing
  162. # happened when the substruct is over.
  163. #
  164. # Essentially we're opening the substructs that got
  165. # added which didn't change the wire format.
  166. d_iter_list.append(d_iter)
  167. substruct_fields = d_item["Description"]["Fields"]
  168. d_iter = iter(substruct_fields)
  169. advance_src = False
  170. continue
  171. # Next, check if src has substruct that dest removed
  172. # (can happen in backward migration: 2.0 -> 1.5)
  173. if exists_in_substruct(s_item, d_item["field"]):
  174. s_iter_list.append(s_iter)
  175. substruct_fields = s_item["Description"]["Fields"]
  176. s_iter = iter(substruct_fields)
  177. advance_dest = False
  178. continue
  179. print "Section \"" + sec + "\",",
  180. print "Description \"" + desc + "\":",
  181. print "expected field \"" + s_item["field"] + "\",",
  182. print "got \"" + d_item["field"] + "\"; skipping rest"
  183. bump_taint()
  184. break
  185. check_version(s_item, d_item, sec, desc)
  186. if not "Description" in s_item:
  187. # Check size of this field only if it's not a VMSTRUCT entry
  188. check_size(s_item, d_item, sec, desc, s_item["field"])
  189. check_description_in_list(s_item, d_item, sec, desc)
  190. def check_subsections(src_sub, dest_sub, desc, sec):
  191. for s_item in src_sub:
  192. found = False
  193. for d_item in dest_sub:
  194. if s_item["name"] != d_item["name"]:
  195. continue
  196. found = True
  197. check_descriptions(s_item, d_item, sec)
  198. if not found:
  199. print "Section \"" + sec + "\", Description \"" + desc + "\":",
  200. print "Subsection \"" + s_item["name"] + "\" not found"
  201. bump_taint()
  202. def check_description_in_list(s_item, d_item, sec, desc):
  203. if not "Description" in s_item:
  204. return
  205. if not "Description" in d_item:
  206. print "Section \"" + sec + "\", Description \"" + desc + "\",",
  207. print "Field \"" + s_item["field"] + "\": missing description"
  208. bump_taint()
  209. return
  210. check_descriptions(s_item["Description"], d_item["Description"], sec)
  211. def check_descriptions(src_desc, dest_desc, sec):
  212. check_version(src_desc, dest_desc, sec, src_desc["name"])
  213. if not check_fields_match(sec, src_desc["name"], dest_desc["name"]):
  214. print "Section \"" + sec + "\":",
  215. print "Description \"" + src_desc["name"] + "\"",
  216. print "missing, got \"" + dest_desc["name"] + "\" instead; skipping"
  217. bump_taint()
  218. return
  219. for f in src_desc:
  220. if not f in dest_desc:
  221. print "Section \"" + sec + "\"",
  222. print "Description \"" + src_desc["name"] + "\":",
  223. print "Entry \"" + f + "\" missing"
  224. bump_taint()
  225. continue
  226. if f == 'Fields':
  227. check_fields(src_desc[f], dest_desc[f], src_desc["name"], sec)
  228. if f == 'Subsections':
  229. check_subsections(src_desc[f], dest_desc[f], src_desc["name"], sec)
  230. def check_version(s, d, sec, desc=None):
  231. if s["version_id"] > d["version_id"]:
  232. print "Section \"" + sec + "\"",
  233. if desc:
  234. print "Description \"" + desc + "\":",
  235. print "version error:", s["version_id"], ">", d["version_id"]
  236. bump_taint()
  237. if not "minimum_version_id" in d:
  238. return
  239. if s["version_id"] < d["minimum_version_id"]:
  240. print "Section \"" + sec + "\"",
  241. if desc:
  242. print "Description \"" + desc + "\":",
  243. print "minimum version error:", s["version_id"], "<",
  244. print d["minimum_version_id"]
  245. bump_taint()
  246. def check_size(s, d, sec, desc=None, field=None):
  247. if s["size"] != d["size"]:
  248. print "Section \"" + sec + "\"",
  249. if desc:
  250. print "Description \"" + desc + "\"",
  251. if field:
  252. print "Field \"" + field + "\"",
  253. print "size mismatch:", s["size"], ",", d["size"]
  254. bump_taint()
  255. def check_machine_type(s, d):
  256. if s["Name"] != d["Name"]:
  257. print "Warning: checking incompatible machine types:",
  258. print "\"" + s["Name"] + "\", \"" + d["Name"] + "\""
  259. return
  260. def main():
  261. 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."
  262. parser = argparse.ArgumentParser(description=help_text)
  263. parser.add_argument('-s', '--src', type=file, required=True,
  264. help='json dump from src qemu')
  265. parser.add_argument('-d', '--dest', type=file, required=True,
  266. help='json dump from dest qemu')
  267. parser.add_argument('--reverse', required=False, default=False,
  268. action='store_true',
  269. help='reverse the direction')
  270. args = parser.parse_args()
  271. src_data = json.load(args.src)
  272. dest_data = json.load(args.dest)
  273. args.src.close()
  274. args.dest.close()
  275. if args.reverse:
  276. temp = src_data
  277. src_data = dest_data
  278. dest_data = temp
  279. for sec in src_data:
  280. dest_sec = sec
  281. if not dest_sec in dest_data:
  282. # Either the section name got changed, or the section
  283. # doesn't exist in dest.
  284. dest_sec = get_changed_sec_name(sec)
  285. if not dest_sec in dest_data:
  286. print "Section \"" + sec + "\" does not exist in dest"
  287. bump_taint()
  288. continue
  289. s = src_data[sec]
  290. d = dest_data[dest_sec]
  291. if sec == "vmschkmachine":
  292. check_machine_type(s, d)
  293. continue
  294. check_version(s, d, sec)
  295. for entry in s:
  296. if not entry in d:
  297. print "Section \"" + sec + "\": Entry \"" + entry + "\"",
  298. print "missing"
  299. bump_taint()
  300. continue
  301. if entry == "Description":
  302. check_descriptions(s[entry], d[entry], sec)
  303. return taint
  304. if __name__ == '__main__':
  305. sys.exit(main())