writing-qmp-commands.txt 19.4 KB
Newer Older
1 2 3 4 5 6 7 8 9
= How to write QMP commands using the QAPI framework =

This document is a step-by-step guide on how to write new QMP commands using
the QAPI framework. It also shows how to implement new style HMP commands.

This document doesn't discuss QMP protocol level details, nor does it dive
into the QAPI framework implementation.

For an in-depth introduction to the QAPI framework, please refer to
10 11
docs/qapi-code-gen.txt. For documentation about the QMP protocol,
start with docs/qmp-intro.txt.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

== Overview ==

Generally speaking, the following steps should be taken in order to write a
new QMP command.

1. Write the command's and type(s) specification in the QAPI schema file
   (qapi-schema.json in the root source directory)

2. Write the QMP command itself, which is a regular C function. Preferably,
   the command should be exported by some QEMU subsystem. But it can also be
   added to the qmp.c file

3. At this point the command can be tested under the QMP protocol

4. Write the HMP command equivalent. This is not required and should only be
   done if it does make sense to have the functionality in HMP. The HMP command
   is implemented in terms of the QMP command

The following sections will demonstrate each of the steps above. We will start
very simple and get more complex as we progress.

=== Testing ===

For all the examples in the next sections, the test setup is the same and is
shown here.

First, QEMU should be started as:

# /path/to/your/source/qemu [...] \
    -chardev socket,id=qmp,port=4444,host=localhost,server \
    -mon chardev=qmp,mode=control,pretty=on

Then, in a different terminal:

$ telnet localhost 4444
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
{
    "QMP": {
        "version": {
            "qemu": {
                "micro": 50, 
                "minor": 15, 
                "major": 0
            }, 
            "package": ""
        }, 
        "capabilities": [
        ]
    }
}

The above output is the QMP server saying you're connected. The server is
actually in capabilities negotiation mode. To enter in command mode type:

{ "execute": "qmp_capabilities" }

Then the server should respond:

{
    "return": {
    }
}

Which is QMP's way of saying "the latest command executed OK and didn't return
any data". Now you're ready to enter the QMP example commands as explained in
the following sections.

== Writing a command that doesn't return data ==

That's the most simple QMP command that can be written. Usually, this kind of
command carries some meaningful action in QEMU but here it will just print
"Hello, world" to the standard output.

Our command will be called "hello-world". It takes no arguments, nor does it
return any data.

The first step is to add the following line to the bottom of the
qapi-schema.json file:

{ 'command': 'hello-world' }

The "command" keyword defines a new QMP command. It's an JSON object. All
schema entries are JSON objects. The line above will instruct the QAPI to
generate any prototypes and the necessary code to marshal and unmarshal
protocol data.

The next step is to write the "hello-world" implementation. As explained
earlier, it's preferable for commands to live in QEMU subsystems. But
"hello-world" doesn't pertain to any, so we put its implementation in qmp.c:

void qmp_hello_world(Error **errp)
{
    printf("Hello, world!\n");
}

There are a few things to be noticed:

1. QMP command implementation functions must be prefixed with "qmp_"
2. qmp_hello_world() returns void, this is in accordance with the fact that the
   command doesn't return any data
3. It takes an "Error **" argument. This is required. Later we will see how to
   return errors and take additional arguments. The Error argument should not
   be touched if the command doesn't return errors
4. We won't add the function's prototype. That's automatically done by the QAPI
5. Printing to the terminal is discouraged for QMP commands, we do it here
   because it's the easiest way to demonstrate a QMP command

You're done. Now build qemu, run it as suggested in the "Testing" section,
and then type the following QMP command:

{ "execute": "hello-world" }

Then check the terminal running qemu and look for the "Hello, world" string. If
you don't see it then something went wrong.

=== Arguments ===

Let's add an argument called "message" to our "hello-world" command. The new
argument will contain the string to be printed to stdout. It's an optional
argument, if it's not present we print our default "Hello, World" string.

The first change we have to do is to modify the command specification in the
schema file to the following:

{ 'command': 'hello-world', 'data': { '*message': 'str' } }

Notice the new 'data' member in the schema. It's an JSON object whose each
element is an argument to the command in question. Also notice the asterisk,
it's used to mark the argument optional (that means that you shouldn't use it
for mandatory arguments). Finally, 'str' is the argument's type, which
stands for "string". The QAPI also supports integers, booleans, enumerations
and user defined types.

Now, let's update our C implementation in qmp.c:

void qmp_hello_world(bool has_message, const char *message, Error **errp)
{
    if (has_message) {
        printf("%s\n", message);
    } else {
        printf("Hello, world\n");
    }
}

