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 .common import build_params, c_name, mcgen 17from .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 error_propagate(errp, err); 51''', 52 c_name=c_name(name), args=argstr, lhs=lhs) 53 if ret_type: 54 ret += mcgen(''' 55 if (err) { 56 goto out; 57 } 58 59 qmp_marshal_output_%(c_name)s(retval, ret, errp); 60''', 61 c_name=ret_type.c_name()) 62 return ret 63 64 65def gen_marshal_output(ret_type): 66 return mcgen(''' 67 68static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, 69 QObject **ret_out, Error **errp) 70{ 71 Visitor *v; 72 73 v = qobject_output_visitor_new(ret_out); 74 if (visit_type_%(c_name)s(v, "unused", &ret_in, errp)) { 75 visit_complete(v, ret_out); 76 } 77 visit_free(v); 78 v = qapi_dealloc_visitor_new(); 79 visit_type_%(c_name)s(v, "unused", &ret_in, NULL); 80 visit_free(v); 81} 82''', 83 c_type=ret_type.c_type(), c_name=ret_type.c_name()) 84 85 86def build_marshal_proto(name): 87 return ('void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' 88 % c_name(name)) 89 90 91def gen_marshal_decl(name): 92 return mcgen(''' 93%(proto)s; 94''', 95 proto=build_marshal_proto(name)) 96 97 98def gen_marshal(name, arg_type, boxed, ret_type): 99 have_args = boxed or (arg_type and not arg_type.is_empty()) 100 101 ret = mcgen(''' 102 103%(proto)s 104{ 105 Error *err = NULL; 106 bool ok = false; 107 Visitor *v; 108''', 109 proto=build_marshal_proto(name)) 110 111 if ret_type: 112 ret += mcgen(''' 113 %(c_type)s retval; 114''', 115 c_type=ret_type.c_type()) 116 117 if have_args: 118 ret += mcgen(''' 119 %(c_name)s arg = {0}; 120''', 121 c_name=arg_type.c_name()) 122 123 ret += mcgen(''' 124 125 v = qobject_input_visitor_new(QOBJECT(args)); 126 if (!visit_start_struct(v, NULL, NULL, 0, errp)) { 127 goto out; 128 } 129''') 130 131 if have_args: 132 ret += mcgen(''' 133 if (visit_type_%(c_arg_type)s_members(v, &arg, errp)) { 134 ok = visit_check_struct(v, errp); 135 } 136''', 137 c_arg_type=arg_type.c_name()) 138 else: 139 ret += mcgen(''' 140 ok = visit_check_struct(v, errp); 141''') 142 143 ret += mcgen(''' 144 visit_end_struct(v, NULL); 145 if (!ok) { 146 goto out; 147 } 148''') 149 150 ret += gen_call(name, arg_type, boxed, ret_type) 151 152 ret += mcgen(''' 153 154out: 155 visit_free(v); 156''') 157 158 ret += mcgen(''' 159 v = qapi_dealloc_visitor_new(); 160 visit_start_struct(v, NULL, NULL, 0, NULL); 161''') 162 163 if have_args: 164 ret += mcgen(''' 165 visit_type_%(c_arg_type)s_members(v, &arg, NULL); 166''', 167 c_arg_type=arg_type.c_name()) 168 169 ret += mcgen(''' 170 visit_end_struct(v, NULL); 171 visit_free(v); 172''') 173 174 ret += mcgen(''' 175} 176''') 177 return ret 178 179 180def gen_register_command(name, success_response, allow_oob, allow_preconfig, 181 coroutine): 182 options = [] 183 184 if not success_response: 185 options += ['QCO_NO_SUCCESS_RESP'] 186 if allow_oob: 187 options += ['QCO_ALLOW_OOB'] 188 if allow_preconfig: 189 options += ['QCO_ALLOW_PRECONFIG'] 190 if coroutine: 191 options += ['QCO_COROUTINE'] 192 193 if not options: 194 options = ['QCO_NO_OPTIONS'] 195 196 options = " | ".join(options) 197 198 ret = mcgen(''' 199 qmp_register_command(cmds, "%(name)s", 200 qmp_marshal_%(c_name)s, %(opts)s); 201''', 202 name=name, c_name=c_name(name), 203 opts=options) 204 return ret 205 206 207def gen_registry(registry, prefix): 208 ret = mcgen(''' 209 210void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds) 211{ 212 QTAILQ_INIT(cmds); 213 214''', 215 c_prefix=c_name(prefix, protect=False)) 216 ret += registry 217 ret += mcgen(''' 218} 219''') 220 return ret 221 222 223class QAPISchemaGenCommandVisitor(QAPISchemaModularCVisitor): 224 225 def __init__(self, prefix): 226 super().__init__( 227 prefix, 'qapi-commands', 228 ' * Schema-defined QAPI/QMP commands', None, __doc__) 229 self._regy = QAPIGenCCode(None) 230 self._visited_ret_types = {} 231 232 def _begin_user_module(self, name): 233 self._visited_ret_types[self._genc] = set() 234 commands = self._module_basename('qapi-commands', name) 235 types = self._module_basename('qapi-types', name) 236 visit = self._module_basename('qapi-visit', name) 237 self._genc.add(mcgen(''' 238#include "qemu/osdep.h" 239#include "qapi/visitor.h" 240#include "qapi/qmp/qdict.h" 241#include "qapi/qobject-output-visitor.h" 242#include "qapi/qobject-input-visitor.h" 243#include "qapi/dealloc-visitor.h" 244#include "qapi/error.h" 245#include "%(visit)s.h" 246#include "%(commands)s.h" 247 248''', 249 commands=commands, visit=visit)) 250 self._genh.add(mcgen(''' 251#include "%(types)s.h" 252 253''', 254 types=types)) 255 256 def visit_end(self): 257 self._add_system_module('init', ' * QAPI Commands initialization') 258 self._genh.add(mcgen(''' 259#include "qapi/qmp/dispatch.h" 260 261void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds); 262''', 263 c_prefix=c_name(self._prefix, protect=False))) 264 self._genc.preamble_add(mcgen(''' 265#include "qemu/osdep.h" 266#include "%(prefix)sqapi-commands.h" 267#include "%(prefix)sqapi-init-commands.h" 268''', 269 prefix=self._prefix)) 270 self._genc.add(gen_registry(self._regy.get_content(), self._prefix)) 271 272 def visit_command(self, name, info, ifcond, features, 273 arg_type, ret_type, gen, success_response, boxed, 274 allow_oob, allow_preconfig, coroutine): 275 if not gen: 276 return 277 # FIXME: If T is a user-defined type, the user is responsible 278 # for making this work, i.e. to make T's condition the 279 # conjunction of the T-returning commands' conditions. If T 280 # is a built-in type, this isn't possible: the 281 # qmp_marshal_output_T() will be generated unconditionally. 282 if ret_type and ret_type not in self._visited_ret_types[self._genc]: 283 self._visited_ret_types[self._genc].add(ret_type) 284 with ifcontext(ret_type.ifcond, 285 self._genh, self._genc, self._regy): 286 self._genc.add(gen_marshal_output(ret_type)) 287 with ifcontext(ifcond, self._genh, self._genc, self._regy): 288 self._genh.add(gen_command_decl(name, arg_type, boxed, ret_type)) 289 self._genh.add(gen_marshal_decl(name)) 290 self._genc.add(gen_marshal(name, arg_type, boxed, ret_type)) 291 self._regy.add(gen_register_command(name, success_response, 292 allow_oob, allow_preconfig, 293 coroutine)) 294 295 296def gen_commands(schema, output_dir, prefix): 297 vis = QAPISchemaGenCommandVisitor(prefix) 298 schema.visit(vis) 299 vis.write(output_dir) 300