vmstate-static-checker.py 15 KB

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