From nobody Tue Jan 21 07:30:12 2025 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 1737134030142688.236886362677; Fri, 17 Jan 2025 09:13:50 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 996) id 897EA1A91; Fri, 17 Jan 2025 12:13:49 -0500 (EST) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 369861A6C; Fri, 17 Jan 2025 12:11:37 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 996) id 4D82E1B50; Fri, 17 Jan 2025 12:11:32 -0500 (EST) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lists.libvirt.org (Postfix) with ESMTP id 6B62818FF for ; Fri, 17 Jan 2025 12:11:16 -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 EC55620591BC; Fri, 17 Jan 2025 09:11:11 -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=-0.6 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, 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 EC55620591BC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1737133872; bh=z/kQvaSa8QON6Xiow3v5PMzvNZAXP7pdE/MOKmx0vX8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=F/w/B/NzcO0GFVtFJEbD8S6xalLMLfpSky4gDsUr/UIP6okE9z1DNUMhtlMwf867b s/6Dd+kKWoBTEhLUaPL8xoTy3VJ9GtfHIFH4p4565PmmgA7HRj4TvQ46Myc7a7wmFq eY9hx+p4T+u9T0NmKMmU1U/sbYurOengxdnBgfQ8= From: Purna Pavan Chandra Aekkaladevi To: devel@lists.libvirt.org Subject: [PATCH v5 4/5] ch: events: facilitate lifecycle events handling Date: Fri, 17 Jan 2025 17:11:08 +0000 Message-Id: <20250117171109.639464-5-paekkaladevi@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250117171109.639464-1-paekkaladevi@linux.microsoft.com> References: <20250117171109.639464-1-paekkaladevi@linux.microsoft.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-ID-Hash: 4XMP2YNMVAU3YAD2LIOBFL4DYNXF432N X-Message-ID-Hash: 4XMP2YNMVAU3YAD2LIOBFL4DYNXF432N 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: liuwe@microsoft.com, paekkaladevi@microsoft.com, jnadimpalli@microsoft.com, prasad.krishnan@microsoft.com, prapal@linux.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: 1737134031882019000 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 | 101 ++++++++++++++++++++++++++++++++++++++++++++- src/ch/ch_events.h | 26 ++++++++++++ 2 files changed, 126 insertions(+), 1 deletion(-) diff --git a/src/ch/ch_events.c b/src/ch/ch_events.c index c5c703ded7..685ddeaa31 100644 --- a/src/ch/ch_events.c +++ b/src/ch/ch_events.c @@ -28,6 +28,101 @@ =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) +{ + virCHDriver *driver =3D ((virCHDomainObjPrivate *)vm->privateData)->d= river; + + virObjectLock(vm); + if (virDomainObjBeginJob(vm, VIR_JOB_MODIFY)) + return -1; + virCHProcessStop(driver, vm, reason); + virDomainObjEndJob(vm); + virObjectUnlock(vm); + + return 0; +} + +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; +} =20 /** * virCHProcessEvents: @@ -77,7 +172,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_ERROR(_("%1$s: Failed to process JSON event do= c: %2$s"), + vm->def->name, json_start); + return -1; + } virJSONValueFree(obj); } else { VIR_ERROR(_("%1$s: Invalid JSON event doc: %2$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