vmstate-static-checker.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. 'e1000': ['dev', 'parent_obj'],
  39. 'ehci': ['dev', 'pcidev'],
  40. 'I440FX': ['dev', 'parent_obj'],
  41. 'ich9_ahci': ['card', 'parent_obj'],
  42. 'ioh-3240-express-root-port': ['port.br.dev',
  43. 'parent_obj.parent_obj.parent_obj',
  44. 'port.br.dev.exp.aer_log',
  45. 'parent_obj.parent_obj.parent_obj.exp.aer_log'],
  46. 'mch': ['d', 'parent_obj'],
  47. 'pci_bridge': ['bridge.dev', 'parent_obj', 'bridge.dev.shpc', 'shpc'],
  48. 'pcnet': ['pci_dev', 'parent_obj'],
  49. 'PIIX3': ['pci_irq_levels', 'pci_irq_levels_vmstate'],
  50. 'piix4_pm': ['dev', 'parent_obj', 'pci0_status',
  51. 'acpi_pci_hotplug.acpi_pcihp_pci_status[0x0]'],
  52. 'rtl8139': ['dev', 'parent_obj'],
  53. 'qxl': ['num_surfaces', 'ssd.num_surfaces'],
  54. 'usb-host': ['dev', 'parent_obj'],
  55. 'usb-mouse': ['usb-ptr-queue', 'HIDPointerEventQueue'],
  56. 'usb-tablet': ['usb-ptr-queue', 'HIDPointerEventQueue'],
  57. 'xhci': ['pci_dev', 'parent_obj'],
  58. 'xio3130-express-downstream-port': ['port.br.dev',
  59. 'parent_obj.parent_obj.parent_obj',
  60. 'port.br.dev.exp.aer_log',
  61. 'parent_obj.parent_obj.parent_obj.exp.aer_log'],
  62. 'xio3130-express-upstream-port': ['br.dev', 'parent_obj.parent_obj',
  63. 'br.dev.exp.aer_log',
  64. 'parent_obj.parent_obj.exp.aer_log'],
  65. }
  66. if not name in changed_names:
  67. return False
  68. if s_field in changed_names[name] and d_field in changed_names[name]:
  69. return True
  70. return False
  71. def exists_in_substruct(fields, item):
  72. # Some QEMU versions moved a few fields inside a substruct. This
  73. # kept the on-wire format the same. This function checks if
  74. # something got shifted inside a substruct. For example, the
  75. # change in commit 1f42d22233b4f3d1a2933ff30e8d6a6d9ee2d08f
  76. if not "Description" in fields:
  77. return False
  78. if not "Fields" in fields["Description"]:
  79. return False
  80. substruct_fields = fields["Description"]["Fields"]
  81. if substruct_fields == []:
  82. return False
  83. return check_fields_match(fields["Description"]["name"],
  84. substruct_fields[0]["field"], item)
  85. def check_fields(src_fields, dest_fields, desc, sec):
  86. # This function checks for all the fields in a section. If some
  87. # fields got embedded into a substruct, this function will also
  88. # attempt to check inside the substruct.
  89. d_iter = iter(dest_fields)
  90. s_iter = iter(src_fields)
  91. # Using these lists as stacks to store previous value of s_iter
  92. # and d_iter, so that when time comes to exit out of a substruct,
  93. # we can go back one level up and continue from where we left off.
  94. s_iter_list = []
  95. d_iter_list = []
  96. advance_src = True
  97. advance_dest = True
  98. while True:
  99. if advance_src:
  100. try:
  101. s_item = s_iter.next()
  102. except StopIteration:
  103. if s_iter_list == []:
  104. break
  105. s_iter = s_iter_list.pop()
  106. continue
  107. else:
  108. # We want to avoid advancing just once -- when entering a
  109. # dest substruct, or when exiting one.
  110. advance_src = True
  111. if advance_dest:
  112. try:
  113. d_item = d_iter.next()
  114. except StopIteration:
  115. if d_iter_list == []:
  116. # We were not in a substruct
  117. print "Section \"" + sec + "\",",
  118. print "Description " + "\"" + desc + "\":",
  119. print "expected field \"" + s_item["field"] + "\",",
  120. print "while dest has no further fields"
  121. bump_taint()
  122. break
  123. d_iter = d_iter_list.pop()
  124. advance_src = False
  125. continue
  126. else:
  127. advance_dest = True
  128. if not check_fields_match(desc, s_item["field"], d_item["field"]):
  129. # Some fields were put in substructs, keeping the
  130. # on-wire format the same, but breaking static tools
  131. # like this one.
  132. # First, check if dest has a new substruct.
  133. if exists_in_substruct(d_item, s_item["field"]):
  134. # listiterators don't have a prev() function, so we
  135. # have to store our current location, descend into the
  136. # substruct, and ensure we come out as if nothing
  137. # happened when the substruct is over.
  138. #
  139. # Essentially we're opening the substructs that got
  140. # added which didn't change the wire format.
  141. d_iter_list.append(d_iter)
  142. substruct_fields = d_item["Description"]["Fields"]
  143. d_iter = iter(substruct_fields)
  144. advance_src = False
  145. continue
  146. # Next, check if src has substruct that dest removed
  147. # (can happen in backward migration: 2.0 -> 1.5)
  148. if exists_in_substruct(s_item, d_item["field"]):
  149. s_iter_list.append(s_iter)
  150. substruct_fields = s_item["Description"]["Fields"]
  151. s_iter = iter(substruct_fields)
  152. advance_dest = False
  153. continue
  154. print "Section \"" + sec + "\",",
  155. print "Description \"" + desc + "\":",
  156. print "expected field \"" + s_item["field"] + "\",",
  157. print "got \"" + d_item["field"] + "\"; skipping rest"
  158. bump_taint()
  159. break
  160. check_version(s_item, d_item, sec, desc)
  161. if not "Description" in s_item:
  162. # Check size of this field only if it's not a VMSTRUCT entry
  163. check_size(s_item, d_item, sec, desc, s_item["field"])
  164. check_description_in_list(s_item, d_item, sec, desc)
  165. def check_subsections(src_sub, dest_sub, desc, sec):
  166. for s_item in src_sub:
  167. found = False
  168. for d_item in dest_sub:
  169. if s_item["name"] != d_item["name"]:
  170. continue
  171. found = True
  172. check_descriptions(s_item, d_item, sec)
  173. if not found:
  174. print "Section \"" + sec + "\", Description \"" + desc + "\":",
  175. print "Subsection \"" + s_item["name"] + "\" not found"
  176. bump_taint()
  177. def check_description_in_list(s_item, d_item, sec, desc):
  178. if not "Description" in s_item:
  179. return
  180. if not "Description" in d_item:
  181. print "Section \"" + sec + "\", Description \"" + desc + "\",",
  182. print "Field \"" + s_item["field"] + "\": missing description"
  183. bump_taint()
  184. return
  185. check_descriptions(s_item["Description"], d_item["Description"], sec)
  186. def check_descriptions(src_desc, dest_desc, sec):
  187. check_version(src_desc, dest_desc, sec, src_desc["name"])
  188. if not check_fields_match(sec, src_desc["name"], dest_desc["name"]):
  189. print "Section \"" + sec + "\":",
  190. print "Description \"" + src_desc["name"] + "\"",
  191. print "missing, got \"" + dest_desc["name"] + "\" instead; skipping"
  192. bump_taint()
  193. return
  194. for f in src_desc:
  195. if not f in dest_desc:
  196. print "Section \"" + sec + "\"",
  197. print "Description \"" + src_desc["name"] + "\":",
  198. print "Entry \"" + f + "\" missing"
  199. bump_taint()
  200. continue
  201. if f == 'Fields':
  202. check_fields(src_desc[f], dest_desc[f], src_desc["name"], sec)
  203. if f == 'Subsections':
  204. check_subsections(src_desc[f], dest_desc[f], src_desc["name"], sec)
  205. def check_version(s, d, sec, desc=None):
  206. if s["version_id"] > d["version_id"]:
  207. print "Section \"" + sec + "\"",
  208. if desc:
  209. print "Description \"" + desc + "\":",
  210. print "version error:", s["version_id"], ">", d["version_id"]
  211. bump_taint()
  212. if not "minimum_version_id" in d:
  213. return
  214. if s["version_id"] < d["minimum_version_id"]:
  215. print "Section \"" + sec + "\"",
  216. if desc:
  217. print "Description \"" + desc + "\":",
  218. print "minimum version error:", s["version_id"], "<",
  219. print d["minimum_version_id"]
  220. bump_taint()
  221. def check_size(s, d, sec, desc=None, field=None):
  222. if s["size"] != d["size"]:
  223. print "Section \"" + sec + "\"",
  224. if desc:
  225. print "Description \"" + desc + "\"",
  226. if field:
  227. print "Field \"" + field + "\"",
  228. print "size mismatch:", s["size"], ",", d["size"]
  229. bump_taint()
  230. def check_machine_type(s, d):
  231. if s["Name"] != d["Name"]:
  232. print "Warning: checking incompatible machine types:",
  233. print "\"" + s["Name"] + "\", \"" + d["Name"] + "\""
  234. return
  235. def main():
  236. 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."
  237. parser = argparse.ArgumentParser(description=help_text)
  238. parser.add_argument('-s', '--src', type=file, required=True,
  239. help='json dump from src qemu')
  240. parser.add_argument('-d', '--dest', type=file, required=True,
  241. help='json dump from dest qemu')
  242. parser.add_argument('--reverse', required=False, default=False,
  243. action='store_true',
  244. help='reverse the direction')
  245. args = parser.parse_args()
  246. src_data = json.load(args.src)
  247. dest_data = json.load(args.dest)
  248. args.src.close()
  249. args.dest.close()
  250. if args.reverse:
  251. temp = src_data
  252. src_data = dest_data
  253. dest_data = temp
  254. for sec in src_data:
  255. if not sec in dest_data:
  256. print "Section \"" + sec + "\" does not exist in dest"
  257. bump_taint()
  258. continue
  259. s = src_data[sec]
  260. d = dest_data[sec]
  261. if sec == "vmschkmachine":
  262. check_machine_type(s, d)
  263. continue
  264. check_version(s, d, sec)
  265. for entry in s:
  266. if not entry in d:
  267. print "Section \"" + sec + "\": Entry \"" + entry + "\"",
  268. print "missing"
  269. bump_taint()
  270. continue
  271. if entry == "Description":
  272. check_descriptions(s[entry], d[entry], sec)
  273. return taint
  274. if __name__ == '__main__':
  275. sys.exit(main())