vmstate-static-checker.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. 'cirrus_vga': ['hw_cursor_x', 'vga.hw_cursor_x',
  50. 'hw_cursor_y', 'vga.hw_cursor_y'],
  51. 'lsiscsi': ['dev', 'parent_obj'],
  52. 'mch': ['d', 'parent_obj'],
  53. 'pci_bridge': ['bridge.dev', 'parent_obj', 'bridge.dev.shpc', 'shpc'],
  54. 'pcnet': ['pci_dev', 'parent_obj'],
  55. 'PIIX3': ['pci_irq_levels', 'pci_irq_levels_vmstate'],
  56. 'piix4_pm': ['dev', 'parent_obj', 'pci0_status',
  57. 'acpi_pci_hotplug.acpi_pcihp_pci_status[0x0]',
  58. 'pm1a.sts', 'ar.pm1.evt.sts', 'pm1a.en', 'ar.pm1.evt.en',
  59. 'pm1_cnt.cnt', 'ar.pm1.cnt.cnt',
  60. 'tmr.timer', 'ar.tmr.timer',
  61. 'tmr.overflow_time', 'ar.tmr.overflow_time',
  62. 'gpe', 'ar.gpe'],
  63. 'rtl8139': ['dev', 'parent_obj'],
  64. 'qxl': ['num_surfaces', 'ssd.num_surfaces'],
  65. 'usb-ccid': ['abProtocolDataStructure', 'abProtocolDataStructure.data'],
  66. 'usb-host': ['dev', 'parent_obj'],
  67. 'usb-mouse': ['usb-ptr-queue', 'HIDPointerEventQueue'],
  68. 'usb-tablet': ['usb-ptr-queue', 'HIDPointerEventQueue'],
  69. 'vmware_vga': ['card', 'parent_obj'],
  70. 'vmware_vga_internal': ['depth', 'new_depth'],
  71. 'xhci': ['pci_dev', 'parent_obj'],
  72. 'x3130-upstream': ['PCIDevice', 'PCIEDevice'],
  73. 'xio3130-express-downstream-port': ['port.br.dev',
  74. 'parent_obj.parent_obj.parent_obj',
  75. 'port.br.dev.exp.aer_log',
  76. 'parent_obj.parent_obj.parent_obj.exp.aer_log'],
  77. 'xio3130-downstream': ['PCIDevice', 'PCIEDevice'],
  78. 'xio3130-express-upstream-port': ['br.dev', 'parent_obj.parent_obj',
  79. 'br.dev.exp.aer_log',
  80. 'parent_obj.parent_obj.exp.aer_log'],
  81. }
  82. if not name in changed_names:
  83. return False
  84. if s_field in changed_names[name] and d_field in changed_names[name]:
  85. return True
  86. return False
  87. def get_changed_sec_name(sec):
  88. # Section names can change -- see commit 292b1634 for an example.
  89. changes = {
  90. "ICH9 LPC": "ICH9-LPC",
  91. }
  92. for item in changes:
  93. if item == sec:
  94. return changes[item]
  95. if changes[item] == sec:
  96. return item
  97. return ""
  98. def exists_in_substruct(fields, item):
  99. # Some QEMU versions moved a few fields inside a substruct. This
  100. # kept the on-wire format the same. This function checks if
  101. # something got shifted inside a substruct. For example, the
  102. # change in commit 1f42d22233b4f3d1a2933ff30e8d6a6d9ee2d08f
  103. if not "Description" in fields:
  104. return False
  105. if not "Fields" in fields["Description"]:
  106. return False
  107. substruct_fields = fields["Description"]["Fields"]
  108. if substruct_fields == []:
  109. return False
  110. return check_fields_match(fields["Description"]["name"],
  111. substruct_fields[0]["field"], item)
  112. def check_fields(src_fields, dest_fields, desc, sec):
  113. # This function checks for all the fields in a section. If some
  114. # fields got embedded into a substruct, this function will also
  115. # attempt to check inside the substruct.
  116. d_iter = iter(dest_fields)
  117. s_iter = iter(src_fields)
  118. # Using these lists as stacks to store previous value of s_iter
  119. # and d_iter, so that when time comes to exit out of a substruct,
  120. # we can go back one level up and continue from where we left off.
  121. s_iter_list = []
  122. d_iter_list = []
  123. advance_src = True
  124. advance_dest = True
  125. unused_count = 0
  126. while True:
  127. if advance_src:
  128. try:
  129. s_item = s_iter.next()
  130. except StopIteration:
  131. if s_iter_list == []:
  132. break
  133. s_iter = s_iter_list.pop()
  134. continue
  135. else:
  136. if unused_count == 0:
  137. # We want to avoid advancing just once -- when entering a
  138. # dest substruct, or when exiting one.
  139. advance_src = True
  140. if advance_dest:
  141. try:
  142. d_item = d_iter.next()
  143. except StopIteration:
  144. if d_iter_list == []:
  145. # We were not in a substruct
  146. print "Section \"" + sec + "\",",
  147. print "Description " + "\"" + desc + "\":",
  148. print "expected field \"" + s_item["field"] + "\",",
  149. print "while dest has no further fields"
  150. bump_taint()
  151. break
  152. d_iter = d_iter_list.pop()
  153. advance_src = False
  154. continue
  155. else:
  156. if unused_count == 0:
  157. advance_dest = True
  158. if unused_count > 0:
  159. if advance_dest == False:
  160. unused_count = unused_count - s_item["size"]
  161. if unused_count == 0:
  162. advance_dest = True
  163. continue
  164. if unused_count < 0:
  165. print "Section \"" + sec + "\",",
  166. print "Description \"" + desc + "\":",
  167. print "unused size mismatch near \"",
  168. print s_item["field"] + "\""
  169. bump_taint()
  170. break
  171. continue
  172. if advance_src == False:
  173. unused_count = unused_count - d_item["size"]
  174. if unused_count == 0:
  175. advance_src = True
  176. continue
  177. if unused_count < 0:
  178. print "Section \"" + sec + "\",",
  179. print "Description \"" + desc + "\":",
  180. print "unused size mismatch near \"",
  181. print d_item["field"] + "\""
  182. bump_taint()
  183. break
  184. continue
  185. if not check_fields_match(desc, s_item["field"], d_item["field"]):
  186. # Some fields were put in substructs, keeping the
  187. # on-wire format the same, but breaking static tools
  188. # like this one.
  189. # First, check if dest has a new substruct.
  190. if exists_in_substruct(d_item, s_item["field"]):
  191. # listiterators don't have a prev() function, so we
  192. # have to store our current location, descend into the
  193. # substruct, and ensure we come out as if nothing
  194. # happened when the substruct is over.
  195. #
  196. # Essentially we're opening the substructs that got
  197. # added which didn't change the wire format.
  198. d_iter_list.append(d_iter)
  199. substruct_fields = d_item["Description"]["Fields"]
  200. d_iter = iter(substruct_fields)
  201. advance_src = False
  202. continue
  203. # Next, check if src has substruct that dest removed
  204. # (can happen in backward migration: 2.0 -> 1.5)
  205. if exists_in_substruct(s_item, d_item["field"]):
  206. s_iter_list.append(s_iter)
  207. substruct_fields = s_item["Description"]["Fields"]
  208. s_iter = iter(substruct_fields)
  209. advance_dest = False
  210. continue
  211. if s_item["field"] == "unused" or d_item["field"] == "unused":
  212. if s_item["size"] == d_item["size"]:
  213. continue
  214. if d_item["field"] == "unused":
  215. advance_dest = False
  216. unused_count = d_item["size"] - s_item["size"]
  217. continue
  218. if s_item["field"] == "unused":
  219. advance_src = False
  220. unused_count = s_item["size"] - d_item["size"]
  221. continue
  222. print "Section \"" + sec + "\",",
  223. print "Description \"" + desc + "\":",
  224. print "expected field \"" + s_item["field"] + "\",",
  225. print "got \"" + d_item["field"] + "\"; skipping rest"
  226. bump_taint()
  227. break
  228. check_version(s_item, d_item, sec, desc)
  229. if not "Description" in s_item:
  230. # Check size of this field only if it's not a VMSTRUCT entry
  231. check_size(s_item, d_item, sec, desc, s_item["field"])
  232. check_description_in_list(s_item, d_item, sec, desc)
  233. def check_subsections(src_sub, dest_sub, desc, sec):
  234. for s_item in src_sub:
  235. found = False
  236. for d_item in dest_sub:
  237. if s_item["name"] != d_item["name"]:
  238. continue
  239. found = True
  240. check_descriptions(s_item, d_item, sec)
  241. if not found:
  242. print "Section \"" + sec + "\", Description \"" + desc + "\":",
  243. print "Subsection \"" + s_item["name"] + "\" not found"
  244. bump_taint()
  245. def check_description_in_list(s_item, d_item, sec, desc):
  246. if not "Description" in s_item:
  247. return
  248. if not "Description" in d_item:
  249. print "Section \"" + sec + "\", Description \"" + desc + "\",",
  250. print "Field \"" + s_item["field"] + "\": missing description"
  251. bump_taint()
  252. return
  253. check_descriptions(s_item["Description"], d_item["Description"], sec)
  254. def check_descriptions(src_desc, dest_desc, sec):
  255. check_version(src_desc, dest_desc, sec, src_desc["name"])
  256. if not check_fields_match(sec, src_desc["name"], dest_desc["name"]):
  257. print "Section \"" + sec + "\":",
  258. print "Description \"" + src_desc["name"] + "\"",
  259. print "missing, got \"" + dest_desc["name"] + "\" instead; skipping"
  260. bump_taint()
  261. return
  262. for f in src_desc:
  263. if not f in dest_desc:
  264. print "Section \"" + sec + "\"",
  265. print "Description \"" + src_desc["name"] + "\":",
  266. print "Entry \"" + f + "\" missing"
  267. bump_taint()
  268. continue
  269. if f == 'Fields':
  270. check_fields(src_desc[f], dest_desc[f], src_desc["name"], sec)
  271. if f == 'Subsections':
  272. check_subsections(src_desc[f], dest_desc[f], src_desc["name"], sec)
  273. def check_version(s, d, sec, desc=None):
  274. if s["version_id"] > d["version_id"]:
  275. print "Section \"" + sec + "\"",
  276. if desc:
  277. print "Description \"" + desc + "\":",
  278. print "version error:", s["version_id"], ">", d["version_id"]
  279. bump_taint()
  280. if not "minimum_version_id" in d:
  281. return
  282. if s["version_id"] < d["minimum_version_id"]:
  283. print "Section \"" + sec + "\"",
  284. if desc:
  285. print "Description \"" + desc + "\":",
  286. print "minimum version error:", s["version_id"], "<",
  287. print d["minimum_version_id"]
  288. bump_taint()
  289. def check_size(s, d, sec, desc=None, field=None):
  290. if s["size"] != d["size"]:
  291. print "Section \"" + sec + "\"",
  292. if desc:
  293. print "Description \"" + desc + "\"",
  294. if field:
  295. print "Field \"" + field + "\"",
  296. print "size mismatch:", s["size"], ",", d["size"]
  297. bump_taint()
  298. def check_machine_type(s, d):
  299. if s["Name"] != d["Name"]:
  300. print "Warning: checking incompatible machine types:",
  301. print "\"" + s["Name"] + "\", \"" + d["Name"] + "\""
  302. return
  303. def main():
  304. 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."
  305. parser = argparse.ArgumentParser(description=help_text)
  306. parser.add_argument('-s', '--src', type=file, required=True,
  307. help='json dump from src qemu')
  308. parser.add_argument('-d', '--dest', type=file, required=True,
  309. help='json dump from dest qemu')
  310. parser.add_argument('--reverse', required=False, default=False,
  311. action='store_true',
  312. help='reverse the direction')
  313. args = parser.parse_args()
  314. src_data = json.load(args.src)
  315. dest_data = json.load(args.dest)
  316. args.src.close()
  317. args.dest.close()
  318. if args.reverse:
  319. temp = src_data
  320. src_data = dest_data
  321. dest_data = temp
  322. for sec in src_data:
  323. dest_sec = sec
  324. if not dest_sec in dest_data:
  325. # Either the section name got changed, or the section
  326. # doesn't exist in dest.
  327. dest_sec = get_changed_sec_name(sec)
  328. if not dest_sec in dest_data:
  329. print "Section \"" + sec + "\" does not exist in dest"
  330. bump_taint()
  331. continue
  332. s = src_data[sec]
  333. d = dest_data[dest_sec]
  334. if sec == "vmschkmachine":
  335. check_machine_type(s, d)
  336. continue
  337. check_version(s, d, sec)
  338. for entry in s:
  339. if not entry in d:
  340. print "Section \"" + sec + "\": Entry \"" + entry + "\"",
  341. print "missing"
  342. bump_taint()
  343. continue
  344. if entry == "Description":
  345. check_descriptions(s[entry], d[entry], sec)
  346. return taint
  347. if __name__ == '__main__':
  348. sys.exit(main())