qapi-commands.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. #
  2. # QAPI command marshaller generator
  3. #
  4. # Copyright IBM, Corp. 2011
  5. # Copyright (C) 2014-2015 Red Hat, Inc.
  6. #
  7. # Authors:
  8. # Anthony Liguori <aliguori@us.ibm.com>
  9. # Michael Roth <mdroth@linux.vnet.ibm.com>
  10. # Markus Armbruster <armbru@redhat.com>
  11. #
  12. # This work is licensed under the terms of the GNU GPL, version 2.
  13. # See the COPYING file in the top-level directory.
  14. from ordereddict import OrderedDict
  15. from qapi import *
  16. import re
  17. import sys
  18. import os
  19. import getopt
  20. import errno
  21. def type_visitor(name):
  22. if type(name) == list:
  23. return 'visit_type_%sList' % name[0]
  24. else:
  25. return 'visit_type_%s' % name
  26. def generate_command_decl(name, args, ret_type):
  27. arglist=""
  28. for argname, argtype, optional, structured in parse_args(args):
  29. argtype = c_type(argtype, is_param=True)
  30. if optional:
  31. arglist += "bool has_%s, " % c_var(argname)
  32. arglist += "%s %s, " % (argtype, c_var(argname))
  33. return mcgen('''
  34. %(ret_type)s qmp_%(name)s(%(args)sError **errp);
  35. ''',
  36. ret_type=c_type(ret_type), name=c_fun(name), args=arglist).strip()
  37. def gen_err_check(errvar):
  38. if errvar:
  39. return mcgen('''
  40. if (local_err) {
  41. goto out;
  42. }
  43. ''')
  44. return ''
  45. def gen_sync_call(name, args, ret_type, indent=0):
  46. ret = ""
  47. arglist=""
  48. retval=""
  49. if ret_type:
  50. retval = "retval = "
  51. for argname, argtype, optional, structured in parse_args(args):
  52. if optional:
  53. arglist += "has_%s, " % c_var(argname)
  54. arglist += "%s, " % (c_var(argname))
  55. push_indent(indent)
  56. ret = mcgen('''
  57. %(retval)sqmp_%(name)s(%(args)s&local_err);
  58. ''',
  59. name=c_fun(name), args=arglist, retval=retval).rstrip()
  60. if ret_type:
  61. ret += "\n" + gen_err_check('local_err')
  62. ret += "\n" + mcgen(''''
  63. %(marshal_output_call)s
  64. ''',
  65. marshal_output_call=gen_marshal_output_call(name, ret_type)).rstrip()
  66. pop_indent(indent)
  67. return ret.rstrip()
  68. def gen_marshal_output_call(name, ret_type):
  69. if not ret_type:
  70. return ""
  71. return "qmp_marshal_output_%s(retval, ret, &local_err);" % c_fun(name)
  72. def gen_visitor_input_containers_decl(args, obj):
  73. ret = ""
  74. push_indent()
  75. if len(args) > 0:
  76. ret += mcgen('''
  77. QmpInputVisitor *mi = qmp_input_visitor_new_strict(%(obj)s);
  78. QapiDeallocVisitor *md;
  79. Visitor *v;
  80. ''',
  81. obj=obj)
  82. pop_indent()
  83. return ret.rstrip()
  84. def gen_visitor_input_vars_decl(args):
  85. ret = ""
  86. push_indent()
  87. for argname, argtype, optional, structured in parse_args(args):
  88. if optional:
  89. ret += mcgen('''
  90. bool has_%(argname)s = false;
  91. ''',
  92. argname=c_var(argname))
  93. if is_c_ptr(argtype):
  94. ret += mcgen('''
  95. %(argtype)s %(argname)s = NULL;
  96. ''',
  97. argname=c_var(argname), argtype=c_type(argtype))
  98. else:
  99. ret += mcgen('''
  100. %(argtype)s %(argname)s = {0};
  101. ''',
  102. argname=c_var(argname), argtype=c_type(argtype))
  103. pop_indent()
  104. return ret.rstrip()
  105. def gen_visitor_input_block(args, dealloc=False):
  106. ret = ""
  107. errparg = '&local_err'
  108. errarg = 'local_err'
  109. if len(args) == 0:
  110. return ret
  111. push_indent()
  112. if dealloc:
  113. errparg = 'NULL'
  114. errarg = None;
  115. ret += mcgen('''
  116. qmp_input_visitor_cleanup(mi);
  117. md = qapi_dealloc_visitor_new();
  118. v = qapi_dealloc_get_visitor(md);
  119. ''')
  120. else:
  121. ret += mcgen('''
  122. v = qmp_input_get_visitor(mi);
  123. ''')
  124. for argname, argtype, optional, structured in parse_args(args):
  125. if optional:
  126. ret += mcgen('''
  127. visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
  128. ''',
  129. c_name=c_var(argname), name=argname, errp=errparg)
  130. ret += gen_err_check(errarg)
  131. ret += mcgen('''
  132. if (has_%(c_name)s) {
  133. ''',
  134. c_name=c_var(argname))
  135. push_indent()
  136. ret += mcgen('''
  137. %(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
  138. ''',
  139. c_name=c_var(argname), name=argname, argtype=argtype,
  140. visitor=type_visitor(argtype), errp=errparg)
  141. ret += gen_err_check(errarg)
  142. if optional:
  143. pop_indent()
  144. ret += mcgen('''
  145. }
  146. ''')
  147. if dealloc:
  148. ret += mcgen('''
  149. qapi_dealloc_visitor_cleanup(md);
  150. ''')
  151. pop_indent()
  152. return ret.rstrip()
  153. def gen_marshal_output(name, args, ret_type, middle_mode):
  154. if not ret_type:
  155. return ""
  156. ret = mcgen('''
  157. static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
  158. {
  159. Error *local_err = NULL;
  160. QmpOutputVisitor *mo = qmp_output_visitor_new();
  161. QapiDeallocVisitor *md;
  162. Visitor *v;
  163. v = qmp_output_get_visitor(mo);
  164. %(visitor)s(v, &ret_in, "unused", &local_err);
  165. if (local_err) {
  166. goto out;
  167. }
  168. *ret_out = qmp_output_get_qobject(mo);
  169. out:
  170. error_propagate(errp, local_err);
  171. qmp_output_visitor_cleanup(mo);
  172. md = qapi_dealloc_visitor_new();
  173. v = qapi_dealloc_get_visitor(md);
  174. %(visitor)s(v, &ret_in, "unused", NULL);
  175. qapi_dealloc_visitor_cleanup(md);
  176. }
  177. ''',
  178. c_ret_type=c_type(ret_type), c_name=c_fun(name),
  179. visitor=type_visitor(ret_type))
  180. return ret
  181. def gen_marshal_input_decl(name, args, ret_type, middle_mode):
  182. if middle_mode:
  183. return 'int qmp_marshal_input_%s(Monitor *mon, const QDict *qdict, QObject **ret)' % c_fun(name)
  184. else:
  185. return 'static void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_fun(name)
  186. def gen_marshal_input(name, args, ret_type, middle_mode):
  187. hdr = gen_marshal_input_decl(name, args, ret_type, middle_mode)
  188. ret = mcgen('''
  189. %(header)s
  190. {
  191. Error *local_err = NULL;
  192. ''',
  193. header=hdr)
  194. if middle_mode:
  195. ret += mcgen('''
  196. QDict *args = (QDict *)qdict;
  197. ''')
  198. if ret_type:
  199. if is_c_ptr(ret_type):
  200. retval = " %s retval = NULL;" % c_type(ret_type)
  201. else:
  202. retval = " %s retval;" % c_type(ret_type)
  203. ret += mcgen('''
  204. %(retval)s
  205. ''',
  206. retval=retval)
  207. if len(args) > 0:
  208. ret += mcgen('''
  209. %(visitor_input_containers_decl)s
  210. %(visitor_input_vars_decl)s
  211. %(visitor_input_block)s
  212. ''',
  213. visitor_input_containers_decl=gen_visitor_input_containers_decl(args, "QOBJECT(args)"),
  214. visitor_input_vars_decl=gen_visitor_input_vars_decl(args),
  215. visitor_input_block=gen_visitor_input_block(args))
  216. else:
  217. ret += mcgen('''
  218. (void)args;
  219. ''')
  220. ret += mcgen('''
  221. %(sync_call)s
  222. ''',
  223. sync_call=gen_sync_call(name, args, ret_type, indent=4))
  224. if re.search('^ *goto out\\;', ret, re.MULTILINE):
  225. ret += mcgen('''
  226. out:
  227. ''')
  228. if not middle_mode:
  229. ret += mcgen('''
  230. error_propagate(errp, local_err);
  231. ''')
  232. ret += mcgen('''
  233. %(visitor_input_block_cleanup)s
  234. ''',
  235. visitor_input_block_cleanup=gen_visitor_input_block(args,
  236. dealloc=True))
  237. if middle_mode:
  238. ret += mcgen('''
  239. if (local_err) {
  240. qerror_report_err(local_err);
  241. error_free(local_err);
  242. return -1;
  243. }
  244. return 0;
  245. ''')
  246. else:
  247. ret += mcgen('''
  248. return;
  249. ''')
  250. ret += mcgen('''
  251. }
  252. ''')
  253. return ret
  254. def gen_registry(commands):
  255. registry=""
  256. push_indent()
  257. for cmd in commands:
  258. options = 'QCO_NO_OPTIONS'
  259. if not cmd.get('success-response', True):
  260. options = 'QCO_NO_SUCCESS_RESP'
  261. registry += mcgen('''
  262. qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
  263. ''',
  264. name=cmd['command'], c_name=c_fun(cmd['command']),
  265. opts=options)
  266. pop_indent()
  267. ret = mcgen('''
  268. static void qmp_init_marshal(void)
  269. {
  270. %(registry)s
  271. }
  272. qapi_init(qmp_init_marshal);
  273. ''',
  274. registry=registry.rstrip())
  275. return ret
  276. def gen_command_decl_prologue(header, guard, prefix=""):
  277. ret = mcgen('''
  278. /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
  279. /*
  280. * schema-defined QAPI function prototypes
  281. *
  282. * Copyright IBM, Corp. 2011
  283. *
  284. * Authors:
  285. * Anthony Liguori <aliguori@us.ibm.com>
  286. *
  287. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  288. * See the COPYING.LIB file in the top-level directory.
  289. *
  290. */
  291. #ifndef %(guard)s
  292. #define %(guard)s
  293. #include "%(prefix)sqapi-types.h"
  294. #include "qapi/qmp/qdict.h"
  295. #include "qapi/error.h"
  296. ''',
  297. header=basename(header), guard=guardname(header), prefix=prefix)
  298. return ret
  299. def gen_command_def_prologue(prefix="", proxy=False):
  300. ret = mcgen('''
  301. /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
  302. /*
  303. * schema-defined QMP->QAPI command dispatch
  304. *
  305. * Copyright IBM, Corp. 2011
  306. *
  307. * Authors:
  308. * Anthony Liguori <aliguori@us.ibm.com>
  309. *
  310. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  311. * See the COPYING.LIB file in the top-level directory.
  312. *
  313. */
  314. #include "qemu-common.h"
  315. #include "qemu/module.h"
  316. #include "qapi/qmp/qerror.h"
  317. #include "qapi/qmp/types.h"
  318. #include "qapi/qmp/dispatch.h"
  319. #include "qapi/visitor.h"
  320. #include "qapi/qmp-output-visitor.h"
  321. #include "qapi/qmp-input-visitor.h"
  322. #include "qapi/dealloc-visitor.h"
  323. #include "%(prefix)sqapi-types.h"
  324. #include "%(prefix)sqapi-visit.h"
  325. ''',
  326. prefix=prefix)
  327. if not proxy:
  328. ret += '#include "%sqmp-commands.h"' % prefix
  329. return ret + "\n\n"
  330. try:
  331. opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:i:o:m",
  332. ["source", "header", "prefix=",
  333. "input-file=", "output-dir=",
  334. "type=", "middle"])
  335. except getopt.GetoptError, err:
  336. print str(err)
  337. sys.exit(1)
  338. output_dir = ""
  339. prefix = ""
  340. dispatch_type = "sync"
  341. c_file = 'qmp-marshal.c'
  342. h_file = 'qmp-commands.h'
  343. middle_mode = False
  344. do_c = False
  345. do_h = False
  346. for o, a in opts:
  347. if o in ("-p", "--prefix"):
  348. prefix = a
  349. elif o in ("-i", "--input-file"):
  350. input_file = a
  351. elif o in ("-o", "--output-dir"):
  352. output_dir = a + "/"
  353. elif o in ("-t", "--type"):
  354. dispatch_type = a
  355. elif o in ("-m", "--middle"):
  356. middle_mode = True
  357. elif o in ("-c", "--source"):
  358. do_c = True
  359. elif o in ("-h", "--header"):
  360. do_h = True
  361. if not do_c and not do_h:
  362. do_c = True
  363. do_h = True
  364. c_file = output_dir + prefix + c_file
  365. h_file = output_dir + prefix + h_file
  366. def maybe_open(really, name, opt):
  367. if really:
  368. return open(name, opt)
  369. else:
  370. import StringIO
  371. return StringIO.StringIO()
  372. try:
  373. os.makedirs(output_dir)
  374. except os.error, e:
  375. if e.errno != errno.EEXIST:
  376. raise
  377. exprs = parse_schema(input_file)
  378. commands = filter(lambda expr: expr.has_key('command'), exprs)
  379. commands = filter(lambda expr: not expr.has_key('gen'), commands)
  380. if dispatch_type == "sync":
  381. fdecl = maybe_open(do_h, h_file, 'w')
  382. fdef = maybe_open(do_c, c_file, 'w')
  383. ret = gen_command_decl_prologue(header=basename(h_file), guard=guardname(h_file), prefix=prefix)
  384. fdecl.write(ret)
  385. ret = gen_command_def_prologue(prefix=prefix)
  386. fdef.write(ret)
  387. for cmd in commands:
  388. arglist = []
  389. ret_type = None
  390. if cmd.has_key('data'):
  391. arglist = cmd['data']
  392. if cmd.has_key('returns'):
  393. ret_type = cmd['returns']
  394. ret = generate_command_decl(cmd['command'], arglist, ret_type) + "\n"
  395. fdecl.write(ret)
  396. if ret_type:
  397. ret = gen_marshal_output(cmd['command'], arglist, ret_type, middle_mode) + "\n"
  398. fdef.write(ret)
  399. if middle_mode:
  400. fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], arglist, ret_type, middle_mode))
  401. ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
  402. fdef.write(ret)
  403. fdecl.write("\n#endif\n");
  404. if not middle_mode:
  405. ret = gen_registry(commands)
  406. fdef.write(ret)
  407. fdef.flush()
  408. fdef.close()
  409. fdecl.flush()
  410. fdecl.close()