From nobody Sun Feb 8 08:36:14 2026 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 1726751107246353.0381889178634; Thu, 19 Sep 2024 06:05:07 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 25424159D; Thu, 19 Sep 2024 09:05:04 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 8680B15FB; Thu, 19 Sep 2024 09:02:37 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 1C7921374; Thu, 19 Sep 2024 09:02:23 -0400 (EDT) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lists.libvirt.org (Postfix) with ESMTP id B341513AB for ; Thu, 19 Sep 2024 09:02:21 -0400 (EDT) Received: from paekkaladevi-dev-u22.gi4irqh5pfqublruu4yyku2wof.phxx.internal.cloudapp.net (unknown [20.125.125.171]) by linux.microsoft.com (Postfix) with ESMTPSA id 0581720C0A38; Thu, 19 Sep 2024 06:02:19 -0700 (PDT) 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,SPF_HELO_PASS autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 0581720C0A38 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1726750940; bh=ZV952TAprGRcQXW/tnBrfqLmXt1mfEhpSnSgeE2N0vc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YGJgcixYbd/WQp5CPhTVOnwCeoEHPY/Jx2mW7Wx0y5yN73kHT/QuzJ2ruw+uxLEDP LoJ+jCJy/H8uug3Wi2ta4YqoEoBU+8Q4+JqJb4H8feqZArL1x5E8iMN9KFGymeUBow JqnBOyHQOifvPqXQ1AtTLeli6hMF0Q4UA9dtBDLk= From: Purna Pavan Chandra Aekkaladevi To: devel@lists.libvirt.org Subject: [PATCH 3/6] ch: start a new thread for handling ch events Date: Thu, 19 Sep 2024 13:02:15 +0000 Message-Id: <20240919130218.738434-4-paekkaladevi@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240919130218.738434-1-paekkaladevi@linux.microsoft.com> References: <20240919130218.738434-1-paekkaladevi@linux.microsoft.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-ID-Hash: VECCHNZ6VYTPBRBADXKJG3DLP4ARDCVV X-Message-ID-Hash: VECCHNZ6VYTPBRBADXKJG3DLP4ARDCVV 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: paekkaladevi@microsoft.com, prasad.krishnan@microsoft.com, prapal@linux.microsoft.com, liuwe@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: 1726751107871116600 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 | 96 +++++++++++++++++++++++++++++++++++++++++++++ src/ch/ch_events.h | 26 ++++++++++++ src/ch/ch_monitor.c | 28 +++++++++++++ src/ch/ch_monitor.h | 3 ++ src/ch/meson.build | 2 + 6 files changed, 156 insertions(+) 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 1ed4086d2c..d53307cec4 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_interface.c src/ch/ch_monitor.c src/ch/ch_process.c diff --git a/src/ch/ch_events.c b/src/ch/ch_events.c new file mode 100644 index 0000000000..851fbc9f26 --- /dev/null +++ b/src/ch/ch_events.c @@ -0,0 +1,96 @@ +/* + * Copyright Microsoft Corp. 2024 + * + * ch_events.h: 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_events.h" +#include "virfile.h" +#include "virlog.h" + +VIR_LOG_INIT("ch.ch_events"); + +static void virCHEventHandlerLoop(void *data) +{ + virCHMonitor *mon =3D data; + virDomainObj *vm =3D NULL; + int event_monitor_fd; + + VIR_DEBUG("Event handler loop thread starting"); + + while ((event_monitor_fd =3D open(mon->eventmonitorpath, O_RDONLY)) < = 0) { + if (errno =3D=3D EINTR) { + g_usleep(100000); // 100 milli seconds + continue; + } + /* + * Any other error should be a BUG(kernel/libc/libvirtd) + * (ENOMEM can happen on exceeding per-user limits) + */ + VIR_ERROR(_("Failed to open the event monitor FIFO(%1$s) read end!= "), + mon->eventmonitorpath); + abort(); + } + VIR_DEBUG("Opened the event monitor FIFO(%s)", mon->eventmonitorpath); + + /* + * We would need to wait until VM is initialized. + */ + while (!(vm =3D virObjectRef(mon->vm))) + g_usleep(100000); // 100 milli seconds + + while (g_atomic_int_get(&mon->event_handler_stop) =3D=3D 0) { + VIR_DEBUG("Reading events from event monitor file..."); + /* Read and process events here */ + } + + VIR_FORCE_CLOSE(event_monitor_fd); + virObjectUnref(vm); + + VIR_DEBUG("Event handler loop thread exiting"); + return; +} + +int virCHStartEventHandler(virCHMonitor *mon) +{ + g_autofree char *name =3D NULL; + name =3D g_strdup_printf("event-handler-%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 8c99fe1019..5e43bf9ef8 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,28 @@ 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) && + !virFileIsNamedPipe(mon->eventmonitorpath)) { + VIR_WARN("Monitor file (%s) is not a FIFO, 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,6 +616,9 @@ virCHMonitorNew(virDomainObj *vm, virCHDriverConfig *cf= g) if (virCommandRunAsync(cmd, &mon->pid) < 0) return NULL; =20 + if (virCHStartEventHandler(mon) < 0) + return NULL; + /* get a curl handle */ mon->handle =3D curl_easy_init(); =20 @@ -641,6 +667,8 @@ void virCHMonitorClose(virCHMonitor *mon) g_free(mon->eventmonitorpath); } =20 + virCHStopEventHandler(mon); + virObjectUnref(mon); } =20 diff --git a/src/ch/ch_monitor.h b/src/ch/ch_monitor.h index 2ef8706b99..878a185f29 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 633966aac7..684beac1f2 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_driver.h', 'ch_interface.c', 'ch_interface.h', 'ch_monitor.c', --=20 2.34.1