There are two important details to be noticed:

1. All optional arguments are accompanied by a 'has_' boolean, which is set
   if the optional argument is present or false otherwise
2. The C implementation signature must follow the schema's argument ordering,
   which is defined by the "data" member

Time to test our new version of the "hello-world" command. Build qemu, run it as
described in the "Testing" section and then send two commands:

{ "execute": "hello-world" }
{
    "return": {
    }
}

{ "execute": "hello-world", "arguments": { "message": "We love qemu" } }
{
    "return": {
    }
}

You should see "Hello, world" and "we love qemu" in the terminal running qemu,
if you don't see these strings, then something went wrong.

=== Errors ===

QMP commands should use the error interface exported by the error.h header
E
Eric Blake 已提交
187
file. Basically, most errors are set by calling the error_setg() function.
188 189

Let's say we don't accept the string "message" to contain the word "love". If
190
it does contain it, we want the "hello-world" command to return an error:
191 192 193 194 195

void qmp_hello_world(bool has_message, const char *message, Error **errp)
{
    if (has_message) {
        if (strstr(message, "love")) {
E
Eric Blake 已提交
196
            error_setg(errp, "the word 'love' is not allowed");
197 198 199 200 201 202 203 204
            return;
        }
        printf("%s\n", message);
    } else {
        printf("Hello, world\n");
    }
}

E
Eric Blake 已提交
205 206
The first argument to the error_setg() function is the Error pointer
to pointer, which is passed to all QMP functions. The next argument is a human
207 208 209 210
description of the error, this is a free-form printf-like string.

Let's test the example above. Build qemu, run it as defined in the "Testing"
section, and then issue the following command:
211

212
{ "execute": "hello-world", "arguments": { "message": "all you need is love" } }
213 214 215 216 217

The QMP server's response should be:

{
    "error": {
218 219
        "class": "GenericError",
        "desc": "the word 'love' is not allowed"
220 221 222
    }
}

E
Eric Blake 已提交
223 224 225
As a general rule, all QMP errors should use ERROR_CLASS_GENERIC_ERROR
(done by default when using error_setg()). There are two exceptions to
this rule:
226 227 228 229 230 231 232

 1. A non-generic ErrorClass value exists* for the failure you want to report
    (eg. DeviceNotFound)

 2. Management applications have to take special action on the failure you
    want to report, hence you have to add a new ErrorClass value so that they
    can check for it
233

E
Eric Blake 已提交
234 235
If the failure you want to report falls into one of the two cases above,
use error_set() with a second argument of an ErrorClass value.
236

237
 * All existing ErrorClass values are defined in the qapi-schema.json file
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

=== Command Documentation ===

There's only one step missing to make "hello-world"'s implementation complete,
and that's its documentation in the schema file.

This is very important. No QMP command will be accepted in QEMU without proper
documentation.

There are many examples of such documentation in the schema file already, but
here goes "hello-world"'s new entry for the qapi-schema.json file:

##
# @hello-world
#
# Print a client provided string to the standard output stream.
#
255
# @message: string to be printed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
#
# Returns: Nothing on success.
#
# Notes: if @message is not provided, the "Hello, world" string will
#        be printed instead
#
# Since: <next qemu stable release, eg. 1.0>
##
{ 'command': 'hello-world', 'data': { '*message': 'str' } }

Please, note that the "Returns" clause is optional if a command doesn't return
any data nor any errors.

=== Implementing the HMP command ===

Now that the QMP command is in place, we can also make it available in the human
monitor (HMP).

With the introduction of the QAPI, HMP commands make QMP calls. Most of the
time HMP commands are simple wrappers. All HMP commands implementation exist in
the hmp.c file.

Here's the implementation of the "hello-world" HMP command:

void hmp_hello_world(Monitor *mon, const QDict *qdict)
{
    const char *message = qdict_get_try_str(qdict, "message");
283
    Error *err = NULL;
284

285 286 287 288
    qmp_hello_world(!!message, message, &err);
    if (err) {
        monitor_printf(mon, "%s\n", error_get_pretty(err));
        error_free(err);
289 290 291 292 293 294 295 296 297 298 299 300 301 302
        return;
    }
}

Also, you have to add the function's prototype to the hmp.h file.

There are three important points to be noticed:

1. The "mon" and "qdict" arguments are mandatory for all HMP functions. The
   former is the monitor object. The latter is how the monitor passes
   arguments entered by the user to the command implementation
2. hmp_hello_world() performs error checking. In this example we just print
   the error description to the user, but we could do more, like taking
   different actions depending on the error qmp_hello_world() returns
303
3. The "err" variable must be initialized to NULL before performing the
304 305 306 307 308 309 310 311 312 313
   QMP call

