10e33e3d2SDaniel P. BerrangéHow to write monitor commands 20e33e3d2SDaniel P. Berrangé============================= 34b389b5dSLuiz Capitulino 44b389b5dSLuiz CapitulinoThis document is a step-by-step guide on how to write new QMP commands using 50e33e3d2SDaniel P. Berrangéthe QAPI framework and HMP commands. 64b389b5dSLuiz Capitulino 74b389b5dSLuiz CapitulinoThis document doesn't discuss QMP protocol level details, nor does it dive 84b389b5dSLuiz Capitulinointo the QAPI framework implementation. 94b389b5dSLuiz Capitulino 104b389b5dSLuiz CapitulinoFor an in-depth introduction to the QAPI framework, please refer to 11b3125e73SPhilippe Mathieu-Daudédocs/devel/qapi-code-gen.txt. For documentation about the QMP protocol, 12cfb41b88SCleber Rosastart with docs/interop/qmp-intro.txt. 134b389b5dSLuiz Capitulino 1468e6dc59SJohn Snow 1568e6dc59SJohn SnowOverview 1668e6dc59SJohn Snow-------- 174b389b5dSLuiz Capitulino 184b389b5dSLuiz CapitulinoGenerally speaking, the following steps should be taken in order to write a 194b389b5dSLuiz Capitulinonew QMP command. 204b389b5dSLuiz Capitulino 21bfe873e9SMarkus Armbruster1. Define the command and any types it needs in the appropriate QAPI 22bfe873e9SMarkus Armbruster schema module. 234b389b5dSLuiz Capitulino 244b389b5dSLuiz Capitulino2. Write the QMP command itself, which is a regular C function. Preferably, 254b389b5dSLuiz Capitulino the command should be exported by some QEMU subsystem. But it can also be 26f1b3ccfaSKevin Wolf added to the monitor/qmp-cmds.c file 274b389b5dSLuiz Capitulino 284b389b5dSLuiz Capitulino3. At this point the command can be tested under the QMP protocol 294b389b5dSLuiz Capitulino 304b389b5dSLuiz Capitulino4. Write the HMP command equivalent. This is not required and should only be 314b389b5dSLuiz Capitulino done if it does make sense to have the functionality in HMP. The HMP command 324b389b5dSLuiz Capitulino is implemented in terms of the QMP command 334b389b5dSLuiz Capitulino 344b389b5dSLuiz CapitulinoThe following sections will demonstrate each of the steps above. We will start 354b389b5dSLuiz Capitulinovery simple and get more complex as we progress. 364b389b5dSLuiz Capitulino 3768e6dc59SJohn Snow 3868e6dc59SJohn SnowTesting 3968e6dc59SJohn Snow------- 404b389b5dSLuiz Capitulino 414b389b5dSLuiz CapitulinoFor all the examples in the next sections, the test setup is the same and is 424b389b5dSLuiz Capitulinoshown here. 434b389b5dSLuiz Capitulino 4468e6dc59SJohn SnowFirst, QEMU should be started like this:: 454b389b5dSLuiz Capitulino 46bb46af41SMarkus Armbruster # qemu-system-TARGET [...] \ 47c2387413SDaniel P. Berrangé -chardev socket,id=qmp,port=4444,host=localhost,server=on \ 484b389b5dSLuiz Capitulino -mon chardev=qmp,mode=control,pretty=on 494b389b5dSLuiz Capitulino 5068e6dc59SJohn SnowThen, in a different terminal:: 514b389b5dSLuiz Capitulino 524b389b5dSLuiz Capitulino $ telnet localhost 4444 534b389b5dSLuiz Capitulino Trying 127.0.0.1... 544b389b5dSLuiz Capitulino Connected to localhost. 554b389b5dSLuiz Capitulino Escape character is '^]'. 564b389b5dSLuiz Capitulino { 574b389b5dSLuiz Capitulino "QMP": { 584b389b5dSLuiz Capitulino "version": { 594b389b5dSLuiz Capitulino "qemu": { 604b389b5dSLuiz Capitulino "micro": 50, 614b389b5dSLuiz Capitulino "minor": 15, 624b389b5dSLuiz Capitulino "major": 0 634b389b5dSLuiz Capitulino }, 644b389b5dSLuiz Capitulino "package": "" 654b389b5dSLuiz Capitulino }, 664b389b5dSLuiz Capitulino "capabilities": [ 674b389b5dSLuiz Capitulino ] 684b389b5dSLuiz Capitulino } 694b389b5dSLuiz Capitulino } 704b389b5dSLuiz Capitulino 714b389b5dSLuiz CapitulinoThe above output is the QMP server saying you're connected. The server is 7268e6dc59SJohn Snowactually in capabilities negotiation mode. To enter in command mode type:: 734b389b5dSLuiz Capitulino 744b389b5dSLuiz Capitulino { "execute": "qmp_capabilities" } 754b389b5dSLuiz Capitulino 7668e6dc59SJohn SnowThen the server should respond:: 774b389b5dSLuiz Capitulino 784b389b5dSLuiz Capitulino { 794b389b5dSLuiz Capitulino "return": { 804b389b5dSLuiz Capitulino } 814b389b5dSLuiz Capitulino } 824b389b5dSLuiz Capitulino 834b389b5dSLuiz CapitulinoWhich is QMP's way of saying "the latest command executed OK and didn't return 844b389b5dSLuiz Capitulinoany data". Now you're ready to enter the QMP example commands as explained in 854b389b5dSLuiz Capitulinothe following sections. 864b389b5dSLuiz Capitulino 8768e6dc59SJohn Snow 88fa2613afSDaniel P. BerrangéWriting a simple command: hello-world 89fa2613afSDaniel P. Berrangé------------------------------------- 904b389b5dSLuiz Capitulino 914b389b5dSLuiz CapitulinoThat's the most simple QMP command that can be written. Usually, this kind of 924b389b5dSLuiz Capitulinocommand carries some meaningful action in QEMU but here it will just print 934b389b5dSLuiz Capitulino"Hello, world" to the standard output. 944b389b5dSLuiz Capitulino 954b389b5dSLuiz CapitulinoOur command will be called "hello-world". It takes no arguments, nor does it 964b389b5dSLuiz Capitulinoreturn any data. 974b389b5dSLuiz Capitulino 98bfe873e9SMarkus ArmbrusterThe first step is defining the command in the appropriate QAPI schema 99bfe873e9SMarkus Armbrustermodule. We pick module qapi/misc.json, and add the following line at 10068e6dc59SJohn Snowthe bottom:: 1014b389b5dSLuiz Capitulino 1024b389b5dSLuiz Capitulino { 'command': 'hello-world' } 1034b389b5dSLuiz Capitulino 1044b389b5dSLuiz CapitulinoThe "command" keyword defines a new QMP command. It's an JSON object. All 1054b389b5dSLuiz Capitulinoschema entries are JSON objects. The line above will instruct the QAPI to 1064b389b5dSLuiz Capitulinogenerate any prototypes and the necessary code to marshal and unmarshal 1074b389b5dSLuiz Capitulinoprotocol data. 1084b389b5dSLuiz Capitulino 1094b389b5dSLuiz CapitulinoThe next step is to write the "hello-world" implementation. As explained 1104b389b5dSLuiz Capitulinoearlier, it's preferable for commands to live in QEMU subsystems. But 111f1b3ccfaSKevin Wolf"hello-world" doesn't pertain to any, so we put its implementation in 11268e6dc59SJohn Snowmonitor/qmp-cmds.c:: 1134b389b5dSLuiz Capitulino 1144b389b5dSLuiz Capitulino void qmp_hello_world(Error **errp) 1154b389b5dSLuiz Capitulino { 1164b389b5dSLuiz Capitulino printf("Hello, world!\n"); 1174b389b5dSLuiz Capitulino } 1184b389b5dSLuiz Capitulino 1194b389b5dSLuiz CapitulinoThere are a few things to be noticed: 1204b389b5dSLuiz Capitulino 12168e6dc59SJohn Snow1. QMP command implementation functions must be prefixed with "qmp\_" 1224b389b5dSLuiz Capitulino2. qmp_hello_world() returns void, this is in accordance with the fact that the 1234b389b5dSLuiz Capitulino command doesn't return any data 12468e6dc59SJohn Snow3. It takes an "Error \*\*" argument. This is required. Later we will see how to 1254b389b5dSLuiz Capitulino return errors and take additional arguments. The Error argument should not 1264b389b5dSLuiz Capitulino be touched if the command doesn't return errors 1274b389b5dSLuiz Capitulino4. We won't add the function's prototype. That's automatically done by the QAPI 1284b389b5dSLuiz Capitulino5. Printing to the terminal is discouraged for QMP commands, we do it here 1294b389b5dSLuiz Capitulino because it's the easiest way to demonstrate a QMP command 1304b389b5dSLuiz Capitulino 1314b389b5dSLuiz CapitulinoYou're done. Now build qemu, run it as suggested in the "Testing" section, 13268e6dc59SJohn Snowand then type the following QMP command:: 1334b389b5dSLuiz Capitulino 1344b389b5dSLuiz Capitulino { "execute": "hello-world" } 1354b389b5dSLuiz Capitulino 1364b389b5dSLuiz CapitulinoThen check the terminal running qemu and look for the "Hello, world" string. If 1374b389b5dSLuiz Capitulinoyou don't see it then something went wrong. 1384b389b5dSLuiz Capitulino 13968e6dc59SJohn Snow 14068e6dc59SJohn SnowArguments 14168e6dc59SJohn Snow~~~~~~~~~ 1424b389b5dSLuiz Capitulino 1434b389b5dSLuiz CapitulinoLet's add an argument called "message" to our "hello-world" command. The new 1444b389b5dSLuiz Capitulinoargument will contain the string to be printed to stdout. It's an optional 1454b389b5dSLuiz Capitulinoargument, if it's not present we print our default "Hello, World" string. 1464b389b5dSLuiz Capitulino 1474b389b5dSLuiz CapitulinoThe first change we have to do is to modify the command specification in the 14868e6dc59SJohn Snowschema file to the following:: 1494b389b5dSLuiz Capitulino 1504b389b5dSLuiz Capitulino { 'command': 'hello-world', 'data': { '*message': 'str' } } 1514b389b5dSLuiz Capitulino 1524b389b5dSLuiz CapitulinoNotice the new 'data' member in the schema. It's an JSON object whose each 1534b389b5dSLuiz Capitulinoelement is an argument to the command in question. Also notice the asterisk, 1544b389b5dSLuiz Capitulinoit's used to mark the argument optional (that means that you shouldn't use it 1554b389b5dSLuiz Capitulinofor mandatory arguments). Finally, 'str' is the argument's type, which 1564b389b5dSLuiz Capitulinostands for "string". The QAPI also supports integers, booleans, enumerations 1574b389b5dSLuiz Capitulinoand user defined types. 1584b389b5dSLuiz Capitulino 15968e6dc59SJohn SnowNow, let's update our C implementation in monitor/qmp-cmds.c:: 1604b389b5dSLuiz Capitulino 1614b389b5dSLuiz Capitulino void qmp_hello_world(bool has_message, const char *message, Error **errp) 1624b389b5dSLuiz Capitulino { 1634b389b5dSLuiz Capitulino if (has_message) { 1644b389b5dSLuiz Capitulino printf("%s\n", message); 1654b389b5dSLuiz Capitulino } else { 1664b389b5dSLuiz Capitulino printf("Hello, world\n"); 1674b389b5dSLuiz Capitulino } 1684b389b5dSLuiz Capitulino } 1694b389b5dSLuiz Capitulino 1704b389b5dSLuiz CapitulinoThere are two important details to be noticed: 1714b389b5dSLuiz Capitulino 17268e6dc59SJohn Snow1. All optional arguments are accompanied by a 'has\_' boolean, which is set 1734b389b5dSLuiz Capitulino if the optional argument is present or false otherwise 1744b389b5dSLuiz Capitulino2. The C implementation signature must follow the schema's argument ordering, 1754b389b5dSLuiz Capitulino which is defined by the "data" member 1764b389b5dSLuiz Capitulino 1774b389b5dSLuiz CapitulinoTime to test our new version of the "hello-world" command. Build qemu, run it as 17868e6dc59SJohn Snowdescribed in the "Testing" section and then send two commands:: 1794b389b5dSLuiz Capitulino 1804b389b5dSLuiz Capitulino { "execute": "hello-world" } 1814b389b5dSLuiz Capitulino { 1824b389b5dSLuiz Capitulino "return": { 1834b389b5dSLuiz Capitulino } 1844b389b5dSLuiz Capitulino } 1854b389b5dSLuiz Capitulino 1864b389b5dSLuiz Capitulino { "execute": "hello-world", "arguments": { "message": "We love qemu" } } 1874b389b5dSLuiz Capitulino { 1884b389b5dSLuiz Capitulino "return": { 1894b389b5dSLuiz Capitulino } 1904b389b5dSLuiz Capitulino } 1914b389b5dSLuiz Capitulino 192bb46af41SMarkus ArmbrusterYou should see "Hello, world" and "We love qemu" in the terminal running qemu, 1934b389b5dSLuiz Capitulinoif you don't see these strings, then something went wrong. 1944b389b5dSLuiz Capitulino 19568e6dc59SJohn Snow 19668e6dc59SJohn SnowErrors 19768e6dc59SJohn Snow~~~~~~ 1984b389b5dSLuiz Capitulino 1994b389b5dSLuiz CapitulinoQMP commands should use the error interface exported by the error.h header 200455b0fdeSEric Blakefile. Basically, most errors are set by calling the error_setg() function. 2014b389b5dSLuiz Capitulino 2024b389b5dSLuiz CapitulinoLet's say we don't accept the string "message" to contain the word "love". If 20368e6dc59SJohn Snowit does contain it, we want the "hello-world" command to return an error:: 2044b389b5dSLuiz Capitulino 2054b389b5dSLuiz Capitulino void qmp_hello_world(bool has_message, const char *message, Error **errp) 2064b389b5dSLuiz Capitulino { 2074b389b5dSLuiz Capitulino if (has_message) { 2084b389b5dSLuiz Capitulino if (strstr(message, "love")) { 209455b0fdeSEric Blake error_setg(errp, "the word 'love' is not allowed"); 2104b389b5dSLuiz Capitulino return; 2114b389b5dSLuiz Capitulino } 2124b389b5dSLuiz Capitulino printf("%s\n", message); 2134b389b5dSLuiz Capitulino } else { 2144b389b5dSLuiz Capitulino printf("Hello, world\n"); 2154b389b5dSLuiz Capitulino } 2164b389b5dSLuiz Capitulino } 2174b389b5dSLuiz Capitulino 218455b0fdeSEric BlakeThe first argument to the error_setg() function is the Error pointer 219455b0fdeSEric Blaketo pointer, which is passed to all QMP functions. The next argument is a human 220adb2072eSLuiz Capitulinodescription of the error, this is a free-form printf-like string. 2214b389b5dSLuiz Capitulino 222adb2072eSLuiz CapitulinoLet's test the example above. Build qemu, run it as defined in the "Testing" 22368e6dc59SJohn Snowsection, and then issue the following command:: 224adb2072eSLuiz Capitulino 225adb2072eSLuiz Capitulino { "execute": "hello-world", "arguments": { "message": "all you need is love" } } 2264b389b5dSLuiz Capitulino 22768e6dc59SJohn SnowThe QMP server's response should be:: 2284b389b5dSLuiz Capitulino 2294b389b5dSLuiz Capitulino { 2304b389b5dSLuiz Capitulino "error": { 231adb2072eSLuiz Capitulino "class": "GenericError", 232adb2072eSLuiz Capitulino "desc": "the word 'love' is not allowed" 2334b389b5dSLuiz Capitulino } 2344b389b5dSLuiz Capitulino } 2354b389b5dSLuiz Capitulino 236bb46af41SMarkus ArmbrusterNote that error_setg() produces a "GenericError" class. In general, 237bb46af41SMarkus Armbrusterall QMP errors should have that error class. There are two exceptions 238bb46af41SMarkus Armbrusterto this rule: 2394b389b5dSLuiz Capitulino 240bb46af41SMarkus Armbruster 1. To support a management application's need to recognize a specific 241bb46af41SMarkus Armbruster error for special handling 2424b389b5dSLuiz Capitulino 243bb46af41SMarkus Armbruster 2. Backward compatibility 244adb2072eSLuiz Capitulino 245455b0fdeSEric BlakeIf the failure you want to report falls into one of the two cases above, 246455b0fdeSEric Blakeuse error_set() with a second argument of an ErrorClass value. 247adb2072eSLuiz Capitulino 24868e6dc59SJohn Snow 24968e6dc59SJohn SnowCommand Documentation 25068e6dc59SJohn Snow~~~~~~~~~~~~~~~~~~~~~ 2514b389b5dSLuiz Capitulino 2524b389b5dSLuiz CapitulinoThere's only one step missing to make "hello-world"'s implementation complete, 2534b389b5dSLuiz Capitulinoand that's its documentation in the schema file. 2544b389b5dSLuiz Capitulino 2554b389b5dSLuiz CapitulinoThere are many examples of such documentation in the schema file already, but 25668e6dc59SJohn Snowhere goes "hello-world"'s new entry for qapi/misc.json:: 2574b389b5dSLuiz Capitulino 2584b389b5dSLuiz Capitulino ## 2594eb79bdfSZihao Chang # @hello-world: 2604b389b5dSLuiz Capitulino # 2614b389b5dSLuiz Capitulino # Print a client provided string to the standard output stream. 2624b389b5dSLuiz Capitulino # 2631d8bda12SMarkus Armbruster # @message: string to be printed 2644b389b5dSLuiz Capitulino # 2654b389b5dSLuiz Capitulino # Returns: Nothing on success. 2664b389b5dSLuiz Capitulino # 2674b389b5dSLuiz Capitulino # Notes: if @message is not provided, the "Hello, world" string will 2684b389b5dSLuiz Capitulino # be printed instead 2694b389b5dSLuiz Capitulino # 2704b389b5dSLuiz Capitulino # Since: <next qemu stable release, eg. 1.0> 2714b389b5dSLuiz Capitulino ## 2724b389b5dSLuiz Capitulino { 'command': 'hello-world', 'data': { '*message': 'str' } } 2734b389b5dSLuiz Capitulino 2744b389b5dSLuiz CapitulinoPlease, note that the "Returns" clause is optional if a command doesn't return 2754b389b5dSLuiz Capitulinoany data nor any errors. 2764b389b5dSLuiz Capitulino 27768e6dc59SJohn Snow 27868e6dc59SJohn SnowImplementing the HMP command 27968e6dc59SJohn Snow~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2804b389b5dSLuiz Capitulino 2814b389b5dSLuiz CapitulinoNow that the QMP command is in place, we can also make it available in the human 2824b389b5dSLuiz Capitulinomonitor (HMP). 2834b389b5dSLuiz Capitulino 2844b389b5dSLuiz CapitulinoWith the introduction of the QAPI, HMP commands make QMP calls. Most of the 2854b389b5dSLuiz Capitulinotime HMP commands are simple wrappers. All HMP commands implementation exist in 286f1b3ccfaSKevin Wolfthe monitor/hmp-cmds.c file. 2874b389b5dSLuiz Capitulino 28868e6dc59SJohn SnowHere's the implementation of the "hello-world" HMP command:: 2894b389b5dSLuiz Capitulino 2904b389b5dSLuiz Capitulino void hmp_hello_world(Monitor *mon, const QDict *qdict) 2914b389b5dSLuiz Capitulino { 2924b389b5dSLuiz Capitulino const char *message = qdict_get_try_str(qdict, "message"); 293e940f543SMarkus Armbruster Error *err = NULL; 2944b389b5dSLuiz Capitulino 295e940f543SMarkus Armbruster qmp_hello_world(!!message, message, &err); 2966fa6b54fSDaniel P. Berrangé if (hmp_handle_error(mon, err)) { 2974b389b5dSLuiz Capitulino return; 2984b389b5dSLuiz Capitulino } 2994b389b5dSLuiz Capitulino } 3004b389b5dSLuiz Capitulino 3014b389b5dSLuiz CapitulinoAlso, you have to add the function's prototype to the hmp.h file. 3024b389b5dSLuiz Capitulino 3034b389b5dSLuiz CapitulinoThere are three important points to be noticed: 3044b389b5dSLuiz Capitulino 3054b389b5dSLuiz Capitulino1. The "mon" and "qdict" arguments are mandatory for all HMP functions. The 3064b389b5dSLuiz Capitulino former is the monitor object. The latter is how the monitor passes 3074b389b5dSLuiz Capitulino arguments entered by the user to the command implementation 3086fa6b54fSDaniel P. Berrangé2. hmp_hello_world() performs error checking. In this example we just call 3096fa6b54fSDaniel P. Berrangé hmp_handle_error() which prints a message to the user, but we could do 3106fa6b54fSDaniel P. Berrangé more, like taking different actions depending on the error 3116fa6b54fSDaniel P. Berrangé qmp_hello_world() returns 312e940f543SMarkus Armbruster3. The "err" variable must be initialized to NULL before performing the 3134b389b5dSLuiz Capitulino QMP call 3144b389b5dSLuiz Capitulino 3154b389b5dSLuiz CapitulinoThere's one last step to actually make the command available to monitor users, 31668e6dc59SJohn Snowwe should add it to the hmp-commands.hx file:: 3174b389b5dSLuiz Capitulino 3184b389b5dSLuiz Capitulino { 3194b389b5dSLuiz Capitulino .name = "hello-world", 3204b389b5dSLuiz Capitulino .args_type = "message:s?", 3214b389b5dSLuiz Capitulino .params = "hello-world [message]", 3224b389b5dSLuiz Capitulino .help = "Print message to the standard output", 3232b9e3576SMarc-André Lureau .cmd = hmp_hello_world, 3244b389b5dSLuiz Capitulino }, 3254b389b5dSLuiz Capitulino 32668e6dc59SJohn Snow:: 32768e6dc59SJohn Snow 3284b389b5dSLuiz Capitulino STEXI 3294b389b5dSLuiz Capitulino @item hello_world @var{message} 3304b389b5dSLuiz Capitulino @findex hello_world 3314b389b5dSLuiz Capitulino Print message to the standard output 3324b389b5dSLuiz Capitulino ETEXI 3334b389b5dSLuiz Capitulino 3344b389b5dSLuiz CapitulinoTo test this you have to open a user monitor and issue the "hello-world" 3354b389b5dSLuiz Capitulinocommand. It might be instructive to check the command's documentation with 3364b389b5dSLuiz CapitulinoHMP's "help" command. 3374b389b5dSLuiz Capitulino 3384b389b5dSLuiz CapitulinoPlease, check the "-monitor" command-line option to know how to open a user 3394b389b5dSLuiz Capitulinomonitor. 3404b389b5dSLuiz Capitulino 34168e6dc59SJohn Snow 342fa2613afSDaniel P. BerrangéWriting more complex commands 343fa2613afSDaniel P. Berrangé----------------------------- 3444b389b5dSLuiz Capitulino 3454b389b5dSLuiz CapitulinoA QMP command is capable of returning any data the QAPI supports like integers, 3464b389b5dSLuiz Capitulinostrings, booleans, enumerations and user defined types. 3474b389b5dSLuiz Capitulino 3484b389b5dSLuiz CapitulinoIn this section we will focus on user defined types. Please, check the QAPI 3494b389b5dSLuiz Capitulinodocumentation for information about the other types. 3504b389b5dSLuiz Capitulino 35168e6dc59SJohn Snow 352*f2de406fSDaniel P. BerrangéModelling data in QAPI 353*f2de406fSDaniel P. Berrangé~~~~~~~~~~~~~~~~~~~~~~ 354*f2de406fSDaniel P. Berrangé 355*f2de406fSDaniel P. BerrangéFor a QMP command that to be considered stable and supported long term, 356*f2de406fSDaniel P. Berrangéthere is a requirement returned data should be explicitly modelled 357*f2de406fSDaniel P. Berrangéusing fine-grained QAPI types. As a general guide, a caller of the QMP 358*f2de406fSDaniel P. Berrangécommand should never need to parse individual returned data fields. If 359*f2de406fSDaniel P. Berrangéa field appears to need parsing, then it should be split into separate 360*f2de406fSDaniel P. Berrangéfields corresponding to each distinct data item. This should be the 361*f2de406fSDaniel P. Berrangécommon case for any new QMP command that is intended to be used by 362*f2de406fSDaniel P. Berrangémachines, as opposed to exclusively human operators. 363*f2de406fSDaniel P. Berrangé 364*f2de406fSDaniel P. BerrangéSome QMP commands, however, are only intended as ad hoc debugging aids 365*f2de406fSDaniel P. Berrangéfor human operators. While they may return large amounts of formatted 366*f2de406fSDaniel P. Berrangédata, it is not expected that machines will need to parse the result. 367*f2de406fSDaniel P. BerrangéThe overhead of defining a fine grained QAPI type for the data may not 368*f2de406fSDaniel P. Berrangébe justified by the potential benefit. In such cases, it is permitted 369*f2de406fSDaniel P. Berrangéto have a command return a simple string that contains formatted data, 370*f2de406fSDaniel P. Berrangéhowever, it is mandatory for the command to use the 'x-' name prefix. 371*f2de406fSDaniel P. BerrangéThis indicates that the command is not guaranteed to be long term 372*f2de406fSDaniel P. Berrangéstable / liable to change in future and is not following QAPI design 373*f2de406fSDaniel P. Berrangébest practices. An example where this approach is taken is the QMP 374*f2de406fSDaniel P. Berrangécommand "x-query-registers". This returns a formatted dump of the 375*f2de406fSDaniel P. Berrangéarchitecture specific CPU state. The way the data is formatted varies 376*f2de406fSDaniel P. Berrangéacross QEMU targets, is liable to change over time, and is only 377*f2de406fSDaniel P. Berrangéintended to be consumed as an opaque string by machines. 378*f2de406fSDaniel P. Berrangé 37968e6dc59SJohn SnowUser Defined Types 38068e6dc59SJohn Snow~~~~~~~~~~~~~~~~~~ 3814b389b5dSLuiz Capitulino 382e218052fSMarkus ArmbrusterFIXME This example needs to be redone after commit 6d32717 383e218052fSMarkus Armbruster 3844b389b5dSLuiz CapitulinoFor this example we will write the query-alarm-clock command, which returns 3854b389b5dSLuiz Capitulinoinformation about QEMU's timer alarm. For more information about it, please 3864b389b5dSLuiz Capitulinocheck the "-clock" command-line option. 3874b389b5dSLuiz Capitulino 3884b389b5dSLuiz CapitulinoWe want to return two pieces of information. The first one is the alarm clock's 3894b389b5dSLuiz Capitulinoname. The second one is when the next alarm will fire. The former information is 3904b389b5dSLuiz Capitulinoreturned as a string, the latter is an integer in nanoseconds (which is not 3914b389b5dSLuiz Capitulinovery useful in practice, as the timer has probably already fired when the 3924b389b5dSLuiz Capitulinoinformation reaches the client). 3934b389b5dSLuiz Capitulino 39468e6dc59SJohn SnowThe best way to return that data is to create a new QAPI type, as shown below:: 3954b389b5dSLuiz Capitulino 3964b389b5dSLuiz Capitulino ## 3974b389b5dSLuiz Capitulino # @QemuAlarmClock 3984b389b5dSLuiz Capitulino # 3994b389b5dSLuiz Capitulino # QEMU alarm clock information. 4004b389b5dSLuiz Capitulino # 4014b389b5dSLuiz Capitulino # @clock-name: The alarm clock method's name. 4024b389b5dSLuiz Capitulino # 4031d8bda12SMarkus Armbruster # @next-deadline: The time (in nanoseconds) the next alarm will fire. 4044b389b5dSLuiz Capitulino # 4054b389b5dSLuiz Capitulino # Since: 1.0 4064b389b5dSLuiz Capitulino ## 4074b389b5dSLuiz Capitulino { 'type': 'QemuAlarmClock', 4084b389b5dSLuiz Capitulino 'data': { 'clock-name': 'str', '*next-deadline': 'int' } } 4094b389b5dSLuiz Capitulino 4104b389b5dSLuiz CapitulinoThe "type" keyword defines a new QAPI type. Its "data" member contains the 4114b389b5dSLuiz Capitulinotype's members. In this example our members are the "clock-name" and the 4124b389b5dSLuiz Capitulino"next-deadline" one, which is optional. 4134b389b5dSLuiz Capitulino 41468e6dc59SJohn SnowNow let's define the query-alarm-clock command:: 4154b389b5dSLuiz Capitulino 4164b389b5dSLuiz Capitulino ## 4174b389b5dSLuiz Capitulino # @query-alarm-clock 4184b389b5dSLuiz Capitulino # 4194b389b5dSLuiz Capitulino # Return information about QEMU's alarm clock. 4204b389b5dSLuiz Capitulino # 4214b389b5dSLuiz Capitulino # Returns a @QemuAlarmClock instance describing the alarm clock method 4224b389b5dSLuiz Capitulino # being currently used by QEMU (this is usually set by the '-clock' 4234b389b5dSLuiz Capitulino # command-line option). 4244b389b5dSLuiz Capitulino # 4254b389b5dSLuiz Capitulino # Since: 1.0 4264b389b5dSLuiz Capitulino ## 4274b389b5dSLuiz Capitulino { 'command': 'query-alarm-clock', 'returns': 'QemuAlarmClock' } 4284b389b5dSLuiz Capitulino 4294b389b5dSLuiz CapitulinoNotice the "returns" keyword. As its name suggests, it's used to define the 4304b389b5dSLuiz Capitulinodata returned by a command. 4314b389b5dSLuiz Capitulino 4324b389b5dSLuiz CapitulinoIt's time to implement the qmp_query_alarm_clock() function, you can put it 43368e6dc59SJohn Snowin the qemu-timer.c file:: 4344b389b5dSLuiz Capitulino 4354b389b5dSLuiz Capitulino QemuAlarmClock *qmp_query_alarm_clock(Error **errp) 4364b389b5dSLuiz Capitulino { 4374b389b5dSLuiz Capitulino QemuAlarmClock *clock; 4384b389b5dSLuiz Capitulino int64_t deadline; 4394b389b5dSLuiz Capitulino 4404b389b5dSLuiz Capitulino clock = g_malloc0(sizeof(*clock)); 4414b389b5dSLuiz Capitulino 4424b389b5dSLuiz Capitulino deadline = qemu_next_alarm_deadline(); 4434b389b5dSLuiz Capitulino if (deadline > 0) { 4444b389b5dSLuiz Capitulino clock->has_next_deadline = true; 4454b389b5dSLuiz Capitulino clock->next_deadline = deadline; 4464b389b5dSLuiz Capitulino } 4474b389b5dSLuiz Capitulino clock->clock_name = g_strdup(alarm_timer->name); 4484b389b5dSLuiz Capitulino 4494b389b5dSLuiz Capitulino return clock; 4504b389b5dSLuiz Capitulino } 4514b389b5dSLuiz Capitulino 4524b389b5dSLuiz CapitulinoThere are a number of things to be noticed: 4534b389b5dSLuiz Capitulino 4544b389b5dSLuiz Capitulino1. The QemuAlarmClock type is automatically generated by the QAPI framework, 4554b389b5dSLuiz Capitulino its members correspond to the type's specification in the schema file 4564b389b5dSLuiz Capitulino2. As specified in the schema file, the function returns a QemuAlarmClock 4574b389b5dSLuiz Capitulino instance and takes no arguments (besides the "errp" one, which is mandatory 4584b389b5dSLuiz Capitulino for all QMP functions) 4594b389b5dSLuiz Capitulino3. The "clock" variable (which will point to our QAPI type instance) is 4604b389b5dSLuiz Capitulino allocated by the regular g_malloc0() function. Note that we chose to 461dabdf394SStefan Weil initialize the memory to zero. This is recommended for all QAPI types, as 4624b389b5dSLuiz Capitulino it helps avoiding bad surprises (specially with booleans) 4634b389b5dSLuiz Capitulino4. Remember that "next_deadline" is optional? All optional members have a 4644b389b5dSLuiz Capitulino 'has_TYPE_NAME' member that should be properly set by the implementation, 4654b389b5dSLuiz Capitulino as shown above 4664b389b5dSLuiz Capitulino5. Even static strings, such as "alarm_timer->name", should be dynamically 4674b389b5dSLuiz Capitulino allocated by the implementation. This is so because the QAPI also generates 4684b389b5dSLuiz Capitulino a function to free its types and it cannot distinguish between dynamically 4694b389b5dSLuiz Capitulino or statically allocated strings 470eb815e24SMarkus Armbruster6. You have to include "qapi/qapi-commands-misc.h" in qemu-timer.c 4714b389b5dSLuiz Capitulino 4724b389b5dSLuiz CapitulinoTime to test the new command. Build qemu, run it as described in the "Testing" 47368e6dc59SJohn Snowsection and try this:: 4744b389b5dSLuiz Capitulino 4754b389b5dSLuiz Capitulino { "execute": "query-alarm-clock" } 4764b389b5dSLuiz Capitulino { 4774b389b5dSLuiz Capitulino "return": { 4784b389b5dSLuiz Capitulino "next-deadline": 2368219, 4794b389b5dSLuiz Capitulino "clock-name": "dynticks" 4804b389b5dSLuiz Capitulino } 4814b389b5dSLuiz Capitulino } 4824b389b5dSLuiz Capitulino 4834b389b5dSLuiz Capitulino 48468e6dc59SJohn SnowThe HMP command 48568e6dc59SJohn Snow~~~~~~~~~~~~~~~ 48668e6dc59SJohn Snow 48768e6dc59SJohn SnowHere's the HMP counterpart of the query-alarm-clock command:: 4884b389b5dSLuiz Capitulino 4894b389b5dSLuiz Capitulino void hmp_info_alarm_clock(Monitor *mon) 4904b389b5dSLuiz Capitulino { 4914b389b5dSLuiz Capitulino QemuAlarmClock *clock; 492e940f543SMarkus Armbruster Error *err = NULL; 4934b389b5dSLuiz Capitulino 494e940f543SMarkus Armbruster clock = qmp_query_alarm_clock(&err); 4956fa6b54fSDaniel P. Berrangé if (hmp_handle_error(mon, err)) { 4964b389b5dSLuiz Capitulino return; 4974b389b5dSLuiz Capitulino } 4984b389b5dSLuiz Capitulino 4994b389b5dSLuiz Capitulino monitor_printf(mon, "Alarm clock method in use: '%s'\n", clock->clock_name); 5004b389b5dSLuiz Capitulino if (clock->has_next_deadline) { 5014b389b5dSLuiz Capitulino monitor_printf(mon, "Next alarm will fire in %" PRId64 " nanoseconds\n", 5024b389b5dSLuiz Capitulino clock->next_deadline); 5034b389b5dSLuiz Capitulino } 5044b389b5dSLuiz Capitulino 5054b389b5dSLuiz Capitulino qapi_free_QemuAlarmClock(clock); 5064b389b5dSLuiz Capitulino } 5074b389b5dSLuiz Capitulino 5084b389b5dSLuiz CapitulinoIt's important to notice that hmp_info_alarm_clock() calls 5094b389b5dSLuiz Capitulinoqapi_free_QemuAlarmClock() to free the data returned by qmp_query_alarm_clock(). 5104b389b5dSLuiz CapitulinoFor user defined types, the QAPI will generate a qapi_free_QAPI_TYPE_NAME() 5114b389b5dSLuiz Capitulinofunction and that's what you have to use to free the types you define and 5124b389b5dSLuiz Capitulinoqapi_free_QAPI_TYPE_NAMEList() for list types (explained in the next section). 5134b389b5dSLuiz CapitulinoIf the QMP call returns a string, then you should g_free() to free it. 5144b389b5dSLuiz Capitulino 5154b389b5dSLuiz CapitulinoAlso note that hmp_info_alarm_clock() performs error handling. That's not 5164b389b5dSLuiz Capitulinostrictly required if you're sure the QMP function doesn't return errors, but 5174b389b5dSLuiz Capitulinoit's good practice to always check for errors. 5184b389b5dSLuiz Capitulino 5194b389b5dSLuiz CapitulinoAnother important detail is that HMP's "info" commands don't go into the 5204b389b5dSLuiz Capitulinohmp-commands.hx. Instead, they go into the info_cmds[] table, which is defined 52168e6dc59SJohn Snowin the monitor/misc.c file. The entry for the "info alarmclock" follows:: 5224b389b5dSLuiz Capitulino 5234b389b5dSLuiz Capitulino { 5244b389b5dSLuiz Capitulino .name = "alarmclock", 5254b389b5dSLuiz Capitulino .args_type = "", 5264b389b5dSLuiz Capitulino .params = "", 5274b389b5dSLuiz Capitulino .help = "show information about the alarm clock", 5282b9e3576SMarc-André Lureau .cmd = hmp_info_alarm_clock, 5294b389b5dSLuiz Capitulino }, 5304b389b5dSLuiz Capitulino 5314b389b5dSLuiz CapitulinoTo test this, run qemu and type "info alarmclock" in the user monitor. 5324b389b5dSLuiz Capitulino 53368e6dc59SJohn Snow 53468e6dc59SJohn SnowReturning Lists 53568e6dc59SJohn Snow~~~~~~~~~~~~~~~ 5364b389b5dSLuiz Capitulino 5374b389b5dSLuiz CapitulinoFor this example, we're going to return all available methods for the timer 5384b389b5dSLuiz Capitulinoalarm, which is pretty much what the command-line option "-clock ?" does, 5394b389b5dSLuiz Capitulinoexcept that we're also going to inform which method is in use. 5404b389b5dSLuiz Capitulino 54168e6dc59SJohn SnowThis first step is to define a new type:: 5424b389b5dSLuiz Capitulino 5434b389b5dSLuiz Capitulino ## 5444b389b5dSLuiz Capitulino # @TimerAlarmMethod 5454b389b5dSLuiz Capitulino # 5464b389b5dSLuiz Capitulino # Timer alarm method information. 5474b389b5dSLuiz Capitulino # 5484b389b5dSLuiz Capitulino # @method-name: The method's name. 5494b389b5dSLuiz Capitulino # 5504b389b5dSLuiz Capitulino # @current: true if this alarm method is currently in use, false otherwise 5514b389b5dSLuiz Capitulino # 5524b389b5dSLuiz Capitulino # Since: 1.0 5534b389b5dSLuiz Capitulino ## 5544b389b5dSLuiz Capitulino { 'type': 'TimerAlarmMethod', 5554b389b5dSLuiz Capitulino 'data': { 'method-name': 'str', 'current': 'bool' } } 5564b389b5dSLuiz Capitulino 5574b389b5dSLuiz CapitulinoThe command will be called "query-alarm-methods", here is its schema 55868e6dc59SJohn Snowspecification:: 5594b389b5dSLuiz Capitulino 5604b389b5dSLuiz Capitulino ## 5614b389b5dSLuiz Capitulino # @query-alarm-methods 5624b389b5dSLuiz Capitulino # 5634b389b5dSLuiz Capitulino # Returns information about available alarm methods. 5644b389b5dSLuiz Capitulino # 5654b389b5dSLuiz Capitulino # Returns: a list of @TimerAlarmMethod for each method 5664b389b5dSLuiz Capitulino # 5674b389b5dSLuiz Capitulino # Since: 1.0 5684b389b5dSLuiz Capitulino ## 5694b389b5dSLuiz Capitulino { 'command': 'query-alarm-methods', 'returns': ['TimerAlarmMethod'] } 5704b389b5dSLuiz Capitulino 5714b389b5dSLuiz CapitulinoNotice the syntax for returning lists "'returns': ['TimerAlarmMethod']", this 5724b389b5dSLuiz Capitulinoshould be read as "returns a list of TimerAlarmMethod instances". 5734b389b5dSLuiz Capitulino 57468e6dc59SJohn SnowThe C implementation follows:: 5754b389b5dSLuiz Capitulino 5764b389b5dSLuiz Capitulino TimerAlarmMethodList *qmp_query_alarm_methods(Error **errp) 5774b389b5dSLuiz Capitulino { 5784b389b5dSLuiz Capitulino TimerAlarmMethodList *method_list = NULL; 5794b389b5dSLuiz Capitulino const struct qemu_alarm_timer *p; 5804b389b5dSLuiz Capitulino bool current = true; 5814b389b5dSLuiz Capitulino 5824b389b5dSLuiz Capitulino for (p = alarm_timers; p->name; p++) { 58354aa3de7SEric Blake TimerAlarmMethod *value = g_malloc0(*value); 58454aa3de7SEric Blake value->method_name = g_strdup(p->name); 58554aa3de7SEric Blake value->current = current; 58654aa3de7SEric Blake QAPI_LIST_PREPEND(method_list, value); 5874b389b5dSLuiz Capitulino current = false; 5884b389b5dSLuiz Capitulino } 5894b389b5dSLuiz Capitulino 5904b389b5dSLuiz Capitulino return method_list; 5914b389b5dSLuiz Capitulino } 5924b389b5dSLuiz Capitulino 5934b389b5dSLuiz CapitulinoThe most important difference from the previous examples is the 5944b389b5dSLuiz CapitulinoTimerAlarmMethodList type, which is automatically generated by the QAPI from 5954b389b5dSLuiz Capitulinothe TimerAlarmMethod type. 5964b389b5dSLuiz Capitulino 5974b389b5dSLuiz CapitulinoEach list node is represented by a TimerAlarmMethodList instance. We have to 5984b389b5dSLuiz Capitulinoallocate it, and that's done inside the for loop: the "info" pointer points to 5994b389b5dSLuiz Capitulinoan allocated node. We also have to allocate the node's contents, which is 6004b389b5dSLuiz Capitulinostored in its "value" member. In our example, the "value" member is a pointer 6014b389b5dSLuiz Capitulinoto an TimerAlarmMethod instance. 6024b389b5dSLuiz Capitulino 6034b389b5dSLuiz CapitulinoNotice that the "current" variable is used as "true" only in the first 6045708b2b7SChen Hanxiaoiteration of the loop. That's because the alarm timer method in use is the 6054b389b5dSLuiz Capitulinofirst element of the alarm_timers array. Also notice that QAPI lists are handled 6064b389b5dSLuiz Capitulinoby hand and we return the head of the list. 6074b389b5dSLuiz Capitulino 6084b389b5dSLuiz CapitulinoNow Build qemu, run it as explained in the "Testing" section and try our new 60968e6dc59SJohn Snowcommand:: 6104b389b5dSLuiz Capitulino 6114b389b5dSLuiz Capitulino { "execute": "query-alarm-methods" } 6124b389b5dSLuiz Capitulino { 6134b389b5dSLuiz Capitulino "return": [ 6144b389b5dSLuiz Capitulino { 6154b389b5dSLuiz Capitulino "current": false, 6164b389b5dSLuiz Capitulino "method-name": "unix" 6174b389b5dSLuiz Capitulino }, 6184b389b5dSLuiz Capitulino { 6194b389b5dSLuiz Capitulino "current": true, 6204b389b5dSLuiz Capitulino "method-name": "dynticks" 6214b389b5dSLuiz Capitulino } 6224b389b5dSLuiz Capitulino ] 6234b389b5dSLuiz Capitulino } 6244b389b5dSLuiz Capitulino 6254b389b5dSLuiz CapitulinoThe HMP counterpart is a bit more complex than previous examples because it 62668e6dc59SJohn Snowhas to traverse the list, it's shown below for reference:: 6274b389b5dSLuiz Capitulino 6284b389b5dSLuiz Capitulino void hmp_info_alarm_methods(Monitor *mon) 6294b389b5dSLuiz Capitulino { 6304b389b5dSLuiz Capitulino TimerAlarmMethodList *method_list, *method; 631e940f543SMarkus Armbruster Error *err = NULL; 6324b389b5dSLuiz Capitulino 633e940f543SMarkus Armbruster method_list = qmp_query_alarm_methods(&err); 6346fa6b54fSDaniel P. Berrangé if (hmp_handle_error(mon, err)) { 6354b389b5dSLuiz Capitulino return; 6364b389b5dSLuiz Capitulino } 6374b389b5dSLuiz Capitulino 6384b389b5dSLuiz Capitulino for (method = method_list; method; method = method->next) { 6394b389b5dSLuiz Capitulino monitor_printf(mon, "%c %s\n", method->value->current ? '*' : ' ', 6404b389b5dSLuiz Capitulino method->value->method_name); 6414b389b5dSLuiz Capitulino } 6424b389b5dSLuiz Capitulino 6434b389b5dSLuiz Capitulino qapi_free_TimerAlarmMethodList(method_list); 6444b389b5dSLuiz Capitulino } 645