qapi-commands.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. #
  2. # QAPI command marshaller generator
  3. #
  4. # Copyright IBM, Corp. 2011
  5. # Copyright (C) 2014 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 option_value_matches(opt, val, cmd):
  255. if opt in cmd and cmd[opt] == val:
  256. return True
  257. return False
  258. def gen_registry(commands):
  259. registry=""
  260. push_indent()
  261. for cmd in commands:
  262. options = 'QCO_NO_OPTIONS'
  263. if option_value_matches('success-response', 'no', cmd):
  264. options = 'QCO_NO_SUCCESS_RESP'
  265. registry += mcgen('''
  266. qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
  267. ''',
  268. name=cmd['command'], c_name=c_fun(cmd['command']),
  269. opts=options)
  270. pop_indent()
  271. ret = mcgen('''
  272. static void qmp_init_marshal(void)
  273. {
  274. %(registry)s
  275. }
  276. qapi_init(qmp_init_marshal);
  277. ''',
  278. registry=registry.rstrip())
  279. return ret
  280. def gen_command_decl_prologue(header, guard, prefix=""):
  281. ret = mcgen('''
  282. /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
  283. /*
  284. * schema-defined QAPI function prototypes
  285. *
  286. * Copyright IBM, Corp. 2011
  287. *
  288. * Authors:
  289. * Anthony Liguori <aliguori@us.ibm.com>
  290. *
  291. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  292. * See the COPYING.LIB file in the top-level directory.
  293. *
  294. */
  295. #ifndef %(guard)s
  296. #define %(guard)s
  297. #include "%(prefix)sqapi-types.h"
  298. #include "qapi/qmp/qdict.h"
  299. #include "qapi/error.h"
  300. ''',
  301. header=basename(header), guard=guardname(header), prefix=prefix)
  302. return ret
  303. def gen_command_def_prologue(prefix="", proxy=False):
  304. ret = mcgen('''
  305. /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
  306. /*
  307. * schema-defined QMP->QAPI command dispatch
  308. *
  309. * Copyright IBM, Corp. 2011
  310. *
  311. * Authors:
  312. * Anthony Liguori <aliguori@us.ibm.com>
  313. *
  314. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  315. * See the COPYING.LIB file in the top-level directory.
  316. *
  317. */
  318. #include "qemu-common.h"
  319. #include "qemu/module.h"
  320. #include "qapi/qmp/qerror.h"
  321. #include "qapi/qmp/types.h"
  322. #include "qapi/qmp/dispatch.h"
  323. #include "qapi/visitor.h"
  324. #include "qapi/qmp-output-visitor.h"
  325. #include "qapi/qmp-input-visitor.h"
  326. #include "qapi/dealloc-visitor.h"
  327. #include "%(prefix)sqapi-types.h"
  328. #include "%(prefix)sqapi-visit.h"
  329. ''',
  330. prefix=prefix)
  331. if not proxy:
  332. ret += '#include "%sqmp-commands.h"' % prefix
  333. return ret + "\n\n"
  334. try:
  335. opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:i:o:m",
  336. ["source", "header", "prefix=",
  337. "input-file=", "output-dir=",
  338. "type=", "middle"])
  339. except getopt.GetoptError, err:
  340. print str(err)
  341. sys.exit(1)
  342. output_dir = ""
  343. prefix = ""
  344. dispatch_type = "sync"
  345. c_file = 'qmp-marshal.c'
  346. h_file = 'qmp-commands.h'
  347. middle_mode = False
  348. do_c = False
  349. do_h = False
  350. for o, a in opts:
  351. if o in ("-p", "--prefix"):
  352. prefix = a
  353. elif o in ("-i", "--input-file"):
  354. input_file = a
  355. elif o in ("-o", "--output-dir"):
  356. output_dir = a + "/"
  357. elif o in ("-t", "--type"):
  358. dispatch_type = a
  359. elif o in ("-m", "--middle"):
  360. middle_mode = True
  361. elif o in ("-c", "--source"):
  362. do_c = True
  363. elif o in ("-h", "--header"):
  364. do_h = True
  365. if not do_c and not do_h:
  366. do_c = True
  367. do_h = True
  368. c_file = output_dir + prefix + c_file
  369. h_file = output_dir + prefix + h_file
  370. def maybe_open(really, name, opt):
  371. if really:
  372. return open(name, opt)
  373. else:
  374. import StringIO
  375. return StringIO.StringIO()
  376. try:
  377. os.makedirs(output_dir)
  378. except os.error, e:
  379. if e.errno != errno.EEXIST:
  380. raise
  381. exprs = parse_schema(input_file)
  382. commands = filter(lambda expr: expr.has_key('command'), exprs)
  383. commands = filter(lambda expr: not expr.has_key('gen'), commands)
  384. if dispatch_type == "sync":
  385. fdecl = maybe_open(do_h, h_file, 'w')
  386. fdef = maybe_open(do_c, c_file, 'w')
  387. ret = gen_command_decl_prologue(header=basename(h_file), guard=guardname(h_file), prefix=prefix)
  388. fdecl.write(ret)
  389. ret = gen_command_def_prologue(prefix=prefix)
  390. fdef.write(ret)
  391. for cmd in commands:
  392. arglist = []
  393. ret_type = None
  394. if cmd.has_key('data'):
  395. arglist = cmd['data']
  396. if cmd.has_key('returns'):
  397. ret_type = cmd['returns']
  398. ret = generate_command_decl(cmd['command'], arglist, ret_type) + "\n"
  399. fdecl.write(ret)
  400. if ret_type:
  401. ret = gen_marshal_output(cmd['command'], arglist, ret_type, middle_mode) + "\n"
  402. fdef.write(ret)
  403. if middle_mode:
  404. fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], arglist, ret_type, middle_mode))
  405. ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
  406. fdef.write(ret)
  407. fdecl.write("\n#endif\n");
  408. if not middle_mode:
  409. ret = gen_registry(commands)
  410. fdef.write(ret)
  411. fdef.flush()
  412. fdef.close()
  413. fdecl.flush()
  414. fdecl.close()