Add 'query-events' command to QMP to query async events
PATCH 1/1 Add 'query-events' command to QMP to query async events
From: "Daniel P. Berrange" <[email protected]>
Sometimes it is neccessary for an application to determine whether a particular QMP event is available, so they can decide whether to use compatibility code instead. This introduces a new 'query-events' command to QMP todo just that
{ "execute": "query-events" }
{"return": [{"name": "WAKEUP"},
{"name": "SUSPEND"},
{"name": "DEVICE_TRAY_MOVED"},
{"name": "BLOCK_JOB_CANCELLED"},
{"name": "BLOCK_JOB_COMPLETED"},
...snip...
{"name": "SHUTDOWN"}]}* monitor.c: Split out MonitorEvent -> string conversion into monitor_protocol_event_name() API. Add impl of qmp_query_events monitor command handler
* qapi-schema.json, qmp-commands.hx: Define contract of query-events command
--- monitor.c | 93 ++++++++++++++++++++++++++++++++++------------------- qapi-schema.json | 22 +++++++++++++ qmp-commands.hx | 37 +++++++++++++++++++++ 3 files changed, 119 insertions(+), 33 deletions(-)
monitor.c +60 -33
@@ -422,84 +422,93 @@ static void timestamp_put(QDict *qdict)qdict_put_obj (qdict,"timestamp" , obj); } -/** -* monitor_protocol_event(): Generate a Monitor event -* -* Event-specific data can be emitted through the (optional) 'data' parameter. -*/ -void monitor_protocol_event (MonitorEvent event,QObject *data) -{ -QDict *qmp; -const char *event_name; -Monitor *mon; - -assert (event <QEVENT_MAX ); +static const char *monitor_protocol_event_name (MonitorEvent event) +{switch (event) {case QEVENT_SHUTDOWN : - event_name ="SHUTDOWN" ; +return "SHUTDOWN" ;break ;case QEVENT_RESET : - event_name ="RESET" ; +return "RESET" ;break ;case QEVENT_POWERDOWN : - event_name ="POWERDOWN" ; +return "POWERDOWN" ;break ;case QEVENT_STOP : - event_name ="STOP" ; +return "STOP" ;break ;case QEVENT_RESUME : - event_name ="RESUME" ; +return "RESUME" ;break ;case QEVENT_VNC_CONNECTED : - event_name ="VNC_CONNECTED" ; +return "VNC_CONNECTED" ;break ;case QEVENT_VNC_INITIALIZED : - event_name ="VNC_INITIALIZED" ; +return "VNC_INITIALIZED" ;break ;case QEVENT_VNC_DISCONNECTED : - event_name ="VNC_DISCONNECTED" ; +return "VNC_DISCONNECTED" ;break ;case QEVENT_BLOCK_IO_ERROR : - event_name ="BLOCK_IO_ERROR" ; +return "BLOCK_IO_ERROR" ;break ;case QEVENT_RTC_CHANGE : - event_name ="RTC_CHANGE" ; +return "RTC_CHANGE" ;break ;case QEVENT_WATCHDOG : - event_name ="WATCHDOG" ; +return "WATCHDOG" ;break ;case QEVENT_SPICE_CONNECTED : - event_name ="SPICE_CONNECTED" ; +return "SPICE_CONNECTED" ;break ;case QEVENT_SPICE_INITIALIZED : - event_name ="SPICE_INITIALIZED" ; +return "SPICE_INITIALIZED" ;break ;case QEVENT_SPICE_DISCONNECTED : - event_name ="SPICE_DISCONNECTED" ; +return "SPICE_DISCONNECTED" ;break ;case QEVENT_BLOCK_JOB_COMPLETED : - event_name ="BLOCK_JOB_COMPLETED" ; +return "BLOCK_JOB_COMPLETED" ;break ;case QEVENT_BLOCK_JOB_CANCELLED : - event_name ="BLOCK_JOB_CANCELLED" ; +return "BLOCK_JOB_CANCELLED" ;break ;case QEVENT_DEVICE_TRAY_MOVED : - event_name ="DEVICE_TRAY_MOVED" ; +return "DEVICE_TRAY_MOVED" ;break ;case QEVENT_SUSPEND : - event_name ="SUSPEND" ; +return "SUSPEND" ;break ;case QEVENT_WAKEUP : - event_name ="WAKEUP" ; +return "WAKEUP" ;break ;case QEVENT_BALLOON_CHANGE : - event_name ="BALLOON_CHANGE" ; +return "BALLOON_CHANGE" ;break ;default : -abort (); +return NULL ;break ; } +} + +/** +* monitor_protocol_event(): Generate a Monitor event +* +* Event-specific data can be emitted through the (optional) 'data' parameter. +*/ +void monitor_protocol_event (MonitorEvent event,QObject *data) +{ +QDict *qmp; +const char *event_name; +Monitor *mon; + +assert (event <QEVENT_MAX ); + + event_name =monitor_protocol_event_name (event); +if (event_name ==NULL ) { +abort (); + } qmp =qdict_new ();timestamp_put (qmp);
@@ -741,6 +750,24 @@ CommandInfoList *qmp_query_commands(Error **errp)return cmd_list; } +EventInfoList *qmp_query_events (Error **errp) +{ +EventInfoList *info, *ev_list =NULL ; +MonitorEvent e; + +for (e =0 ; e <QEVENT_MAX ; e++) { +const char *evname =monitor_protocol_event_name (e); + info =g_malloc0 (sizeof (*info)); + info->value =g_malloc0 (sizeof (*info->value)); + info->value->name =g_strdup (evname); + + info->next = ev_list; + ev_list = info; + } + +return ev_list; +} +/* set the current CPU defined by the user */ int monitor_set_cpu (int cpu_index) {
qapi-schema.json +22 -0
@@ -228,6 +228,28 @@ { 'command': 'query-commands', 'returns': ['CommandInfo'] } ## +# @EventInfo: +# +# Information about a QMP event +# +# @name: The event name +# +# Since: 1.2.0 +## +{ 'type': 'EventInfo', 'data': {'name': 'str'} } + +## +# @query-events: +# +# Return a list of supported QMP events by this server +# +# Returns: A list of @EventInfo for all supported events +# +# Since: 1.2.0 +## +{ 'command': 'query-events', 'returns': ['EventInfo'] } + +## # @MigrationStats # # Detailed migration status.
qmp-commands.hx +37 -0
@@ -1179,6 +1179,43 @@ EQMP }, SQMP +query-events +-------------- + +List QMP available events. + +Each event is represented by a json-object, the returned value is a json-array +of all events. + +Each json-object contain: + +- "name": event's name (json-string) + +Example: + +-> { "execute": "query-events" } +<- { + "return":[ + { + "name":"SHUTDOWN" + }, + { + "name":"RESET" + } + ] + } + +Note: This example has been shortened as the real response is too long. + +EQMP + + { + .name = "query-events", + .args_type = "", + .mhandler.cmd_new = qmp_marshal_input_query_events, + }, + +SQMP query-chardev -------------
Signature · 1 line
1.7.7.6
Quoted 6 lines · Daniel P. Berrange
From: "Daniel P. Berrange" <[email protected]>
Sometimes it is neccessary for an application to determine whether a particular QMP event is available, so they can decide whether to use compatibility code instead. This introduces a new 'query-events' command to QMP todo just
s/todo/to do/
Quoted 11 lines
that
{ "execute": "query-events" }
{"return": [{"name": "WAKEUP"},{"name": "SUSPEND"}, {"name": "DEVICE_TRAY_MOVED"}, {"name": "BLOCK_JOB_CANCELLED"}, {"name": "BLOCK_JOB_COMPLETED"}, ...snip... {"name": "SHUTDOWN"}]}
Quoted 5 lines
*
monitor.c: Split out MonitorEvent -> string conversion intomonitor_protocol_event_name()API. Add impl ofqmp_query_eventsmonitor command handler
* qapi-schema.json, qmp-commands.hx: Define contract of query-events command
Definitely useful for libvirt.
monitor.c static void timestamp_put(QDict *qdict) · line 431
Quoted 7 lines
monitor.c +3
-1+static const char *monitor_protocol_event_name (MonitorEvent event) +{switch (event) {case QEVENT_SHUTDOWN : - event_name ="SHUTDOWN" ; +return "SHUTDOWN" ;break ;
These 'break' statements are now unreachable; does this matter in qemu coding style?
qmp-commands.hx EQMP · line 1190
Quoted 9 lines
qmp-commands.hx +9
-0+query-events +-------------- + +List QMP available events. + +Each event is represented by a json-object, the returned value is a json-array +of all events. + +Each json-object contain:
s/contain/contains/
Signature · 2 lines
Eric Blake [email protected] +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 30 lines · Eric Blake
Quoted 6 lines · Daniel P. Berrange
From: "Daniel P. Berrange"<[email protected]>
Sometimes it is neccessary for an application to determine whether a particular QMP event is available, so they can decide whether to use compatibility code instead. This introduces a new 'query-events' command to QMP todo just
s/todo/to do/
Quoted 11 lines
that
{ "execute": "query-events" }
{"return": [{"name": "WAKEUP"},{"name": "SUSPEND"}, {"name": "DEVICE_TRAY_MOVED"}, {"name": "BLOCK_JOB_CANCELLED"}, {"name": "BLOCK_JOB_COMPLETED"}, ...snip... {"name": "SHUTDOWN"}]}Quoted 5 lines
*
monitor.c: Split out MonitorEvent -> string conversion intomonitor_protocol_event_name()API. Add impl ofqmp_query_eventsmonitor command handler
* qapi-schema.json, qmp-commands.hx: Define contract of query-events commandDefinitely useful for libvirt.
Quoted 10 lines
monitor.c static void timestamp_put(QDict *qdict) · line 431
Quoted 7 lines
monitor.c +3
-1+static const char *monitor_protocol_event_name (MonitorEvent event) +{switch (event) {case QEVENT_SHUTDOWN : - event_name ="SHUTDOWN" ; +return "SHUTDOWN" ;break ;These 'break' statements are now unreachable; does this matter in qemu coding style?
Seems like we should just take the opportunity to kill off the function entirely and replace it with a table:
static const char *monitor_event_names[] = {
[QEVENT_SHUTDOWN] = "SHUTDOWN",
...
};
Regards,
Anthony Liguori
Quoted 42 lines · Anthony Liguori
Quoted 30 lines · Eric Blake
Quoted 6 lines · Daniel P. Berrange
From: "Daniel P. Berrange"<[email protected]>
Sometimes it is neccessary for an application to determine whether a particular QMP event is available, so they can decide whether to use compatibility code instead. This introduces a new 'query-events' command to QMP todo just
s/todo/to do/
Quoted 11 lines
that
{ "execute": "query-events" }
{"return": [{"name": "WAKEUP"},{"name": "SUSPEND"}, {"name": "DEVICE_TRAY_MOVED"}, {"name": "BLOCK_JOB_CANCELLED"}, {"name": "BLOCK_JOB_COMPLETED"}, ...snip... {"name": "SHUTDOWN"}]}Quoted 5 lines
*
monitor.c: Split out MonitorEvent -> string conversion intomonitor_protocol_event_name()API. Add impl ofqmp_query_eventsmonitor command handler
* qapi-schema.json, qmp-commands.hx: Define contract of query-events commandDefinitely useful for libvirt.
Quoted 10 lines
monitor.c static void timestamp_put(QDict *qdict) · line 431
Quoted 7 lines
monitor.c +3
-1+static const char *monitor_protocol_event_name (MonitorEvent event) +{switch (event) {case QEVENT_SHUTDOWN : - event_name ="SHUTDOWN" ; +return "SHUTDOWN" ;break ;These 'break' statements are now unreachable; does this matter in qemu coding style?
Quoted 7 lines
Seems like we should just take the opportunity to kill off the function entirely and replace it with a table:
static const char *monitor_event_names[] = {
[QEVENT_SHUTDOWN] = "SHUTDOWN",
...
};
That sounds good to me, I'll update the patch accordingly.
Regards,
Daniel
Signature · 4 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
Quoted 42 lines · On Wed, 16 May 2012 11:18:22 -0500 Anthony Liguori <[email protected]>…
Quoted 30 lines · Eric Blake
Quoted 6 lines · Daniel P. Berrange
From: "Daniel P. Berrange"<[email protected]>
Sometimes it is neccessary for an application to determine whether a particular QMP event is available, so they can decide whether to use compatibility code instead. This introduces a new 'query-events' command to QMP todo just
s/todo/to do/
Quoted 11 lines
that
{ "execute": "query-events" }
{"return": [{"name": "WAKEUP"},{"name": "SUSPEND"}, {"name": "DEVICE_TRAY_MOVED"}, {"name": "BLOCK_JOB_CANCELLED"}, {"name": "BLOCK_JOB_COMPLETED"}, ...snip... {"name": "SHUTDOWN"}]}Quoted 5 lines
*
monitor.c: Split out MonitorEvent -> string conversion intomonitor_protocol_event_name()API. Add impl ofqmp_query_eventsmonitor command handler
* qapi-schema.json, qmp-commands.hx: Define contract of query-events commandDefinitely useful for libvirt.
Quoted 10 lines
monitor.c static void timestamp_put(QDict *qdict) · line 431
Quoted 7 lines
monitor.c +3
-1+static const char *monitor_protocol_event_name (MonitorEvent event) +{switch (event) {case QEVENT_SHUTDOWN : - event_name ="SHUTDOWN" ; +return "SHUTDOWN" ;break ;These 'break' statements are now unreachable; does this matter in qemu coding style?
Quoted 7 lines
Seems like we should just take the opportunity to kill off the function entirely and replace it with a table:
static const char *monitor_event_names[] = {
[QEVENT_SHUTDOWN] = "SHUTDOWN",
...
};
Yes, that's really better.
Btw, the ideal way of having this would be to add the event type to the qapi and then add schema introspection.
As I'm not sure I can guarantee that for 1.2, I'm ok with this command.
Anthony, I'm assuming you're ok too.