This introduces a new event type VIR_DOMAIN_EVENT_ID_WATCHDOG This event includes the action that is about to be taken as a result of the watchdog triggering typedef enum { VIR_DOMAIN_EVENT_WATCHDOG_NONE = 0, VIR_DOMAIN_EVENT_WATCHDOG_PAUSE, VIR_DOMAIN_EVENT_WATCHDOG_RESET, VIR_DOMAIN_EVENT_WATCHDOG_POWEROFF, VIR_DOMAIN_EVENT_WATCHDOG_SHUTDOWN, VIR_DOMAIN_EVENT_WATCHDOG_DEBUG, } virDomainEventWatchdogAction; Thus there is a new callback definition for this event type typedef void (*virConnectDomainEventWatchdogCallback)(virConnectPtr conn, virDomainPtr dom, int action, void *opaque); * daemon/remote.c: Dispatch watchdog events to client * examples/domain-events/events-c/event-test.c: Watch for watchdog events * include/libvirt/libvirt.h.in: Define new watchdg event ID and callback signature * src/conf/domain_event.c, src/conf/domain_event.h, src/libvirt_private.syms: Extend API to handle watchdog events * src/qemu/qemu_driver.c: Connect to the QEMU monitor event for watchdogs and emit a libvirt watchdog event * src/remote/remote_driver.c: Receive and dispatch watchdog events to application * src/remote/remote_protocol.x: Wire protocol definition for watchdog events * src/qemu/qemu_monitor.c, src/qemu/qemu_monitor.h, src/qemu/qemu_monitor_json.c: Watch for WATCHDOG event from QEMU monitor --- daemon/remote.c | 31 +++++++++++++++++++++++ daemon/remote_dispatch_table.h | 5 ++++ examples/domain-events/events-c/event-test.c | 22 +++++++++++++++- include/libvirt/libvirt.h.in | 13 ++++++++++ src/conf/domain_event.c | 34 ++++++++++++++++++++++++++ src/conf/domain_event.h | 3 ++ src/libvirt_private.syms | 2 + src/qemu/qemu_driver.c | 23 +++++++++++++++++ src/qemu/qemu_monitor.c | 15 +++++++++++ src/qemu/qemu_monitor.h | 4 +++ src/qemu/qemu_monitor_json.c | 24 ++++++++++++++++++ src/remote/remote_driver.c | 31 +++++++++++++++++++++++ src/remote/remote_protocol.c | 13 +++++++++- src/remote/remote_protocol.h | 11 +++++++- src/remote/remote_protocol.x | 8 +++++- 15 files changed, 234 insertions(+), 5 deletions(-) diff --git a/daemon/remote.c b/daemon/remote.c index ac62f43..b1562b6 100644 --- a/daemon/remote.c +++ b/daemon/remote.c @@ -187,10 +187,41 @@ static int remoteRelayDomainEventRTCChange(virConnectPtr conn ATTRIBUTE_UNUSED, } +static int remoteRelayDomainEventWatchdog(virConnectPtr conn ATTRIBUTE_UNUSED, + virDomainPtr dom, + int action, + void *opaque) +{ + struct qemud_client *client = opaque; + remote_domain_event_watchdog_msg data; + + if (!client) + return -1; + + REMOTE_DEBUG("Relaying domain watchdog event %s %d %d", dom->name, dom->id, action); + + virMutexLock(&client->lock); + + /* build return data */ + memset(&data, 0, sizeof data); + make_nonnull_domain (&data.dom, dom); + data.action = action; + + remoteDispatchDomainEventSend (client, + REMOTE_PROC_DOMAIN_EVENT_WATCHDOG, + (xdrproc_t)xdr_remote_domain_event_watchdog_msg, &data); + + virMutexUnlock(&client->lock); + + return 0; +} + + static virConnectDomainEventGenericCallback domainEventCallbacks[] = { VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle), VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventReboot), VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventRTCChange), + VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventWatchdog), }; verify(ARRAY_CARDINALITY(domainEventCallbacks) == VIR_DOMAIN_EVENT_ID_LAST); diff --git a/daemon/remote_dispatch_table.h b/daemon/remote_dispatch_table.h index e5cc724..11520b9 100644 --- a/daemon/remote_dispatch_table.h +++ b/daemon/remote_dispatch_table.h @@ -847,3 +847,8 @@ .args_filter = (xdrproc_t) xdr_void, .ret_filter = (xdrproc_t) xdr_void, }, +{ /* Async event DomainEventWatchdog => 169 */ + .fn = NULL, + .args_filter = (xdrproc_t) xdr_void, + .ret_filter = (xdrproc_t) xdr_void, +}, diff --git a/examples/domain-events/events-c/event-test.c b/examples/domain-events/events-c/event-test.c index 97b37de..997af63 100644 --- a/examples/domain-events/events-c/event-test.c +++ b/examples/domain-events/events-c/event-test.c @@ -193,6 +193,17 @@ static int myDomainEventRTCChangeCallback(virConnectPtr conn ATTRIBUTE_UNUSED, return 0; } +static int myDomainEventWatchdogCallback(virConnectPtr conn ATTRIBUTE_UNUSED, + virDomainPtr dom, + int action, + void *opaque ATTRIBUTE_UNUSED) +{ + printf("%s EVENT: Domain %s(%d) watchdog action=%d\n", __func__, virDomainGetName(dom), + virDomainGetID(dom), action); + + return 0; +} + static void myFreeFunc(void *opaque) { char *str = opaque; @@ -312,6 +323,7 @@ int main(int argc, char **argv) int callback2ret = -1; int callback3ret = -1; int callback4ret = -1; + int callback5ret = -1; struct sigaction action_stop = { .sa_handler = stop @@ -359,11 +371,17 @@ int main(int argc, char **argv) VIR_DOMAIN_EVENT_ID_RTC_CHANGE, VIR_DOMAIN_EVENT_CALLBACK(myDomainEventRTCChangeCallback), strdup("callback rtcchange"), myFreeFunc); + callback5ret = virConnectDomainEventRegisterAny(dconn, + NULL, + VIR_DOMAIN_EVENT_ID_WATCHDOG, + VIR_DOMAIN_EVENT_CALLBACK(myDomainEventWatchdogCallback), + strdup("callback watchdog"), myFreeFunc); if ((callback1ret != -1) && (callback2ret != -1) && (callback3ret != -1) && - (callback4ret != -1)) { + (callback4ret != -1) && + (callback5ret != -1)) { while(run) { struct pollfd pfd = { .fd = h_fd, .events = h_event, @@ -403,7 +421,7 @@ int main(int argc, char **argv) virConnectDomainEventDeregisterAny(dconn, callback2ret); virConnectDomainEventDeregisterAny(dconn, callback3ret); virConnectDomainEventDeregisterAny(dconn, callback4ret); - + virConnectDomainEventDeregisterAny(dconn, callback5ret); } DEBUG0("Closing connection"); diff --git a/include/libvirt/libvirt.h.in b/include/libvirt/libvirt.h.in index edb0a6e..7bee89c 100644 --- a/include/libvirt/libvirt.h.in +++ b/include/libvirt/libvirt.h.in @@ -1846,6 +1846,18 @@ typedef void (*virConnectDomainEventRTCChangeCallback)(virConnectPtr conn, virDomainPtr dom, long long utcoffset, void *opaque); +typedef enum { + VIR_DOMAIN_EVENT_WATCHDOG_NONE = 0, + VIR_DOMAIN_EVENT_WATCHDOG_PAUSE, + VIR_DOMAIN_EVENT_WATCHDOG_RESET, + VIR_DOMAIN_EVENT_WATCHDOG_POWEROFF, + VIR_DOMAIN_EVENT_WATCHDOG_SHUTDOWN, + VIR_DOMAIN_EVENT_WATCHDOG_DEBUG, +} virDomainEventWatchdogAction; +typedef void (*virConnectDomainEventWatchdogCallback)(virConnectPtr conn, + virDomainPtr dom, + int action, + void *opaque); /* Use this to cast the event specific callback into the generic one * for use for virDomainEventRegister */ @@ -1856,6 +1868,7 @@ typedef enum { VIR_DOMAIN_EVENT_ID_LIFECYCLE = 0, /* virConnectDomainEventCallback */ VIR_DOMAIN_EVENT_ID_REBOOT = 1, /* virConnectDomainEventGenericCallback */ VIR_DOMAIN_EVENT_ID_RTC_CHANGE = 2, /* virConnectDomainEventRTCChangeCallback */ + VIR_DOMAIN_EVENT_ID_WATCHDOG = 3, /* virConnectDomainEventWatchdogCallback */ /* * NB: this enum value will increase over time as new events are diff --git a/src/conf/domain_event.c b/src/conf/domain_event.c index 79dfbac..4482599 100644 --- a/src/conf/domain_event.c +++ b/src/conf/domain_event.c @@ -67,6 +67,9 @@ struct _virDomainEvent { struct { long long offset; } rtcChange; + struct { + int action; + } watchdog; } data; }; @@ -562,6 +565,31 @@ virDomainEventPtr virDomainEventRTCChangeNewFromObj(virDomainObjPtr obj, return ev; } +virDomainEventPtr virDomainEventWatchdogNewFromDom(virDomainPtr dom, + int action) +{ + virDomainEventPtr ev = + virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_WATCHDOG, + dom->id, dom->name, dom->uuid); + + if (ev) + ev->data.watchdog.action = action; + + return ev; +} +virDomainEventPtr virDomainEventWatchdogNewFromObj(virDomainObjPtr obj, + int action) +{ + virDomainEventPtr ev = + virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_WATCHDOG, + obj->def->id, obj->def->name, obj->def->uuid); + + if (ev) + ev->data.watchdog.action = action; + + return ev; +} + /** * virDomainEventQueueFree: * @queue: pointer to the queue @@ -678,6 +706,12 @@ void virDomainEventDispatchDefaultFunc(virConnectPtr conn, cbopaque); break; + case VIR_DOMAIN_EVENT_ID_WATCHDOG: + ((virConnectDomainEventWatchdogCallback)cb)(conn, dom, + event->data.watchdog.action, + 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 8517ffe..b791614 100644 --- a/src/conf/domain_event.h +++ b/src/conf/domain_event.h @@ -118,6 +118,9 @@ virDomainEventPtr virDomainEventRebootNewFromObj(virDomainObjPtr obj); virDomainEventPtr virDomainEventRTCChangeNewFromDom(virDomainPtr dom, long long offset); virDomainEventPtr virDomainEventRTCChangeNewFromObj(virDomainObjPtr obj, long long offset); +virDomainEventPtr virDomainEventWatchdogNewFromDom(virDomainPtr dom, int action); +virDomainEventPtr virDomainEventWatchdogNewFromObj(virDomainObjPtr obj, int action); + int virDomainEventQueuePush(virDomainEventQueuePtr evtQueue, virDomainEventPtr event); diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 582a98e..17902b1 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -218,6 +218,8 @@ virDomainEventRebootNewFromDom; virDomainEventRebootNewFromObj; virDomainEventRTCChangeNewFromDom; virDomainEventRTCChangeNewFromObj; +virDomainEventWatchdogNewFromDom; +virDomainEventWatchdogNewFromObj; virDomainEventFree; virDomainEventDispatchDefaultFunc; virDomainEventDispatch; diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index a5cf246..ceba5db 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -906,11 +906,34 @@ qemuHandleDomainRTCChange(qemuMonitorPtr mon ATTRIBUTE_UNUSED, } +static int +qemuHandleDomainWatchdog(qemuMonitorPtr mon ATTRIBUTE_UNUSED, + virDomainObjPtr vm, + int action) +{ + struct qemud_driver *driver = qemu_driver; + virDomainEventPtr event; + + virDomainObjLock(vm); + event = virDomainEventWatchdogNewFromObj(vm, action); + virDomainObjUnlock(vm); + + if (event) { + qemuDriverLock(driver); + qemuDomainEventQueue(driver, event); + qemuDriverUnlock(driver); + } + + return 0; +} + + static qemuMonitorCallbacks monitorCallbacks = { .eofNotify = qemuHandleMonitorEOF, .diskSecretLookup = findVolumeQcowPassphrase, .domainReset = qemuHandleDomainReset, .domainRTCChange = qemuHandleDomainRTCChange, + .domainWatchdog = qemuHandleDomainWatchdog, }; static int diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c index c0e2384..d7116c9 100644 --- a/src/qemu/qemu_monitor.c +++ b/src/qemu/qemu_monitor.c @@ -806,6 +806,21 @@ int qemuMonitorEmitRTCChange(qemuMonitorPtr mon, long long offset) } +int qemuMonitorEmitWatchdog(qemuMonitorPtr mon, int action) +{ + int ret = -1; + VIR_DEBUG("mon=%p", mon); + + qemuMonitorRef(mon); + qemuMonitorUnlock(mon); + if (mon->cb && mon->cb->domainWatchdog) + ret = mon->cb->domainWatchdog(mon, mon->vm, action); + qemuMonitorLock(mon); + qemuMonitorUnref(mon); + return ret; +} + + int qemuMonitorSetCapabilities(qemuMonitorPtr mon) { int ret; diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h index 86d6b87..3558dba 100644 --- a/src/qemu/qemu_monitor.h +++ b/src/qemu/qemu_monitor.h @@ -89,6 +89,9 @@ struct _qemuMonitorCallbacks { int (*domainRTCChange)(qemuMonitorPtr mon, virDomainObjPtr vm, long long offset); + int (*domainWatchdog)(qemuMonitorPtr mon, + virDomainObjPtr vm, + int action); }; @@ -126,6 +129,7 @@ int qemuMonitorEmitReset(qemuMonitorPtr mon); int qemuMonitorEmitPowerdown(qemuMonitorPtr mon); int qemuMonitorEmitStop(qemuMonitorPtr mon); int qemuMonitorEmitRTCChange(qemuMonitorPtr mon, long long offset); +int qemuMonitorEmitWatchdog(qemuMonitorPtr mon, int action); int qemuMonitorStartCPUs(qemuMonitorPtr mon, virConnectPtr conn); diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c index ce7796a..d7fbf38 100644 --- a/src/qemu/qemu_monitor_json.c +++ b/src/qemu/qemu_monitor_json.c @@ -50,6 +50,7 @@ static void qemuMonitorJSONHandleReset(qemuMonitorPtr mon, virJSONValuePtr data) static void qemuMonitorJSONHandlePowerdown(qemuMonitorPtr mon, virJSONValuePtr data); static void qemuMonitorJSONHandleStop(qemuMonitorPtr mon, virJSONValuePtr data); static void qemuMonitorJSONHandleRTCChange(qemuMonitorPtr mon, virJSONValuePtr data); +static void qemuMonitorJSONHandleWatchdog(qemuMonitorPtr mon, virJSONValuePtr data); struct { const char *type; @@ -60,6 +61,7 @@ struct { { "POWERDOWN", qemuMonitorJSONHandlePowerdown, }, { "STOP", qemuMonitorJSONHandleStop, }, { "RTC_CHANGE", qemuMonitorJSONHandleRTCChange, }, + { "WATCHDOG", qemuMonitorJSONHandleWatchdog, }, }; @@ -507,6 +509,28 @@ static void qemuMonitorJSONHandleRTCChange(qemuMonitorPtr mon, virJSONValuePtr d qemuMonitorEmitRTCChange(mon, offset); } +VIR_ENUM_DECL(qemuMonitorWatchdogAction) +VIR_ENUM_IMPL(qemuMonitorWatchdogAction, VIR_DOMAIN_EVENT_WATCHDOG_DEBUG, + "none", "pause", "reset", "poweroff" "shutdown", "debug"); + +static void qemuMonitorJSONHandleWatchdog(qemuMonitorPtr mon, virJSONValuePtr data) +{ + const char *action; + int actionID; + if (!(action = virJSONValueObjectGetString(data, "action"))) { + VIR_WARN0("missing action in watchdog event"); + } + if (action) { + if ((actionID = qemuMonitorWatchdogActionTypeFromString(action)) < 0) { + VIR_WARN("unknown action %s in watchdog event", action); + actionID = VIR_DOMAIN_EVENT_WATCHDOG_NONE; + } + } else { + actionID = VIR_DOMAIN_EVENT_WATCHDOG_NONE; + } + qemuMonitorEmitRTCChange(mon, actionID); +} + int qemuMonitorJSONSetCapabilities(qemuMonitorPtr mon) diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index afaa26c..8652083 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -6941,6 +6941,33 @@ remoteDomainReadEventRTCChange(virConnectPtr conn, XDR *xdr) } +static virDomainEventPtr +remoteDomainReadEventWatchdog(virConnectPtr conn, XDR *xdr) +{ + remote_domain_event_watchdog_msg msg; + virDomainPtr dom; + virDomainEventPtr event = NULL; + memset (&msg, 0, sizeof msg); + + /* unmarshall parameters, and process it*/ + if (! xdr_remote_domain_event_watchdog_msg(xdr, &msg) ) { + error (conn, VIR_ERR_RPC, + _("unable to demarshall reboot event")); + return NULL; + } + + dom = get_nonnull_domain(conn,msg.dom); + if (!dom) + return NULL; + + event = virDomainEventWatchdogNewFromDom(dom, msg.action); + xdr_free ((xdrproc_t) &xdr_remote_domain_event_watchdog_msg, (char *) &msg); + + virDomainFree(dom); + return event; +} + + static virDrvOpenStatus ATTRIBUTE_NONNULL (1) remoteSecretOpen (virConnectPtr conn, virConnectAuthPtr auth, @@ -8462,6 +8489,10 @@ processCallDispatchMessage(virConnectPtr conn, struct private_data *priv, event = remoteDomainReadEventRTCChange(conn, xdr); break; + case REMOTE_PROC_DOMAIN_EVENT_WATCHDOG: + event = remoteDomainReadEventWatchdog(conn, xdr); + break; + default: DEBUG("Unexpected event proc %d", hdr->proc); break; diff --git a/src/remote/remote_protocol.c b/src/remote/remote_protocol.c index f78bea0..8a6c353 100644 --- a/src/remote/remote_protocol.c +++ b/src/remote/remote_protocol.c @@ -3041,7 +3041,18 @@ xdr_remote_domain_event_rtc_change_msg (XDR *xdrs, remote_domain_event_rtc_chang if (!xdr_remote_nonnull_domain (xdrs, &objp->dom)) return FALSE; - if (!xdr_uint64_t (xdrs, &objp->offset)) + if (!xdr_int64_t (xdrs, &objp->offset)) + return FALSE; + return TRUE; +} + +bool_t +xdr_remote_domain_event_watchdog_msg (XDR *xdrs, remote_domain_event_watchdog_msg *objp) +{ + + if (!xdr_remote_nonnull_domain (xdrs, &objp->dom)) + return FALSE; + if (!xdr_int (xdrs, &objp->action)) return FALSE; return TRUE; } diff --git a/src/remote/remote_protocol.h b/src/remote/remote_protocol.h index 4afd5f3..f68989f 100644 --- a/src/remote/remote_protocol.h +++ b/src/remote/remote_protocol.h @@ -1722,9 +1722,15 @@ typedef struct remote_domain_event_reboot_msg remote_domain_event_reboot_msg; struct remote_domain_event_rtc_change_msg { remote_nonnull_domain dom; - uint64_t offset; + int64_t offset; }; typedef struct remote_domain_event_rtc_change_msg remote_domain_event_rtc_change_msg; + +struct remote_domain_event_watchdog_msg { + remote_nonnull_domain dom; + int action; +}; +typedef struct remote_domain_event_watchdog_msg remote_domain_event_watchdog_msg; #define REMOTE_PROGRAM 0x20008086 #define REMOTE_PROTOCOL_VERSION 1 @@ -1897,6 +1903,7 @@ enum remote_procedure { REMOTE_PROC_DOMAIN_EVENTS_DEREGISTER_ANY = 166, REMOTE_PROC_DOMAIN_EVENT_REBOOT = 167, REMOTE_PROC_DOMAIN_EVENT_RTC_CHANGE = 168, + REMOTE_PROC_DOMAIN_EVENT_WATCHDOG = 169, }; typedef enum remote_procedure remote_procedure; @@ -2210,6 +2217,7 @@ extern bool_t xdr_remote_domain_events_register_any_args (XDR *, remote_domain_ extern bool_t xdr_remote_domain_events_deregister_any_args (XDR *, remote_domain_events_deregister_any_args*); extern bool_t xdr_remote_domain_event_reboot_msg (XDR *, remote_domain_event_reboot_msg*); extern bool_t xdr_remote_domain_event_rtc_change_msg (XDR *, remote_domain_event_rtc_change_msg*); +extern bool_t xdr_remote_domain_event_watchdog_msg (XDR *, remote_domain_event_watchdog_msg*); extern bool_t xdr_remote_procedure (XDR *, remote_procedure*); extern bool_t xdr_remote_message_type (XDR *, remote_message_type*); extern bool_t xdr_remote_message_status (XDR *, remote_message_status*); @@ -2497,6 +2505,7 @@ extern bool_t xdr_remote_domain_events_register_any_args (); extern bool_t xdr_remote_domain_events_deregister_any_args (); extern bool_t xdr_remote_domain_event_reboot_msg (); extern bool_t xdr_remote_domain_event_rtc_change_msg (); +extern bool_t xdr_remote_domain_event_watchdog_msg (); extern bool_t xdr_remote_procedure (); extern bool_t xdr_remote_message_type (); extern bool_t xdr_remote_message_status (); diff --git a/src/remote/remote_protocol.x b/src/remote/remote_protocol.x index 0e0d59a..f793968 100644 --- a/src/remote/remote_protocol.x +++ b/src/remote/remote_protocol.x @@ -1534,6 +1534,11 @@ struct remote_domain_event_rtc_change_msg { hyper offset; }; +struct remote_domain_event_watchdog_msg { + remote_nonnull_domain dom; + int action; +}; + /*----- Protocol. -----*/ /* Define the program number, protocol version and procedure numbers here. */ @@ -1724,7 +1729,8 @@ enum remote_procedure { REMOTE_PROC_DOMAIN_EVENTS_REGISTER_ANY = 165, REMOTE_PROC_DOMAIN_EVENTS_DEREGISTER_ANY = 166, REMOTE_PROC_DOMAIN_EVENT_REBOOT = 167, - REMOTE_PROC_DOMAIN_EVENT_RTC_CHANGE = 168 + REMOTE_PROC_DOMAIN_EVENT_RTC_CHANGE = 168, + REMOTE_PROC_DOMAIN_EVENT_WATCHDOG = 169 /* * Notice how the entries are grouped in sets of 10 ? -- 1.6.6.1 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list