From nobody Sun Dec 22 09:13:21 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 1733132831337721.844484527364; Mon, 2 Dec 2024 01:47:11 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 996) id F03C31A07; Mon, 2 Dec 2024 04:47:09 -0500 (EST) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id DF11B1906; Mon, 2 Dec 2024 04:46:13 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 996) id BE7C01810; Mon, 2 Dec 2024 04:46:09 -0500 (EST) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lists.libvirt.org (Postfix) with ESMTP id 778BC17ED for ; Mon, 2 Dec 2024 04:46:05 -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 58B322053073; Mon, 2 Dec 2024 01:46:04 -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 58B322053073 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1733132764; bh=/nKAtjdW2ukqx4Dfh7dBmOEDFJ06RuZmvirsxLHbtNo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pG3jjzOdFj6fgskjiaZ6C/RvqYv8jnYBfbTIQKX+/wB+k0+7IMAa6xE0Nss6akFdP aNic64OAFe4gQ8Svr0Vs45VhCI94OC3CDCrOCZtf19Zc62eMHE906qlLd2iANjh28M QZGktGmI+8KqUVi1MIUTNrjEBNNZuGdv7FIjTQS4= From: Purna Pavan Chandra Aekkaladevi To: devel@lists.libvirt.org Subject: [PATCH v4 2/5] ch: start a new thread for handling ch events Date: Mon, 2 Dec 2024 09:45:59 +0000 Message-Id: <20241202094602.288683-3-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: ZEC66YFJL6ANIU2UGTFNTGNPQ56SDT2K X-Message-ID-Hash: ZEC66YFJL6ANIU2UGTFNTGNPQ56SDT2K 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: 1733132833274116600 Content-Type: text/plain; charset="utf-8" Use a FIFO(named pipe) for --event-monitor option in CH. Introduce a new thread, `virCHEventHandlerLoop`, to continuously monitor and handle events from cloud-hypervisor. Signed-off-by: Purna Pavan Chandra Aekkaladevi Co-authored-by: Vineeth Pillai --- po/POTFILES | 1 + src/ch/ch_events.c | 111 ++++++++++++++++++++++++++++++++++++++++++++ src/ch/ch_events.h | 26 +++++++++++ src/ch/ch_monitor.c | 32 +++++++++++-- src/ch/ch_monitor.h | 3 ++ src/ch/meson.build | 2 + 6 files changed, 172 insertions(+), 3 deletions(-) create mode 100644 src/ch/ch_events.c create mode 100644 src/ch/ch_events.h diff --git a/po/POTFILES b/po/POTFILES index 3514aa3dca..3b07459021 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -21,6 +21,7 @@ src/bhyve/bhyve_process.c src/ch/ch_conf.c src/ch/ch_domain.c src/ch/ch_driver.c +src/ch/ch_events.c src/ch/ch_hostdev.c src/ch/ch_interface.c src/ch/ch_monitor.c diff --git a/src/ch/ch_events.c b/src/ch/ch_events.c new file mode 100644 index 0000000000..247f99cef0 --- /dev/null +++ b/src/ch/ch_events.c @@ -0,0 +1,111 @@ +/* + * Copyright Microsoft Corp. 2024 + * + * ch_events.c: Handle Cloud-Hypervisor events + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#include + +#include + +#include "ch_domain.h" +#include "ch_events.h" +#include "ch_process.h" +#include "virfile.h" +#include "virlog.h" + +VIR_LOG_INIT("ch.ch_events"); + +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 void virCHEventHandlerLoop(void *data) +{ + virCHMonitor *mon =3D data; + virDomainObj *vm =3D NULL; + int event_monitor_fd; + + /* Obtain a vm reference */ + vm =3D virObjectRef(mon->vm); + + VIR_DEBUG("%s: Event handler loop thread starting", vm->def->name); + + while ((event_monitor_fd =3D open(mon->eventmonitorpath, O_RDONLY)) < = 0) { + if (errno =3D=3D EINTR) { + /* 100 milli seconds */ + g_usleep(100000); + continue; + } + /* Any other error should be a BUG(kernel/libc/libvirtd) + * (ENOMEM can happen on exceeding per-user limits) + */ + VIR_ERROR(_("%1$s: Failed to open the event monitor FIFO(%2$s) rea= d end!"), + vm->def->name, mon->eventmonitorpath); + virCHEventStopProcess(vm, VIR_DOMAIN_SHUTOFF_FAILED); + goto end; + } + VIR_DEBUG("%s: Opened the event monitor FIFO(%s)", vm->def->name, mon-= >eventmonitorpath); + + while (g_atomic_int_get(&mon->event_handler_stop) =3D=3D 0) { + VIR_DEBUG("%s: Reading events from event monitor file", vm->def->n= ame); + /* Read and process events here */ + } + + end: + VIR_FORCE_CLOSE(event_monitor_fd); + virObjectUnref(vm); + VIR_DEBUG("%s: Event handler loop thread exiting", vm->def->name); + return; +} + +int virCHStartEventHandler(virCHMonitor *mon) +{ + g_autofree char *name =3D NULL; + name =3D g_strdup_printf("ch-evt-%d", mon->pid); + + virObjectRef(mon); + if (virThreadCreateFull(&mon->event_handler_thread, + false, + virCHEventHandlerLoop, + name, + false, + mon) < 0) { + virObjectUnref(mon); + return -1; + } + virObjectUnref(mon); + + g_atomic_int_set(&mon->event_handler_stop, 0); + return 0; +} + +void virCHStopEventHandler(virCHMonitor *mon) +{ + g_atomic_int_set(&mon->event_handler_stop, 1); +} diff --git a/src/ch/ch_events.h b/src/ch/ch_events.h new file mode 100644 index 0000000000..4c8a48231d --- /dev/null +++ b/src/ch/ch_events.h @@ -0,0 +1,26 @@ +/* + * Copyright Microsoft Corp. 2024 + * + * ch_events.h: header file for handling Cloud-Hypervisor events + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#pragma once + +#include "ch_monitor.h" + +int virCHStartEventHandler(virCHMonitor *mon); +void virCHStopEventHandler(virCHMonitor *mon); diff --git a/src/ch/ch_monitor.c b/src/ch/ch_monitor.c index 59117d72b6..f00a892a09 100644 --- a/src/ch/ch_monitor.c +++ b/src/ch/ch_monitor.c @@ -26,6 +26,7 @@ =20 #include "datatypes.h" #include "ch_conf.h" +#include "ch_events.h" #include "ch_interface.h" #include "ch_monitor.h" #include "domain_interface.h" @@ -572,6 +573,27 @@ virCHMonitorNew(virDomainObj *vm, virCHDriverConfig *c= fg) return NULL; } =20 + /* Event monitor file to listen for VM state changes */ + mon->eventmonitorpath =3D g_strdup_printf("%s/%s-event-monitor-fifo", + cfg->stateDir, vm->def->name); + if (virFileExists(mon->eventmonitorpath)) { + VIR_WARN("Monitor file (%s) already exists, trying to delete!", + mon->eventmonitorpath); + if (virFileRemove(mon->eventmonitorpath, -1, -1) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Failed to remove the file: %1$s"), + mon->eventmonitorpath); + return NULL; + } + } + + if (mkfifo(mon->eventmonitorpath, S_IWUSR | S_IRUSR) < 0 && + errno !=3D EEXIST) { + virReportSystemError(errno, "%s", + _("Cannot create monitor FIFO")); + return NULL; + } + cmd =3D virCommandNew(vm->def->emulator); virCommandSetUmask(cmd, 0x002); socket_fd =3D chMonitorCreateSocket(mon->socketpath); @@ -593,12 +615,15 @@ virCHMonitorNew(virDomainObj *vm, virCHDriverConfig *= cfg) if (virCommandRunAsync(cmd, &mon->pid) < 0) return NULL; =20 - /* get a curl handle */ - mon->handle =3D curl_easy_init(); - /* now has its own reference */ mon->vm =3D virObjectRef(vm); =20 + if (virCHStartEventHandler(mon) < 0) + return NULL; + + /* get a curl handle */ + mon->handle =3D curl_easy_init(); + return g_steal_pointer(&mon); } =20 @@ -634,6 +659,7 @@ void virCHMonitorClose(virCHMonitor *mon) g_clear_pointer(&mon->socketpath, g_free); } =20 + virCHStopEventHandler(mon); if (mon->eventmonitorpath) { if (virFileRemove(mon->eventmonitorpath, -1, -1) < 0) { VIR_WARN("Unable to remove CH event monitor file '%s'", diff --git a/src/ch/ch_monitor.h b/src/ch/ch_monitor.h index 7f72626c2b..6d154652fa 100644 --- a/src/ch/ch_monitor.h +++ b/src/ch/ch_monitor.h @@ -97,6 +97,9 @@ struct _virCHMonitor { =20 char *eventmonitorpath; =20 + virThread event_handler_thread; + int event_handler_stop; + pid_t pid; =20 virDomainObj *vm; diff --git a/src/ch/meson.build b/src/ch/meson.build index ca1291c158..0b4a5aeb49 100644 --- a/src/ch/meson.build +++ b/src/ch/meson.build @@ -7,6 +7,8 @@ ch_driver_sources =3D [ 'ch_domain.h', 'ch_driver.c', 'ch_driver.h', + 'ch_events.c', + 'ch_events.h', 'ch_interface.c', 'ch_interface.h', 'ch_monitor.c', --=20 2.34.1