提交 32e6ac9c 编写于 作者: D Daniel P. Berrange

Add support for an explicit RTC change event

This introduces a new event type

   VIR_DOMAIN_EVENT_ID_RTC_CHANGE

This event includes the new UTC offset measured in seconds.
Thus there is a new callback definition for this event type

 typedef void (*virConnectDomainEventRTCChangeCallback)(virConnectPtr conn,
                                                        virDomainPtr dom,
                                                        long long utcoffset,
                                                        void *opaque);

If the guest XML configuration for the <clock> is set to
offset='variable', then the XML will automatically be
updated with the new UTC offset value. This ensures that
during migration/save/restore the new offset is preserved.

* daemon/remote.c: Dispatch RTC change events to client
* examples/domain-events/events-c/event-test.c: Watch for
  RTC change events
* include/libvirt/libvirt.h.in: Define new RTC change event ID
  and callback signature
* src/conf/domain_event.c, src/conf/domain_event.h,
  src/libvirt_private.syms: Extend API to handle RTC change events
* src/qemu/qemu_driver.c: Connect to the QEMU monitor event
  for RTC changes and emit a libvirt RTC change event
* src/remote/remote_driver.c: Receive and dispatch RTC change
  events to application
* src/remote/remote_protocol.x: Wire protocol definition for
  RTC change events
* src/qemu/qemu_monitor.c, src/qemu/qemu_monitor.h,
  src/qemu/qemu_monitor_json.c: Watch for RTC_CHANGE event
  from QEMU monitor
