From nobody Tue Jan 21 07:30:21 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 1737133945062370.41460407163606; Fri, 17 Jan 2025 09:12:25 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 996) id 60C9919AC; Fri, 17 Jan 2025 12:12:24 -0500 (EST) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 986541B1B; Fri, 17 Jan 2025 12:11:18 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 996) id C844118E8; Fri, 17 Jan 2025 12:11:13 -0500 (EST) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lists.libvirt.org (Postfix) with ESMTP id B525418E8 for ; Fri, 17 Jan 2025 12:11:12 -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 0BE3420591B8; 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 0BE3420591B8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1737133871; bh=fnmoCPKpVeLNEObp1PsjItWoy5mesZFSjUKeXqsGhz0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hFB/VWacYuS1s+6aSLO56HfCuj/pg5JJ5oELWQLMn4usgS5LvUFt/V1YPBmswza5f 3sZ/GzY38IZ6FPbOVX3E1aqb4hPFoIqK05Kdr8VIPFmn9wba+ncJ2bIighQznYO4zG OpMdfm3eBEcbPcAekmMwy3D7CRjUErCOEaGSgCug= From: Purna Pavan Chandra Aekkaladevi To: devel@lists.libvirt.org Subject: [PATCH v5 2/5] ch: start a new thread for handling ch events Date: Fri, 17 Jan 2025 17:11:06 +0000 Message-Id: <20250117171109.639464-3-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: VZFB6BF2P5VJ7C6OS3TDL3Z6OOUX3WCO X-Message-ID-Hash: VZFB6BF2P5VJ7C6OS3TDL3Z6OOUX3WCO 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: 1737133947420019000 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 --- src/ch/ch_events.c | 78 +++++++++++++++++++++++++++++++++++++++++++++ src/ch/ch_events.h | 26 +++++++++++++++ src/ch/ch_monitor.c | 60 +++++++++++++++++++++++++++++++--- src/ch/ch_monitor.h | 4 +++ src/ch/meson.build | 2 ++ 5 files changed, 166 insertions(+), 4 deletions(-) create mode 100644 src/ch/ch_events.c create mode 100644 src/ch/ch_events.h diff --git a/src/ch/ch_events.c b/src/ch/ch_events.c new file mode 100644 index 0000000000..b6bbce2db0 --- /dev/null +++ b/src/ch/ch_events.c @@ -0,0 +1,78 @@ +/* + * 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 void virCHEventHandlerLoop(void *data) +{ + virCHMonitor *mon =3D data; + virDomainObj *vm =3D NULL; + + /* Obtain a vm reference */ + vm =3D virObjectRef(mon->vm); + + VIR_DEBUG("%s: Event handler loop thread starting", vm->def->name); + + 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 */ + } + + 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 a72ee40aa1..bedcde2dde 100644 --- a/src/ch/ch_monitor.c +++ b/src/ch/ch_monitor.c @@ -20,12 +20,14 @@ =20 #include =20 +#include #include #include #include =20 #include "datatypes.h" #include "ch_conf.h" +#include "ch_events.h" #include "ch_interface.h" #include "ch_monitor.h" #include "domain_interface.h" @@ -542,6 +544,7 @@ virCHMonitorNew(virDomainObj *vm, virCHDriverConfig *cf= g, int logfile) g_autoptr(virCHMonitor) mon =3D NULL; g_autoptr(virCommand) cmd =3D NULL; int socket_fd =3D 0; + int event_monitor_fd; =20 if (virCHMonitorInitialize() < 0) return NULL; @@ -549,6 +552,9 @@ virCHMonitorNew(virDomainObj *vm, virCHDriverConfig *cf= g, int logfile) if (!(mon =3D virObjectLockableNew(virCHMonitorClass))) return NULL; =20 + /* avoid VIR_FORCE_CLOSE()-ing garbage fd value in virCHMonitorClose */ + mon->eventmonitorfd =3D -1; + if (!vm->def) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("VM is not defined")); @@ -557,8 +563,6 @@ virCHMonitorNew(virDomainObj *vm, virCHDriverConfig *cf= g, int logfile) =20 /* prepare to launch Cloud-Hypervisor socket */ mon->socketpath =3D g_strdup_printf("%s/%s-socket", cfg->stateDir, vm-= >def->name); - mon->eventmonitorpath =3D g_strdup_printf("%s/%s-event-monitor", - cfg->stateDir, vm->def->name); if (g_mkdir_with_parents(cfg->stateDir, 0777) < 0) { virReportSystemError(errno, _("Cannot create socket directory '%1$s'"), @@ -573,6 +577,27 @@ virCHMonitorNew(virDomainObj *vm, virCHDriverConfig *c= fg, int logfile) 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); virCommandSetOutputFD(cmd, &logfile); virCommandSetErrorFD(cmd, &logfile); @@ -597,12 +622,35 @@ virCHMonitorNew(virDomainObj *vm, virCHDriverConfig *= cfg, int logfile) if (virCommandRunAsync(cmd, &mon->pid) < 0) return NULL; =20 - /* get a curl handle */ - mon->handle =3D curl_easy_init(); + /* open the reader end of fifo before start Event Handler */ + 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); + /* CH process(Writer) is blocked at this point as EventHandler(Rea= der) + * fails to open the FIFO. + */ + return NULL; + } + mon->eventmonitorfd =3D event_monitor_fd; + VIR_DEBUG("%s: Opened the event monitor FIFO(%s)", vm->def->name, mon-= >eventmonitorpath); =20 /* 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 @@ -638,6 +686,10 @@ void virCHMonitorClose(virCHMonitor *mon) g_clear_pointer(&mon->socketpath, g_free); } =20 + virCHStopEventHandler(mon); + if (mon->eventmonitorfd >=3D 0) { + VIR_FORCE_CLOSE(mon->eventmonitorfd); + } 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 67dc54f9f4..b9092f22b8 100644 --- a/src/ch/ch_monitor.h +++ b/src/ch/ch_monitor.h @@ -97,6 +97,10 @@ struct _virCHMonitor { char *socketpath; =20 char *eventmonitorpath; + int eventmonitorfd; + + virThread event_handler_thread; + int event_handler_stop; =20 pid_t pid; =20 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