This patch introduces a new event type for the QMP event SUSPEND: VIR_DOMAIN_EVENT_ID_SUSPEND The event doesn't take any data. Thus the callback definition is: typedef void (*virConnectDomainEventSuspendCallback)(virConnectPtr conn, virDomainPtr dom, void *opaque); --- daemon/remote.c | 23 +++++++++ examples/domain-events/events-c/event-test.c | 21 ++++++++- examples/domain-events/events-python/event-test.py | 4 ++ include/libvirt/libvirt.h.in | 16 +++++++ python/libvirt-override-virConnect.py | 9 ++++ python/libvirt-override.c | 48 ++++++++++++++++++++ src/conf/domain_event.c | 28 +++++++++++ src/conf/domain_event.h | 2 + src/libvirt_private.syms | 6 ++- src/qemu/qemu_monitor.c | 12 +++++ src/qemu/qemu_monitor.h | 3 + src/qemu/qemu_monitor_json.c | 9 ++++ src/qemu/qemu_process.c | 22 +++++++++ src/remote/remote_driver.c | 32 +++++++++++++ src/remote/remote_protocol.x | 7 +++- src/remote_protocol-structs | 3 + 16 files changed, 240 insertions(+), 5 deletions(-) diff --git a/daemon/remote.c b/daemon/remote.c index 548faab..6a48456 100644 --- a/daemon/remote.c +++ b/daemon/remote.c @@ -559,6 +559,28 @@ static int remoteRelayDomainEventWakeup(virConnectPtr conn ATTRIBUTE_UNUSED, return 0; } +static int remoteRelayDomainEventSuspend(virConnectPtr conn ATTRIBUTE_UNUSED, + virDomainPtr dom, + void *opaque) { + virNetServerClientPtr client = opaque; + remote_domain_event_suspend_msg data; + + if (!client) + return -1; + + VIR_DEBUG("Relaying domain %s %d system suspend", dom->name, dom->id); + + /* build return data */ + memset(&data, 0, sizeof data); + make_nonnull_domain(&data.dom, dom); + + remoteDispatchDomainEventSend(client, remoteProgram, + REMOTE_PROC_DOMAIN_EVENT_SUSPEND, + (xdrproc_t)xdr_remote_domain_event_suspend_msg, &data); + + return 0; +} + static virConnectDomainEventGenericCallback domainEventCallbacks[] = { VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle), @@ -573,6 +595,7 @@ static virConnectDomainEventGenericCallback domainEventCallbacks[] = { VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDiskChange), VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventTrayMoved), VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventWakeup), + VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventSuspend), }; verify(ARRAY_CARDINALITY(domainEventCallbacks) == VIR_DOMAIN_EVENT_ID_LAST); diff --git a/examples/domain-events/events-c/event-test.c b/examples/domain-events/events-c/event-test.c index 15cf5b3..1629358 100644 --- a/examples/domain-events/events-c/event-test.c +++ b/examples/domain-events/events-c/event-test.c @@ -334,6 +334,15 @@ static int myDomainEventWakeupCallback(virConnectPtr conn ATTRIBUTE_UNUSED, return 0; } +static int myDomainEventSuspendCallback(virConnectPtr conn ATTRIBUTE_UNUSED, + virDomainPtr dom, + void *opaque ATTRIBUTE_UNUSED) +{ + printf("%s EVENT: Domain %s(%d) system suspend", + __func__, virDomainGetName(dom), virDomainGetID(dom)); + return 0; +} + static void myFreeFunc(void *opaque) { char *str = opaque; @@ -371,6 +380,7 @@ int main(int argc, char **argv) int callback9ret = -1; int callback10ret = -1; int callback11ret = -1; + int callback12ret = -1; struct sigaction action_stop; memset(&action_stop, 0, sizeof action_stop); @@ -450,7 +460,12 @@ int main(int argc, char **argv) NULL, VIR_DOMAIN_EVENT_ID_WAKEUP, VIR_DOMAIN_EVENT_CALLBACK(myDomainEventWakeupCallback), - strdup("system wakeup"), myFreeFunc); + strdup("wakeup"), myFreeFunc); + callback12ret = virConnectDomainEventRegisterAny(dconn, + NULL, + VIR_DOMAIN_EVENT_ID_SUSPEND, + VIR_DOMAIN_EVENT_CALLBACK(myDomainEventSuspendCallback), + strdup("suspend"), myFreeFunc); if ((callback1ret != -1) && (callback2ret != -1) && @@ -461,7 +476,8 @@ int main(int argc, char **argv) (callback7ret != -1) && (callback9ret != -1) && (callback10ret != -1) && - (callback11ret != -1)) { + (callback11ret != -1) && + (callback12ret != -1)) { if (virConnectSetKeepAlive(dconn, 5, 3) < 0) { virErrorPtr err = virGetLastError(); fprintf(stderr, "Failed to start keepalive protocol: %s\n", @@ -488,6 +504,7 @@ int main(int argc, char **argv) virConnectDomainEventDeregisterAny(dconn, callback9ret); virConnectDomainEventDeregisterAny(dconn, callback10ret); virConnectDomainEventDeregisterAny(dconn, callback11ret); + virConnectDomainEventDeregisterAny(dconn, callback12ret); if (callback8ret != -1) virConnectDomainEventDeregisterAny(dconn, callback8ret); } diff --git a/examples/domain-events/events-python/event-test.py b/examples/domain-events/events-python/event-test.py index d70ab0a..7698541 100644 --- a/examples/domain-events/events-python/event-test.py +++ b/examples/domain-events/events-python/event-test.py @@ -480,6 +480,9 @@ def myDomainEventTrayMovedCallback(conn, dom, devAlias, trayOpened, opaque): def myDomainEventWakeupCallback(conn, dom, opaque): print "myDomainEventWakeupCallback: Domain %s(%s) system wakeup" % ( dom.name(), dom.ID()) +def myDomainEventSystemCallback(conn, dom, opaque): + print "myDomainEventSuspendCallback: Domain %s(%s) system suspend" % ( + dom.name(), dom.ID()) def usage(out=sys.stderr): print >>out, "usage: "+os.path.basename(sys.argv[0])+" [-hdl] [uri]" print >>out, " uri will default to qemu:///system" @@ -540,6 +543,7 @@ def main(): vc.domainEventRegisterAny(None, libvirt.VIR_DOMAIN_EVENT_ID_DISK_CHANGE, myDomainEventDiskChangeCallback, None) vc.domainEventRegisterAny(None, libvirt.VIR_DOMAIN_EVENT_ID_TRAY_MOVED, myDomainEventTrayMovedCallback, None) vc.domainEventRegisterAny(None, libvirt.VIR_DOMAIN_EVENT_ID_WAKEUP, myDomainEventWakeupCallback, None) + vc.domainEventRegisterAny(None, libvirt.VIR_DOMAIN_EVENT_ID_SUSPEND, myDomainEventSuspendCallback, None) vc.setKeepAlive(5, 3) diff --git a/include/libvirt/libvirt.h.in b/include/libvirt/libvirt.h.in index 775a0c7..aa02bcb 100644 --- a/include/libvirt/libvirt.h.in +++ b/include/libvirt/libvirt.h.in @@ -3677,6 +3677,21 @@ typedef void (*virConnectDomainEventWakeupCallback)(virConnectPtr conn, virDomainPtr dom, void *opaque); +/** + * virConnectDomainEventSuspendCallback: + * @conn: connection object + * @dom: domain on which the event occurred + * @opaque: application specified data + * + * This callback occurs when the guest is waken up. + * + * The callback signature to use when registering for an event of type + * VIR_DOMAIN_EVENT_ID_SUSPEND with virConnectDomainEventRegisterAny() + */ +typedef void (*virConnectDomainEventSuspendCallback)(virConnectPtr conn, + virDomainPtr dom, + void *opaque); + /** * VIR_DOMAIN_EVENT_CALLBACK: @@ -3700,6 +3715,7 @@ typedef enum { VIR_DOMAIN_EVENT_ID_DISK_CHANGE = 9, /* virConnectDomainEventDiskChangeCallback */ VIR_DOMAIN_EVENT_ID_TRAY_MOVED = 10, /* virConnectDomainEventTrayMovedCallback */ VIR_DOMAIN_EVENT_ID_WAKEUP = 11, /* virConnectDomainEventWakeupCallback */ + VIR_DOMAIN_EVENT_ID_SUSPEND = 12, /* virConnectDomainEventSuspendCallback */ #ifdef VIR_ENUM_SENTINELS /* diff --git a/python/libvirt-override-virConnect.py b/python/libvirt-override-virConnect.py index 323bb18..d8d195f 100644 --- a/python/libvirt-override-virConnect.py +++ b/python/libvirt-override-virConnect.py @@ -152,6 +152,15 @@ cb(self, virDomain(self, _obj=dom), opaque) return 0; + def _dispatchDomainEventSuspendCallback(self, dom, cbData): + """Dispatches event to python user domain suspend event callbacks + """ + cb = cbData["cb"] + opaque = cbData["opaque"] + + cb(self, virDomain(self, _obj=dom), opaque) + return 0; + def domainEventDeregisterAny(self, callbackID): """Removes a Domain Event Callback. De-registering for a domain callback will disable delivery of this event type """ diff --git a/python/libvirt-override.c b/python/libvirt-override.c index 3b439a4..d76bc81 100644 --- a/python/libvirt-override.c +++ b/python/libvirt-override.c @@ -5019,6 +5019,51 @@ libvirt_virConnectDomainEventWakeupCallback(virConnectPtr conn ATTRIBUTE_UNUSED, return ret; } +static int +libvirt_virConnectDomainEventSuspendCallback(virConnectPtr conn ATTRIBUTE_UNUSED, + virDomainPtr dom, + void *opaque) +{ + PyObject *pyobj_cbData = (PyObject*)opaque; + PyObject *pyobj_dom; + PyObject *pyobj_ret; + PyObject *pyobj_conn; + PyObject *dictKey; + int ret = -1; + + LIBVIRT_ENSURE_THREAD_STATE; + /* Create a python instance of this virDomainPtr */ + virDomainRef(dom); + + pyobj_dom = libvirt_virDomainPtrWrap(dom); + Py_INCREF(pyobj_cbData); + + dictKey = libvirt_constcharPtrWrap("conn"); + pyobj_conn = PyDict_GetItem(pyobj_cbData, dictKey); + Py_DECREF(dictKey); + + /* Call the Callback Dispatcher */ + pyobj_ret = PyObject_CallMethod(pyobj_conn, + (char*)"_dispatchDomainEventSuspendCallback", + (char*)"OO", + pyobj_dom, + pyobj_cbData); + + Py_DECREF(pyobj_cbData); + Py_DECREF(pyobj_dom); + + if(!pyobj_ret) { + DEBUG("%s - ret:%p\n", __FUNCTION__, pyobj_ret); + PyErr_Print(); + } else { + Py_DECREF(pyobj_ret); + ret = 0; + } + + LIBVIRT_RELEASE_THREAD_STATE; + return ret; +} + static PyObject * libvirt_virConnectDomainEventRegisterAny(ATTRIBUTE_UNUSED PyObject * self, PyObject * args) @@ -5085,6 +5130,9 @@ libvirt_virConnectDomainEventRegisterAny(ATTRIBUTE_UNUSED PyObject * self, case VIR_DOMAIN_EVENT_ID_WAKEUP: cb = VIR_DOMAIN_EVENT_CALLBACK(libvirt_virConnectDomainEventWakeupCallback); break; + case VIR_DOMAIN_EVENT_ID_SUSPEND: + cb = VIR_DOMAIN_EVENT_CALLBACK(libvirt_virConnectDomainEventSuspendCallback); + break; } if (!cb) { diff --git a/src/conf/domain_event.c b/src/conf/domain_event.c index 956d3c2..3bf6088 100644 --- a/src/conf/domain_event.c +++ b/src/conf/domain_event.c @@ -1121,6 +1121,30 @@ virDomainEventWakeupNewFromDom(virDomainPtr dom) return virDomainEventWakeupNew(dom->id, dom->name, dom->uuid); } +static virDomainEventPtr +virDomainEventSuspendNew(int id, const char *name, + unsigned char *uuid) +{ + virDomainEventPtr ev = + virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_SUSPEND, + id, name, uuid); + return ev; +} + +virDomainEventPtr +virDomainEventSuspendNewFromObj(virDomainObjPtr obj) +{ + return virDomainEventSuspendNew(obj->def->id, + obj->def->name, + obj->def->uuid); +} + +virDomainEventPtr +virDomainEventSuspendNewFromDom(virDomainPtr dom) +{ + return virDomainEventSuspendNew(dom->id, dom->name, dom->uuid); +} + /** * virDomainEventQueuePush: @@ -1256,6 +1280,10 @@ virDomainEventDispatchDefaultFunc(virConnectPtr conn, (cb)(conn, dom, cbopaque); break; + case VIR_DOMAIN_EVENT_ID_SUSPEND: + (cb)(conn, dom, cbopaque); + break; + default: VIR_WARN("Unexpected event ID %d", event->eventID); break; diff --git a/src/conf/domain_event.h b/src/conf/domain_event.h index 51b6570..d4155ac 100644 --- a/src/conf/domain_event.h +++ b/src/conf/domain_event.h @@ -122,6 +122,8 @@ virDomainEventPtr virDomainEventTrayMovedNewFromDom(virDomainPtr dom, unsigned int trayOpened); virDomainEventPtr virDomainEventWakeupNewFromObj(virDomainObjPtr obj); virDomainEventPtr virDomainEventWakeupNewFromDom(virDomainPtr dom); +virDomainEventPtr virDomainEventSuspendNewFromObj(virDomainObjPtr obj); +virDomainEventPtr virDomainEventSuspendNewFromDom(virDomainPtr dom); void virDomainEventFree(virDomainEventPtr event); diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index e2e34ca..143e367 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -512,10 +512,12 @@ virDomainEventStateRegisterID; virDomainEventStateFree; virDomainEventStateNew; virDomainEventStateQueue; -virDomainEventWakeupNewFromDom; -virDomainEventWakeupNewFromObj; +virDomainEventSuspendNewFromDom; +virDomainEventSuspendNewFromObj; virDomainEventTrayMovedNewFromDom; virDomainEventTrayMovedNewFromObj; +virDomainEventWakeupNewFromDom; +virDomainEventWakeupNewFromObj; virDomainEventWatchdogNewFromDom; virDomainEventWatchdogNewFromObj; diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c index bf51bdd..564a6c0 100644 --- a/src/qemu/qemu_monitor.c +++ b/src/qemu/qemu_monitor.c @@ -1065,6 +1065,18 @@ qemuMonitorEmitWakeup(qemuMonitorPtr mon) return ret; } +int +qemuMonitorEmitSuspend(qemuMonitorPtr mon) + +{ + int ret = -1; + VIR_DEBUG("mon=%p", mon); + + QEMU_MONITOR_CALLBACK(mon, ret, domainSuspend, mon->vm); + + return ret; +} + int qemuMonitorEmitBlockJob(qemuMonitorPtr mon, const char *diskAlias, int type, diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h index 08f1d7a..5d2ed7b 100644 --- a/src/qemu/qemu_monitor.h +++ b/src/qemu/qemu_monitor.h @@ -130,6 +130,8 @@ struct _qemuMonitorCallbacks { unsigned int trayOpened); int (*domainWakeup)(qemuMonitorPtr mon, virDomainObjPtr vm); + int (*domainSuspend)(qemuMonitorPtr mon, + virDomainObjPtr vm); }; @@ -202,6 +204,7 @@ int qemuMonitorEmitTrayMoved(qemuMonitorPtr mon, const char *devAlias, unsigned int trayOpened); int qemuMonitorEmitWakeup(qemuMonitorPtr mon); +int qemuMonitorEmitSuspend(qemuMonitorPtr mon); int qemuMonitorEmitBlockJob(qemuMonitorPtr mon, const char *diskAlias, int type, diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c index 19cda44..d41818b 100644 --- a/src/qemu/qemu_monitor_json.c +++ b/src/qemu/qemu_monitor_json.c @@ -61,6 +61,7 @@ static void qemuMonitorJSONHandleVNCDisconnect(qemuMonitorPtr mon, virJSONValueP static void qemuMonitorJSONHandleBlockJob(qemuMonitorPtr mon, virJSONValuePtr data); static void qemuMonitorJSONHandleTrayMoved(qemuMonitorPtr mon, virJSONValuePtr data); static void qemuMonitorJSONHandleWakeup(qemuMonitorPtr mon, virJSONValuePtr data); +static void qemuMonitorJSONHandleSuspend(qemuMonitorPtr mon, virJSONValuePtr data); static struct { const char *type; @@ -79,6 +80,7 @@ static struct { { "BLOCK_JOB_COMPLETED", qemuMonitorJSONHandleBlockJob, }, { "DEVICE_TRAY_MOVED", qemuMonitorJSONHandleTrayMoved, }, { "WAKEUP", qemuMonitorJSONHandleWakeup, }, + { "SUSPEND", qemuMonitorJSONHandleSuspend, }, }; @@ -752,6 +754,13 @@ qemuMonitorJSONHandleWakeup(qemuMonitorPtr mon, qemuMonitorEmitWakeup(mon); } +static void +qemuMonitorJSONHandleSuspend(qemuMonitorPtr mon, + virJSONValuePtr data ATTRIBUTE_UNUSED) +{ + qemuMonitorEmitSuspend(mon); +} + int qemuMonitorJSONHumanCommandWithFd(qemuMonitorPtr mon, const char *cmd_str, diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index de173bd..2cff63e 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -1079,6 +1079,27 @@ qemuProcessHandleWakeup(qemuMonitorPtr mon ATTRIBUTE_UNUSED, return 0; } +static int +qemuProcessHandleSuspend(qemuMonitorPtr mon ATTRIBUTE_UNUSED, + virDomainObjPtr vm) +{ + struct qemud_driver *driver = qemu_driver; + virDomainEventPtr event = NULL; + + virDomainObjLock(vm); + event = virDomainEventSuspendNewFromObj(vm); + + virDomainObjUnlock(vm); + + if (event) { + qemuDriverLock(driver); + qemuDomainEventQueue(driver, event); + qemuDriverUnlock(driver); + } + + return 0; +} + static qemuMonitorCallbacks monitorCallbacks = { .destroy = qemuProcessHandleMonitorDestroy, .eofNotify = qemuProcessHandleMonitorEOF, @@ -1094,6 +1115,7 @@ static qemuMonitorCallbacks monitorCallbacks = { .domainBlockJob = qemuProcessHandleBlockJob, .domainTrayMoved = qemuProcessHandleTrayMoved, .domainWakeup = qemuProcessHandleWakeup, + .domainSuspend = qemuProcessHandleSuspend, }; static int diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index dd68738..ace841f 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -248,6 +248,11 @@ remoteDomainBuildEventWakeup(virNetClientProgramPtr prog, virNetClientPtr client, void *evdata, void *opaque); +static void +remoteDomainBuildEventSuspend(virNetClientProgramPtr prog, + virNetClientPtr client, + void *evdata, void *opaque); + static virNetClientProgramEvent remoteDomainEvents[] = { { REMOTE_PROC_DOMAIN_EVENT_RTC_CHANGE, remoteDomainBuildEventRTCChange, @@ -297,6 +302,10 @@ static virNetClientProgramEvent remoteDomainEvents[] = { remoteDomainBuildEventWakeup, sizeof(remote_domain_event_wakeup_msg), (xdrproc_t)xdr_remote_domain_event_wakeup_msg }, + { REMOTE_PROC_DOMAIN_EVENT_SUSPEND, + remoteDomainBuildEventSuspend, + sizeof(remote_domain_event_suspend_msg), + (xdrproc_t)xdr_remote_domain_event_suspend_msg }, }; enum virDrvOpenRemoteFlags { @@ -3709,6 +3718,29 @@ remoteDomainBuildEventWakeup(virNetClientProgramPtr prog ATTRIBUTE_UNUSED, } +static void +remoteDomainBuildEventSuspend(virNetClientProgramPtr prog ATTRIBUTE_UNUSED, + virNetClientPtr client ATTRIBUTE_UNUSED, + void *evdata, void *opaque) +{ + virConnectPtr conn = opaque; + struct private_data *priv = conn->privateData; + remote_domain_event_suspend_msg *msg = evdata; + virDomainPtr dom; + virDomainEventPtr event = NULL; + + dom = get_nonnull_domain(conn, msg->dom); + if (!dom) + return; + + event = virDomainEventSuspendNewFromDom(dom); + + virDomainFree(dom); + + remoteDomainEventQueue(priv, event); +} + + static virDrvOpenStatus ATTRIBUTE_NONNULL (1) remoteSecretOpen(virConnectPtr conn, virConnectAuthPtr auth, unsigned int flags) diff --git a/src/remote/remote_protocol.x b/src/remote/remote_protocol.x index 5695fa2..cd3e1d5 100644 --- a/src/remote/remote_protocol.x +++ b/src/remote/remote_protocol.x @@ -2188,6 +2188,10 @@ struct remote_domain_event_wakeup_msg { remote_nonnull_domain dom; }; +struct remote_domain_event_suspend_msg { + remote_nonnull_domain dom; +}; + struct remote_domain_managed_save_args { remote_nonnull_domain dom; unsigned int flags; @@ -2777,7 +2781,8 @@ enum remote_procedure { REMOTE_PROC_DOMAIN_BLOCK_REBASE = 266, /* autogen autogen */ REMOTE_PROC_DOMAIN_PM_WAKEUP = 267, /* autogen autogen */ REMOTE_PROC_DOMAIN_EVENT_TRAY_MOVED = 268, /* autogen autogen */ - REMOTE_PROC_DOMAIN_EVENT_WAKEUP = 269 /* autogen autogen */ + REMOTE_PROC_DOMAIN_EVENT_WAKEUP = 269, /* autogen autogen */ + REMOTE_PROC_DOMAIN_EVENT_SUSPEND = 270 /* autogen autogen */ /* * Notice how the entries are grouped in sets of 10 ? diff --git a/src/remote_protocol-structs b/src/remote_protocol-structs index 2911610..6918397 100644 --- a/src/remote_protocol-structs +++ b/src/remote_protocol-structs @@ -1658,6 +1658,9 @@ struct remote_domain_event_tray_moved_msg { struct remote_domain_event_wakeup_msg { remote_nonnull_domain dom; }; +struct remote_domain_event_suspend_msg { + remote_nonnull_domain dom; +}; struct remote_domain_managed_save_args { remote_nonnull_domain dom; u_int flags; -- 1.7.1 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list