There's one last step to actually make the command available to monitor users,
we should add it to the hmp-commands.hx file:

    {
        .name       = "hello-world",
        .args_type  = "message:s?",
        .params     = "hello-world [message]",
        .help       = "Print message to the standard output",
314
        .cmd        = hmp_hello_world,
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
    },

STEXI
@item hello_world @var{message}
@findex hello_world
Print message to the standard output
ETEXI

To test this you have to open a user monitor and issue the "hello-world"
command. It might be instructive to check the command's documentation with
HMP's "help" command.

Please, check the "-monitor" command-line option to know how to open a user
monitor.

== Writing a command that returns data ==

A QMP command is capable of returning any data the QAPI supports like integers,
strings, booleans, enumerations and user defined types.

In this section we will focus on user defined types. Please, check the QAPI
documentation for information about the other types.

=== User Defined Types ===

340 341
FIXME This example needs to be redone after commit 6d32717

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
For this example we will write the query-alarm-clock command, which returns
information about QEMU's timer alarm. For more information about it, please
check the "-clock" command-line option.

We want to return two pieces of information. The first one is the alarm clock's
name. The second one is when the next alarm will fire. The former information is
returned as a string, the latter is an integer in nanoseconds (which is not
very useful in practice, as the timer has probably already fired when the
information reaches the client).

The best way to return that data is to create a new QAPI type, as shown below:

##
# @QemuAlarmClock
#
# QEMU alarm clock information.
#
# @clock-name: The alarm clock method's name.
#
361
# @next-deadline: The time (in nanoseconds) the next alarm will fire.
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
#
# Since: 1.0
##
{ 'type': 'QemuAlarmClock',
  'data': { 'clock-name': 'str', '*next-deadline': 'int' } }

The "type" keyword defines a new QAPI type. Its "data" member contains the
type's members. In this example our members are the "clock-name" and the
"next-deadline" one, which is optional.

Now let's define the query-alarm-clock command:

##
# @query-alarm-clock
#
# Return information about QEMU's alarm clock.
#
# Returns a @QemuAlarmClock instance describing the alarm clock method
# being currently used by QEMU (this is usually set by the '-clock'
# command-line option).
#
# Since: 1.0
##
{ 'command': 'query-alarm-clock', 'returns': 'QemuAlarmClock' }

Notice the "returns" keyword. As its name suggests, it's used to define the
data returned by a command.

It's time to implement the qmp_query_alarm_clock() function, you can put it
in the qemu-timer.c file:

QemuAlarmClock *qmp_query_alarm_clock(Error **errp)
{
    QemuAlarmClock *clock;
    int64_t deadline;

    clock = g_malloc0(sizeof(*clock));

    deadline = qemu_next_alarm_deadline();
    if (deadline > 0) {
        clock->has_next_deadline = true;
        clock->next_deadline = deadline;
    }
    clock->clock_name = g_strdup(alarm_timer->name);

    return clock;
}

There are a number of things to be noticed:

1. The QemuAlarmClock type is automatically generated by the QAPI framework,
   its members correspond to the type's specification in the schema file
2. As specified in the schema file, the function returns a QemuAlarmClock
   instance and takes no arguments (besides the "errp" one, which is mandatory
   for all QMP functions)
3. The "clock" variable (which will point to our QAPI type instance) is
   allocated by the regular g_malloc0() function. Note that we chose to
419
   initialize the memory to zero. This is recommended for all QAPI types, as
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
   it helps avoiding bad surprises (specially with booleans)
4. Remember that "next_deadline" is optional? All optional members have a
   'has_TYPE_NAME' member that should be properly set by the implementation,
   as shown above
5. Even static strings, such as "alarm_timer->name", should be dynamically
   allocated by the implementation. This is so because the QAPI also generates
   a function to free its types and it cannot distinguish between dynamically
   or statically allocated strings
6. You have to include the "qmp-commands.h" header file in qemu-timer.c,
   otherwise qemu won't build

Time to test the new command. Build qemu, run it as described in the "Testing"
section and try this:

{ "execute": "query-alarm-clock" }
{
    "return": {
        "next-deadline": 2368219,
        "clock-name": "dynticks"
    }
}

==== The HMP command ====

Here's the HMP counterpart of the query-alarm-clock command:

void hmp_info_alarm_clock(Monitor *mon)
{
    QemuAlarmClock *clock;
449
    Error *err = NULL;
450

451 452
    clock = qmp_query_alarm_clock(&err);
    if (err) {
453
        monitor_printf(mon, "Could not query alarm clock information\n");
454
        error_free(err);
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
        return;
    }

    monitor_printf(mon, "Alarm clock method in use: '%s'\n", clock->clock_name);
    if (clock->has_next_deadline) {
        monitor_printf(mon, "Next alarm will fire in %" PRId64 " nanoseconds\n",
                       clock->next_deadline);
    }

   qapi_free_QemuAlarmClock(clock); 
}

