From nobody Sun Dec 22 08:48:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=linux.microsoft.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1733132855188818.8683982362393; Mon, 2 Dec 2024 01:47:35 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 996) id 0BABA197B; Mon, 2 Dec 2024 04:47:33 -0500 (EST) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id B941B1A45; Mon, 2 Dec 2024 04:46:16 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 996) id 5F15C17E3; Mon, 2 Dec 2024 04:46:11 -0500 (EST) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lists.libvirt.org (Postfix) with ESMTP id 8A88C180A for ; Mon, 2 Dec 2024 04:46:06 -0500 (EST) Received: from paekkaladevi-dev-u22.gi4irqh5pfqublruu4yyku2wof.phxx.internal.cloudapp.net (unknown [20.125.125.171]) by linux.microsoft.com (Postfix) with ESMTPSA id 456C12053051; Mon, 2 Dec 2024 01:46:05 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-1.5 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_LOW, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_PASS autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 456C12053051 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1733132765; bh=Wd0q6XEsMU8Q69ff3r8ys+WtBzqEo2QWVZGYJCEYErU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ca2wrhe1gOcorsxkqukeNWvl1zuloMP/lytoy2xhVuvBiN5vvKShk1N4cfS3E1VHk vz7j9dVbnWiS7dD/pCeLXyBWhFSw3LdEnoYrxXMl7ZioFtKDg/+5RYTY0jxHU+hieg ulpy6uib2N+ro596/fYaqvyHa43rBKi2y04QNiaU= From: Purna Pavan Chandra Aekkaladevi To: devel@lists.libvirt.org Subject: [PATCH v4 4/5] ch: events: facilitate lifecycle events handling Date: Mon, 2 Dec 2024 09:46:01 +0000 Message-Id: <20241202094602.288683-5-paekkaladevi@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241202094602.288683-1-paekkaladevi@linux.microsoft.com> References: <20241202094602.288683-1-paekkaladevi@linux.microsoft.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-ID-Hash: JG7RR46Q6M5JXUQVR4MIT6BMMIRKPLUI X-Message-ID-Hash: JG7RR46Q6M5JXUQVR4MIT6BMMIRKPLUI X-MailFrom: paekkaladevi@linux.microsoft.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header CC: prasad.krishnan@microsoft.com, prapal@linux.microsoft.com, liuwe@microsoft.com, jnadimpalli@microsoft.com, paekkaladevi@microsoft.com, Purna Pavan Chandra Aekkaladevi X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-ZohoMail-DKIM: fail (Computed bodyhash is different from the expected one) X-ZM-MESSAGEID: 1733132857350116600 Content-Type: text/plain; charset="utf-8" Implement `virCHProcessEvent` that maps event string to corresponding event type and take appropriate actions. As part of this, handle the shutdown event by correctly updating the domain state. this change also facilitates the handling of other VM lifecycle events, such as booting, rebooting, pause, resume, etc. Signed-off-by: Purna Pavan Chandra Aekkaladevi --- src/ch/ch_events.c | 87 +++++++++++++++++++++++++++++++++++++++++++++- src/ch/ch_events.h | 26 ++++++++++++++ 2 files changed, 112 insertions(+), 1 deletion(-) diff --git a/src/ch/ch_events.c b/src/ch/ch_events.c index a4c2fc4130..45605a0826 100644 --- a/src/ch/ch_events.c +++ b/src/ch/ch_events.c @@ -30,6 +30,26 @@ =20 VIR_LOG_INIT("ch.ch_events"); =20 +VIR_ENUM_IMPL(virCHEvent, + VIR_CH_EVENT_LAST, + "vmm:starting", + "vmm:shutdown", + "vm:booting", + "vm:booted", + "vm:rebooting", + "vm:rebooted", + "vm:shutdown", + "vm:deleted", + "vm:pausing", + "vm:paused", + "vm:resuming", + "vm:resumed", + "vm:snapshotting", + "vm:snapshotted", + "vm:restoring", + "vm:restored", +); + static int virCHEventStopProcess(virDomainObj *vm, virDomainShutoffReason reason) { @@ -45,6 +65,67 @@ static int virCHEventStopProcess(virDomainObj *vm, return 0; } =20 +static int virCHProcessEvent(virCHMonitor *mon, + virJSONValue *eventJSON) +{ + const char *event; + const char *source; + virCHEvent ev; + g_autofree char *timestamp =3D NULL; + g_autofree char *full_event =3D NULL; + virDomainObj *vm =3D mon->vm; + int ret =3D 0; + + if (virJSONValueObjectHasKey(eventJSON, "source") =3D=3D 0) { + VIR_WARN("%s: Invalid JSON from monitor, no source key", vm->def->= name); + return -1; + } + if (virJSONValueObjectHasKey(eventJSON, "event") =3D=3D 0) { + VIR_WARN("%s: Invalid JSON from monitor, no event key", vm->def->n= ame); + return -1; + } + source =3D virJSONValueObjectGetString(eventJSON, "source"); + event =3D virJSONValueObjectGetString(eventJSON, "event"); + full_event =3D g_strdup_printf("%s:%s", source, event); + ev =3D virCHEventTypeFromString(full_event); + VIR_DEBUG("%s: Source: %s, Event: %s, ev: %d", vm->def->name, source, = event, ev); + + switch (ev) { + case VIR_CH_EVENT_VMM_STARTING: + case VIR_CH_EVENT_VM_BOOTING: + case VIR_CH_EVENT_VM_BOOTED: + case VIR_CH_EVENT_VM_REBOOTING: + case VIR_CH_EVENT_VM_REBOOTED: + case VIR_CH_EVENT_VM_PAUSING: + case VIR_CH_EVENT_VM_PAUSED: + case VIR_CH_EVENT_VM_RESUMING: + case VIR_CH_EVENT_VM_RESUMED: + case VIR_CH_EVENT_VM_SNAPSHOTTING: + case VIR_CH_EVENT_VM_SNAPSHOTTED: + case VIR_CH_EVENT_VM_RESTORING: + case VIR_CH_EVENT_VM_RESTORED: + case VIR_CH_EVENT_VM_DELETED: + break; + case VIR_CH_EVENT_VMM_SHUTDOWN: + if (virCHEventStopProcess(vm, VIR_DOMAIN_SHUTOFF_SHUTDOWN)) { + VIR_WARN("Failed to mark the VM(%s) as SHUTDOWN!", + vm->def->name); + ret =3D -1; + } + break; + case VIR_CH_EVENT_VM_SHUTDOWN: + virObjectLock(vm); + virDomainObjSetState(vm, VIR_DOMAIN_SHUTOFF, VIR_DOMAIN_SHUTOF= F_SHUTDOWN); + virObjectUnlock(vm); + break; + case VIR_CH_EVENT_LAST: + default: + VIR_WARN("%s: Unknown event: %s", vm->def->name, full_event); + } + + return ret; +} + /** * virCHProcessEvents: * @mon: the CH monitor object @@ -94,7 +175,11 @@ static int virCHProcessEvents(virCHMonitor *mon) json_start =3D buf + start_index; =20 if ((obj =3D virJSONValueFromString(json_start))) { - /* Process the event string (obj) here */ + if (virCHProcessEvent(mon, obj) < 0) { + VIR_WARN("%s: Failed to process JSON event doc: %s= ", + vm->def->name, json_start); + ret =3D -1; + } virJSONValueFree(obj); } else { VIR_WARN("%s: Invalid JSON event doc: %s", diff --git a/src/ch/ch_events.h b/src/ch/ch_events.h index 2e9cdf03bb..3b360628f7 100644 --- a/src/ch/ch_events.h +++ b/src/ch/ch_events.h @@ -24,5 +24,31 @@ =20 #define CH_EVENT_BUFFER_SZ PIPE_BUF =20 +typedef enum { + /* source: vmm */ + VIR_CH_EVENT_VMM_STARTING =3D 0, + VIR_CH_EVENT_VMM_SHUTDOWN, + + /* source: vm */ + VIR_CH_EVENT_VM_BOOTING, + VIR_CH_EVENT_VM_BOOTED, + VIR_CH_EVENT_VM_REBOOTING, + VIR_CH_EVENT_VM_REBOOTED, + VIR_CH_EVENT_VM_SHUTDOWN, + VIR_CH_EVENT_VM_DELETED, + VIR_CH_EVENT_VM_PAUSING, + VIR_CH_EVENT_VM_PAUSED, + VIR_CH_EVENT_VM_RESUMING, + VIR_CH_EVENT_VM_RESUMED, + VIR_CH_EVENT_VM_SNAPSHOTTING, + VIR_CH_EVENT_VM_SNAPSHOTTED, + VIR_CH_EVENT_VM_RESTORING, + VIR_CH_EVENT_VM_RESTORED, + + VIR_CH_EVENT_LAST +} virCHEvent; + +VIR_ENUM_DECL(virCHEvent); + int virCHStartEventHandler(virCHMonitor *mon); void virCHStopEventHandler(virCHMonitor *mon); --=20 2.34.1