上级 86132734
......@@ -157,9 +157,40 @@ static int remoteRelayDomainEventReboot(virConnectPtr conn ATTRIBUTE_UNUSED,
}
static int remoteRelayDomainEventRTCChange(virConnectPtr conn ATTRIBUTE_UNUSED,
virDomainPtr dom,
long long offset,
void *opaque)
{
struct qemud_client *client = opaque;
remote_domain_event_rtc_change_msg data;
if (!client)
return -1;
REMOTE_DEBUG("Relaying domain rtc change event %s %d %lld", dom->name, dom->id, offset);
virMutexLock(&client->lock);
/* build return data */
memset(&data, 0, sizeof data);
make_nonnull_domain (&data.dom, dom);
data.offset = offset;
remoteDispatchDomainEventSend (client,
REMOTE_PROC_DOMAIN_EVENT_RTC_CHANGE,
(xdrproc_t)xdr_remote_domain_event_rtc_change_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),
};
verify(ARRAY_CARDINALITY(domainEventCallbacks) == VIR_DOMAIN_EVENT_ID_LAST);
......
......@@ -852,3 +852,8 @@
.args_filter = (xdrproc_t) xdr_void,
.ret_filter = (xdrproc_t) xdr_void,
},
{ /* Async event DomainEventRtcChange => 170 */
.fn = NULL,
.args_filter = (xdrproc_t) xdr_void,
.ret_filter = (xdrproc_t) xdr_void,
},
......@@ -182,6 +182,17 @@ static int myDomainEventRebootCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
return 0;
}
static int myDomainEventRTCChangeCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
virDomainPtr dom,
long long offset,
void *opaque ATTRIBUTE_UNUSED)
{
printf("%s EVENT: Domain %s(%d) rtc change %lld\n", __func__, virDomainGetName(dom),
virDomainGetID(dom), offset);
return 0;
}
static void myFreeFunc(void *opaque)
{
char *str = opaque;
......@@ -300,6 +311,7 @@ int main(int argc, char **argv)
int callback1ret = -1;
int callback2ret = -1;
int callback3ret = -1;
int callback4ret = -1;
struct sigaction action_stop = {
.sa_handler = stop
......@@ -342,10 +354,16 @@ int main(int argc, char **argv)
VIR_DOMAIN_EVENT_ID_REBOOT,
VIR_DOMAIN_EVENT_CALLBACK(myDomainEventRebootCallback),
strdup("callback reboot"), myFreeFunc);
callback4ret = virConnectDomainEventRegisterAny(dconn,
NULL,
VIR_DOMAIN_EVENT_ID_RTC_CHANGE,
VIR_DOMAIN_EVENT_CALLBACK(myDomainEventRTCChangeCallback),
strdup("callback rtcchange"), myFreeFunc);
if ((callback1ret != -1) &&
(callback2ret != -1) &&
(callback3ret != -1)) {
(callback3ret != -1) &&
(callback4ret != -1)) {
while(run) {
struct pollfd pfd = { .fd = h_fd,
.events = h_event,
......@@ -384,6 +402,7 @@ int main(int argc, char **argv)
virConnectDomainEventDeregister(dconn, myDomainEventCallback1);
virConnectDomainEventDeregisterAny(dconn, callback2ret);
virConnectDomainEventDeregisterAny(dconn, callback3ret);
virConnectDomainEventDeregisterAny(dconn, callback4ret);
}
......
......@@ -1863,6 +1863,21 @@ typedef void (*virConnectDomainEventGenericCallback)(virConnectPtr conn,
virDomainPtr dom,
void *opaque);
/**
* virConnectDomainEventRTCChangeCallback:
* @conn: connection object
* @dom: domain on which the event occurred
* @utcoffset: the new RTC offset from UTC, measured in seconds
* @opaque: application specified data
*
* The callback signature to use when registering for an event of type
* VIR_DOMAIN_EVENT_ID_RTC_CHANGE with virConnectDomainEventRegisterAny()
*/
typedef void (*virConnectDomainEventRTCChangeCallback)(virConnectPtr conn,
virDomainPtr dom,
long long utcoffset,
void *opaque);
/**
* VIR_DOMAIN_EVENT_CALLBACK:
*
......@@ -1875,6 +1890,7 @@ typedef void (*virConnectDomainEventGenericCallback)(virConnectPtr conn,
typedef enum {
VIR_DOMAIN_EVENT_ID_LIFECYCLE = 0, /* virConnectDomainEventCallback */
VIR_DOMAIN_EVENT_ID_REBOOT = 1, /* virConnectDomainEventGenericCallback */
VIR_DOMAIN_EVENT_ID_RTC_CHANGE = 2, /* virConnectDomainEventRTCChangeCallback */
/*
* NB: this enum value will increase over time as new events are
......
......@@ -170,6 +170,7 @@ skipped_types = {
# 'int *': "usually a return type",
'virConnectDomainEventCallback': "No function types in python",
'virConnectDomainEventGenericCallback': "No function types in python",
'virConnectDomainEventRTCChangeCallback': "No function types in python",
'virEventAddHandleFunc': "No function types in python",
}
......
......@@ -64,6 +64,9 @@ struct _virDomainEvent {
int type;
int detail;
} lifecycle;
struct {
long long offset;
} rtcChange;
} data;
};
......@@ -534,6 +537,31 @@ virDomainEventPtr virDomainEventRebootNewFromObj(virDomainObjPtr obj)
obj->def->id, obj->def->name, obj->def->uuid);
}
virDomainEventPtr virDomainEventRTCChangeNewFromDom(virDomainPtr dom,
long long offset)
{
virDomainEventPtr ev =
virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_RTC_CHANGE,
dom->id, dom->name, dom->uuid);
if (ev)
ev->data.rtcChange.offset = offset;
return ev;
}
virDomainEventPtr virDomainEventRTCChangeNewFromObj(virDomainObjPtr obj,
long long offset)
{
virDomainEventPtr ev =
virDomainEventNewInternal(VIR_DOMAIN_EVENT_ID_RTC_CHANGE,
obj->def->id, obj->def->name, obj->def->uuid);
if (ev)
ev->data.rtcChange.offset = offset;
return ev;
}
/**
* virDomainEventQueueFree:
* @queue: pointer to the queue
......@@ -644,6 +672,12 @@ void virDomainEventDispatchDefaultFunc(virConnectPtr conn,
cbopaque);
break;
case VIR_DOMAIN_EVENT_ID_RTC_CHANGE:
((virConnectDomainEventRTCChangeCallback)cb)(conn, dom,
event->data.rtcChange.offset,
cbopaque);
break;
default:
VIR_WARN("Unexpected event ID %d", event->eventID);
break;
......
......@@ -115,6 +115,9 @@ virDomainEventPtr virDomainEventNewFromDef(virDomainDefPtr def, int type, int de
virDomainEventPtr virDomainEventRebootNewFromDom(virDomainPtr dom);
virDomainEventPtr virDomainEventRebootNewFromObj(virDomainObjPtr obj);
virDomainEventPtr virDomainEventRTCChangeNewFromDom(virDomainPtr dom, long long offset);
virDomainEventPtr virDomainEventRTCChangeNewFromObj(virDomainObjPtr obj, long long offset);
int virDomainEventQueuePush(virDomainEventQueuePtr evtQueue,
virDomainEventPtr event);
......
......@@ -216,6 +216,8 @@ virDomainEventNewFromObj;
virDomainEventNewFromDef;
virDomainEventRebootNewFromDom;
virDomainEventRebootNewFromObj;
virDomainEventRTCChangeNewFromDom;
virDomainEventRTCChangeNewFromObj;
virDomainEventFree;
virDomainEventDispatchDefaultFunc;
virDomainEventDispatch;
......
......@@ -886,10 +886,40 @@ qemuHandleDomainReset(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
}
static int
qemuHandleDomainRTCChange(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
virDomainObjPtr vm,
long long offset)
{
struct qemud_driver *driver = qemu_driver;
virDomainEventPtr event;
virDomainObjLock(vm);
event = virDomainEventRTCChangeNewFromObj(vm, offset);
if (vm->def->clock.offset == VIR_DOMAIN_CLOCK_OFFSET_VARIABLE)
vm->def->clock.data.adjustment = offset;
if (virDomainSaveStatus(driver->caps, driver->stateDir, vm) < 0)
VIR_WARN0("unable to save domain status with RTC change");
virDomainObjUnlock(vm);
if (event) {
qemuDriverLock(driver);
qemuDomainEventQueue(driver, event);
qemuDriverUnlock(driver);
}
return 0;
}
static qemuMonitorCallbacks monitorCallbacks = {
.eofNotify = qemuHandleMonitorEOF,
.diskSecretLookup = findVolumeQcowPassphrase,
.domainReset = qemuHandleDomainReset,
.domainRTCChange = qemuHandleDomainRTCChange,
};
static int
......
......@@ -791,6 +791,21 @@ int qemuMonitorEmitStop(qemuMonitorPtr mon)
}
int qemuMonitorEmitRTCChange(qemuMonitorPtr mon, long long offset)
{
int ret = -1;
VIR_DEBUG("mon=%p", mon);
qemuMonitorRef(mon);
qemuMonitorUnlock(mon);
if (mon->cb && mon->cb->domainRTCChange)
ret = mon->cb->domainRTCChange(mon, mon->vm, offset);
qemuMonitorLock(mon);
qemuMonitorUnref(mon);
return ret;
}
int qemuMonitorSetCapabilities(qemuMonitorPtr mon)
{
int ret;
......
......@@ -86,6 +86,9 @@ struct _qemuMonitorCallbacks {
virDomainObjPtr vm);
int (*domainStop)(qemuMonitorPtr mon,
virDomainObjPtr vm);
int (*domainRTCChange)(qemuMonitorPtr mon,
virDomainObjPtr vm,
long long offset);
};
......@@ -122,6 +125,7 @@ int qemuMonitorEmitShutdown(qemuMonitorPtr mon);
int qemuMonitorEmitReset(qemuMonitorPtr mon);
int qemuMonitorEmitPowerdown(qemuMonitorPtr mon);
int qemuMonitorEmitStop(qemuMonitorPtr mon);
int qemuMonitorEmitRTCChange(qemuMonitorPtr mon, long long offset);
int qemuMonitorStartCPUs(qemuMonitorPtr mon,
virConnectPtr conn);
......
......@@ -49,6 +49,7 @@ static void qemuMonitorJSONHandleShutdown(qemuMonitorPtr mon, virJSONValuePtr da
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);
struct {
const char *type;
......@@ -58,6 +59,7 @@ struct {
{ "RESET", qemuMonitorJSONHandleReset, },
{ "POWERDOWN", qemuMonitorJSONHandlePowerdown, },
{ "STOP", qemuMonitorJSONHandleStop, },
{ "RTC_CHANGE", qemuMonitorJSONHandleRTCChange, },
};
......@@ -495,6 +497,16 @@ static void qemuMonitorJSONHandleStop(qemuMonitorPtr mon, virJSONValuePtr data A
qemuMonitorEmitStop(mon);
}
static void qemuMonitorJSONHandleRTCChange(qemuMonitorPtr mon, virJSONValuePtr data)
{
long long offset = 0;
if (virJSONValueObjectGetNumberLong(data, "offset", &offset) < 0) {
VIR_WARN0("missing offset in RTC change event");
offset = 0;
}
qemuMonitorEmitRTCChange(mon, offset);
}
int
qemuMonitorJSONSetCapabilities(qemuMonitorPtr mon)
......
......@@ -6940,6 +6940,33 @@ remoteDomainReadEventReboot(virConnectPtr conn, XDR *xdr)
}
static virDomainEventPtr
remoteDomainReadEventRTCChange(virConnectPtr conn, XDR *xdr)
{
remote_domain_event_rtc_change_msg msg;
virDomainPtr dom;
virDomainEventPtr event = NULL;
memset (&msg, 0, sizeof msg);
/* unmarshall parameters, and process it*/
if (! xdr_remote_domain_event_rtc_change_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 = virDomainEventRTCChangeNewFromDom(dom, msg.offset);
xdr_free ((xdrproc_t) &xdr_remote_domain_event_rtc_change_msg, (char *) &msg);
virDomainFree(dom);
return event;
}
static virDrvOpenStatus ATTRIBUTE_NONNULL (1)
remoteSecretOpen (virConnectPtr conn,
virConnectAuthPtr auth,
......@@ -8487,6 +8514,10 @@ processCallDispatchMessage(virConnectPtr conn, struct private_data *priv,
event = remoteDomainReadEventReboot(conn, xdr);
break;
case REMOTE_PROC_DOMAIN_EVENT_RTC_CHANGE:
event = remoteDomainReadEventRTCChange(conn, xdr);
break;
default:
DEBUG("Unexpected event proc %d", hdr->proc);
break;
......
......@@ -3059,6 +3059,17 @@ xdr_remote_domain_event_reboot_msg (XDR *xdrs, remote_domain_event_reboot_msg *o
return TRUE;
}
bool_t
xdr_remote_domain_event_rtc_change_msg (XDR *xdrs, remote_domain_event_rtc_change_msg *objp)
{
if (!xdr_remote_nonnull_domain (xdrs, &objp->dom))
return FALSE;
if (!xdr_int64_t (xdrs, &objp->offset))
return FALSE;
return TRUE;
}
bool_t
xdr_remote_procedure (XDR *xdrs, remote_procedure *objp)
{
......
......@@ -1732,6 +1732,12 @@ struct remote_domain_event_reboot_msg {
remote_nonnull_domain dom;
};
typedef struct remote_domain_event_reboot_msg remote_domain_event_reboot_msg;
struct remote_domain_event_rtc_change_msg {
remote_nonnull_domain dom;
int64_t offset;
};
typedef struct remote_domain_event_rtc_change_msg remote_domain_event_rtc_change_msg;
#define REMOTE_PROGRAM 0x20008086
#define REMOTE_PROTOCOL_VERSION 1
......@@ -1905,6 +1911,7 @@ enum remote_procedure {
REMOTE_PROC_DOMAIN_EVENTS_REGISTER_ANY = 167,
REMOTE_PROC_DOMAIN_EVENTS_DEREGISTER_ANY = 168,
REMOTE_PROC_DOMAIN_EVENT_REBOOT = 169,
REMOTE_PROC_DOMAIN_EVENT_RTC_CHANGE = 170,
};
typedef enum remote_procedure remote_procedure;
......@@ -2219,6 +2226,7 @@ extern bool_t xdr_remote_domain_migrate_set_max_downtime_args (XDR *, remote_do
extern bool_t xdr_remote_domain_events_register_any_args (XDR *, remote_domain_events_register_any_args*);
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_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*);
......@@ -2507,6 +2515,7 @@ extern bool_t xdr_remote_domain_migrate_set_max_downtime_args ();
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_procedure ();
extern bool_t xdr_remote_message_type ();
extern bool_t xdr_remote_message_status ();
......
......@@ -1540,6 +1540,11 @@ struct remote_domain_event_reboot_msg {
remote_nonnull_domain dom;
};
struct remote_domain_event_rtc_change_msg {
remote_nonnull_domain dom;
hyper offset;
};
/*----- Protocol. -----*/
/* Define the program number, protocol version and procedure numbers here. */
......@@ -1731,7 +1736,8 @@ enum remote_procedure {
REMOTE_PROC_DOMAIN_MIGRATE_SET_MAX_DOWNTIME = 166,
REMOTE_PROC_DOMAIN_EVENTS_REGISTER_ANY = 167,
REMOTE_PROC_DOMAIN_EVENTS_DEREGISTER_ANY = 168,
REMOTE_PROC_DOMAIN_EVENT_REBOOT = 169
REMOTE_PROC_DOMAIN_EVENT_REBOOT = 169,
REMOTE_PROC_DOMAIN_EVENT_RTC_CHANGE = 170
/*
* Notice how the entries are grouped in sets of 10 ?
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册