It's important to notice that hmp_info_alarm_clock() calls
qapi_free_QemuAlarmClock() to free the data returned by qmp_query_alarm_clock().
For user defined types, the QAPI will generate a qapi_free_QAPI_TYPE_NAME()
function and that's what you have to use to free the types you define and
qapi_free_QAPI_TYPE_NAMEList() for list types (explained in the next section).
If the QMP call returns a string, then you should g_free() to free it.

Also note that hmp_info_alarm_clock() performs error handling. That's not
strictly required if you're sure the QMP function doesn't return errors, but
it's good practice to always check for errors.

Another important detail is that HMP's "info" commands don't go into the
hmp-commands.hx. Instead, they go into the info_cmds[] table, which is defined
in the monitor.c file. The entry for the "info alarmclock" follows:

    {
        .name       = "alarmclock",
        .args_type  = "",
        .params     = "",
        .help       = "show information about the alarm clock",
487
        .cmd        = hmp_info_alarm_clock,
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
    },

To test this, run qemu and type "info alarmclock" in the user monitor.

=== Returning Lists ===

For this example, we're going to return all available methods for the timer
alarm, which is pretty much what the command-line option "-clock ?" does,
except that we're also going to inform which method is in use.

This first step is to define a new type:

##
# @TimerAlarmMethod
#
# Timer alarm method information.
#
# @method-name: The method's name.
#
# @current: true if this alarm method is currently in use, false otherwise
#
# Since: 1.0
##
{ 'type': 'TimerAlarmMethod',
  'data': { 'method-name': 'str', 'current': 'bool' } }

The command will be called "query-alarm-methods", here is its schema
specification:

##
# @query-alarm-methods
#
# Returns information about available alarm methods.
#
# Returns: a list of @TimerAlarmMethod for each method
#
# Since: 1.0
##
{ 'command': 'query-alarm-methods', 'returns': ['TimerAlarmMethod'] }

Notice the syntax for returning lists "'returns': ['TimerAlarmMethod']", this
should be read as "returns a list of TimerAlarmMethod instances".

The C implementation follows:

TimerAlarmMethodList *qmp_query_alarm_methods(Error **errp)
{
    TimerAlarmMethodList *method_list = NULL;
    const struct qemu_alarm_timer *p;
    bool current = true;

    for (p = alarm_timers; p->name; p++) {
        TimerAlarmMethodList *info = g_malloc0(sizeof(*info));
        info->value = g_malloc0(sizeof(*info->value));
        info->value->method_name = g_strdup(p->name);
        info->value->current = current;

        current = false;

        info->next = method_list;
        method_list = info;
    }

    return method_list;
}

The most important difference from the previous examples is the
TimerAlarmMethodList type, which is automatically generated by the QAPI from
the TimerAlarmMethod type.

Each list node is represented by a TimerAlarmMethodList instance. We have to
allocate it, and that's done inside the for loop: the "info" pointer points to
an allocated node. We also have to allocate the node's contents, which is
stored in its "value" member. In our example, the "value" member is a pointer
to an TimerAlarmMethod instance.

Notice that the "current" variable is used as "true" only in the first
565
iteration of the loop. That's because the alarm timer method in use is the
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
first element of the alarm_timers array. Also notice that QAPI lists are handled
by hand and we return the head of the list.

Now Build qemu, run it as explained in the "Testing" section and try our new
command:

{ "execute": "query-alarm-methods" }
{
    "return": [
        {
            "current": false, 
            "method-name": "unix"
        }, 
        {
            "current": true, 
            "method-name": "dynticks"
        }
    ]
}

The HMP counterpart is a bit more complex than previous examples because it
has to traverse the list, it's shown below for reference:

void hmp_info_alarm_methods(Monitor *mon)
{
    TimerAlarmMethodList *method_list, *method;
592
    Error *err = NULL;
593

594 595
    method_list = qmp_query_alarm_methods(&err);
    if (err) {
596
        monitor_printf(mon, "Could not query alarm methods\n");
597
        error_free(err);
598 599 600 601 602 603 604 605 606 607
        return;
    }

    for (method = method_list; method; method = method->next) {
        monitor_printf(mon, "%c %s\n", method->value->current ? '*' : ' ',
                                       method->value->method_name);
    }

    qapi_free_TimerAlarmMethodList(method_list);
}