vmstate-static-checker.py 15 KB

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