2
0

vmstate-static-checker.py 13 KB

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