1""" 2QAPI command marshaller generator 3 4Copyright IBM, Corp. 2011 5Copyright (C) 2014-2018 Red Hat, Inc. 6 7Authors: 8 Anthony Liguori <aliguori@us.ibm.com> 9 Michael Roth <mdroth@linux.vnet.ibm.com> 10 Markus Armbruster <armbru@redhat.com> 11 12This work is licensed under the terms of the GNU GPL, version 2. 13See the COPYING file in the top-level directory. 14""" 15 16from qapi.common import * 17from qapi.gen import QAPIGenCCode, QAPISchemaModularCVisitor, ifcontext 18 19 20def gen_command_decl(name, arg_type, boxed, ret_type): 21 return mcgen(''' 22%(c_type)s qmp_%(c_name)s(%(params)s); 23''', 24 c_type=(ret_type and ret_type.c_type()) or 'void', 25 c_name=c_name(name), 26 params=build_params(arg_type, boxed, 'Error **errp')) 27 28 29def gen_call(name, arg_type, boxed, ret_type): 30 ret = '' 31 32 argstr = '' 33 if boxed: 34 assert arg_type 35 argstr = '&arg, ' 36 elif arg_type: 37 assert not arg_type.variants 38 for memb in arg_type.members: 39 if memb.optional: 40 argstr += 'arg.has_%s, ' % c_name(memb.name) 41 argstr += 'arg.%s, ' % c_name(memb.name) 42 43 lhs = '' 44 if ret_type: 45 lhs = 'retval = ' 46 47 ret = mcgen(''' 48 49 %(lhs)sqmp_%(c_name)s(%(args)s&err); 50''', 51 c_name=c_name(name), args=argstr, lhs=lhs) 52 if ret_type: 53 ret += mcgen(''' 54 if (err) { 55 goto out; 56 } 57 58 qmp_marshal_output_%(c_name)s(retval, ret, &err); 59''', 60 c_name=ret_type.c_name()) 61 return ret 62 63 64def gen_marshal_output(ret_type): 65 return mcgen(''' 66 67static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp) 68{ 69 Error *err = NULL; 70 Visitor *v; 71 72 v = qobject_output_visitor_new(ret_out); 73 visit_type_%(c_name)s(v, "unused", &ret_in, &err); 74 if (!err) { 75 visit_complete(v, ret_out); 76 } 77 error_propagate(errp, err); 78 visit_free(v); 79 v = qapi_dealloc_visitor_new(); 80 visit_type_%(c_name)s(v, "unused", &ret_in, NULL); 81 visit_free(v); 82} 83''', 84 c_type=ret_type.c_type(), c_name=ret_type.c_name()) 85 86 87def build_marshal_proto(name): 88 return ('void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' 89 % c_name(name)) 90 91 92def gen_marshal_decl(name): 93 return mcgen(''' 94%(proto)s; 95''', 96 proto=build_marshal_proto(name)) 97 98 99def gen_marshal(name, arg_type, boxed, ret_type): 100 have_args = boxed or (arg_type and not arg_type.is_empty()) 101 102 ret = mcgen(''' 103 104%(proto)s 105{ 106 Error *err = NULL; 107''', 108 proto=build_marshal_proto(name)) 109 110 if ret_type: 111 ret += mcgen(''' 112 %(c_type)s retval; 113''', 114 c_type=ret_type.c_type()) 115 116 if have_args: 117 visit_members = ('visit_type_%s_members(v, &arg, &err);' 118 % arg_type.c_name()) 119 ret += mcgen(''' 120 Visitor *v; 121 %(c_name)s arg = {0}; 122 123''', 124 c_name=arg_type.c_name()) 125 else: 126 visit_members = '' 127 ret += mcgen(''' 128 Visitor *v = NULL; 129 130 if (args) { 131''') 132 push_indent() 133 134 ret += mcgen(''' 135 v = qobject_input_visitor_new(QOBJECT(args)); 136 visit_start_struct(v, NULL, NULL, 0, &err); 137 if (err) { 138 goto out; 139 } 140 %(visit_members)s 141 if (!err) { 142 visit_check_struct(v, &err); 143 } 144 visit_end_struct(v, NULL); 145 if (err) { 146 goto out; 147 } 148''', 149 visit_members=visit_members) 150 151 if not have_args: 152 pop_indent() 153 ret += mcgen(''' 154 } 155''') 156 157 ret += gen_call(name, arg_type, boxed, ret_type) 158 159 ret += mcgen(''' 160 161out: 162 error_propagate(errp, err); 163 visit_free(v); 164''') 165 166 if have_args: 167 visit_members = ('visit_type_%s_members(v, &arg, NULL);' 168 % arg_type.c_name()) 169 else: 170 visit_members = '' 171 ret += mcgen(''' 172 if (args) { 173''') 174 push_indent() 175 176 ret += mcgen(''' 177 v = qapi_dealloc_visitor_new(); 178 visit_start_struct(v, NULL, NULL, 0, NULL); 179 %(visit_members)s 180 visit_end_struct(v, NULL); 181 visit_free(v); 182''', 183 visit_members=visit_members) 184 185 if not have_args: 186 pop_indent() 187 ret += mcgen(''' 188 } 189''') 190 191 ret += mcgen(''' 192} 193''') 194 return ret 195 196 197def gen_register_command(name, success_response, allow_oob, allow_preconfig): 198 options = [] 199 200 if not success_response: 201 options += ['QCO_NO_SUCCESS_RESP'] 202 if allow_oob: 203 options += ['QCO_ALLOW_OOB'] 204 if allow_preconfig: 205 options += ['QCO_ALLOW_PRECONFIG'] 206 207 if not options: 208 options = ['QCO_NO_OPTIONS'] 209 210 options = " | ".join(options) 211 212 ret = mcgen(''' 213 qmp_register_command(cmds, "%(name)s", 214 qmp_marshal_%(c_name)s, %(opts)s); 215''', 216 name=name, c_name=c_name(name), 217 opts=options) 218 return ret 219 220 221def gen_registry(registry, prefix): 222 ret = mcgen(''' 223 224void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds) 225{ 226 QTAILQ_INIT(cmds); 227 228''', 229 c_prefix=c_name(prefix, protect=False)) 230 ret += registry 231 ret += mcgen(''' 232} 233''') 234 return ret 235 236 237class QAPISchemaGenCommandVisitor(QAPISchemaModularCVisitor): 238 239 def __init__(self, prefix): 240 QAPISchemaModularCVisitor.__init__( 241 self, prefix, 'qapi-commands', 242 ' * Schema-defined QAPI/QMP commands', __doc__) 243 self._regy = QAPIGenCCode(None) 244 self._visited_ret_types = {} 245 246 def _begin_user_module(self, name): 247 self._visited_ret_types[self._genc] = set() 248 commands = self._module_basename('qapi-commands', name) 249 types = self._module_basename('qapi-types', name) 250 visit = self._module_basename('qapi-visit', name) 251 self._genc.add(mcgen(''' 252#include "qemu/osdep.h" 253#include "qapi/visitor.h" 254#include "qapi/qmp/qdict.h" 255#include "qapi/qobject-output-visitor.h" 256#include "qapi/qobject-input-visitor.h" 257#include "qapi/dealloc-visitor.h" 258#include "qapi/error.h" 259#include "%(visit)s.h" 260#include "%(commands)s.h" 261 262''', 263 commands=commands, visit=visit)) 264 self._genh.add(mcgen(''' 265#include "%(types)s.h" 266#include "qapi/qmp/dispatch.h" 267 268''', 269 types=types)) 270 271 def visit_end(self): 272 (genc, genh) = self._module[self._main_module] 273 genh.add(mcgen(''' 274void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds); 275''', 276 c_prefix=c_name(self._prefix, protect=False))) 277 genc.add(gen_registry(self._regy.get_content(), self._prefix)) 278 279 def visit_command(self, name, info, ifcond, arg_type, ret_type, gen, 280 success_response, boxed, allow_oob, allow_preconfig, 281 features): 282 if not gen: 283 return 284 # FIXME: If T is a user-defined type, the user is responsible 285 # for making this work, i.e. to make T's condition the 286 # conjunction of the T-returning commands' conditions. If T 287 # is a built-in type, this isn't possible: the 288 # qmp_marshal_output_T() will be generated unconditionally. 289 if ret_type and ret_type not in self._visited_ret_types[self._genc]: 290 self._visited_ret_types[self._genc].add(ret_type) 291 with ifcontext(ret_type.ifcond, 292 self._genh, self._genc, self._regy): 293 self._genc.add(gen_marshal_output(ret_type)) 294 with ifcontext(ifcond, self._genh, self._genc, self._regy): 295 self._genh.add(gen_command_decl(name, arg_type, boxed, ret_type)) 296 self._genh.add(gen_marshal_decl(name)) 297 self._genc.add(gen_marshal(name, arg_type, boxed, ret_type)) 298 self._regy.add(gen_register_command(name, success_response, 299 allow_oob, allow_preconfig)) 300 301 302def gen_commands(schema, output_dir, prefix): 303 vis = QAPISchemaGenCommandVisitor(prefix) 304 schema.visit(vis) 305 vis.write(output_dir) 306