From nobody Sun Feb 8 07:50:35 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 1726751054775137.48176427420321; Thu, 19 Sep 2024 06:04:14 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id A0CB915B6; Thu, 19 Sep 2024 09:04:13 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id F297D15B1; Thu, 19 Sep 2024 09:02:33 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 0184B1375; 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 BB027139F 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 6EBDA20C0A3A; Thu, 19 Sep 2024 06:02:20 -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 6EBDA20C0A3A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1726750940; bh=xtfC5xpPUxeQn0MSGqDyOGpn6rWDGbQgjssfUmQhHeU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bmVlWK/Qrov8iE20B/PNVJxQz4vJuUxh3I8YHvkEgmJvXNBMaLKEQsCRrDSxBpH+G XJ6Ewm4AV6tR2NCckIH2RduzkD0Vggrd47mBnzD7xIdVTmW6j/10yY4np9KIJQ2WTI eW4FPQQxjBh0mPcmHLhsPkzW2/cEd8Y88z21IB3c= From: Purna Pavan Chandra Aekkaladevi To: devel@lists.libvirt.org Subject: [PATCH 4/6] ch: events: Read and parse cloud-hypervisor events Date: Thu, 19 Sep 2024 13:02:16 +0000 Message-Id: <20240919130218.738434-5-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: KDO5NFLAFCFGU4U3B6ENYT3JYVP73CRZ X-Message-ID-Hash: KDO5NFLAFCFGU4U3B6ENYT3JYVP73CRZ 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: 1726751055873116600 Content-Type: text/plain; charset="utf-8" Implement `chReadProcessEvents` and `chProcessEvents` to read events from event monitor FIFO file and parse them accordingly. Signed-off-by: Purna Pavan Chandra Aekkaladevi Co-authored-by: Vineeth Pillai --- src/ch/ch_events.c | 142 +++++++++++++++++++++++++++++++++++++++++++- src/ch/ch_events.h | 2 + src/ch/ch_monitor.h | 6 ++ 3 files changed, 149 insertions(+), 1 deletion(-) diff --git a/src/ch/ch_events.c b/src/ch/ch_events.c index 851fbc9f26..a028f9813e 100644 --- a/src/ch/ch_events.c +++ b/src/ch/ch_events.c @@ -28,6 +28,142 @@ =20 VIR_LOG_INIT("ch.ch_events"); =20 +static int virCHProcessEvents(virCHMonitor *mon) +{ + char *buf =3D mon->event_buffer.buffer; + ssize_t sz =3D mon->event_buffer.buf_fill_sz; + virJSONValue *obj =3D NULL; + int blocks =3D 0; + size_t i =3D 0; + char *json_start; + ssize_t start_index =3D -1; + ssize_t end_index =3D -1; + char tmp; + int ret =3D 0; + + while (i < sz) { + if (buf[i] =3D=3D '{') { + blocks++; + if (blocks =3D=3D 1) + start_index =3D i; + } else if (buf[i] =3D=3D '}' && blocks > 0) { + blocks--; + if (blocks =3D=3D 0) { + // valid json document + end_index =3D i; + + /* + * We may hit a corner case where a valid JSON + * doc happens to end right at the end of the buffer. + * virJSONValueFromString needs '\0' end the JSON doc. + * So we need to adjust the buffer accordingly. + */ + if (end_index =3D=3D CH_EVENT_BUFFER_SZ - 1) { + if (start_index =3D=3D 0) { + /* + * We have a valid JSON doc same as the buffer + * size. As per protocol, max JSON doc should be + * less than the buffer size. So this is an error. + * Ignore this JSON doc. + */ + VIR_WARN("Invalid JSON doc size. Expected <=3D %d", + CH_EVENT_BUFFER_SZ); + start_index =3D -1; + ret =3D -1; + break; + } + + /* + * Move the valid JSON doc to the start of the buffer so + * that we can safely fit a '\0' at the end. + */ + memmove(buf, buf+start_index, end_index-start_index+1); + end_index -=3D start_index; + start_index =3D 0; + } + + // temporarily null terminate the JSON doc + tmp =3D buf[end_index + 1]; + buf[end_index + 1] =3D '\0'; + json_start =3D buf + start_index; + + if ((obj =3D virJSONValueFromString(json_start))) { + /* Process the event string (obj) here */ + virJSONValueFree(obj); + } else { + VIR_WARN("Invalid JSON event doc: %s", json_start); + ret =3D -1; + } + + // replace the original character + buf[end_index + 1] =3D tmp; + start_index =3D -1; + } + } + + i++; + } + + if (start_index =3D=3D -1) { + // We have processed all the JSON docs in the buffer. + mon->event_buffer.buf_fill_sz =3D 0; + } else if (start_index > 0) { + // We have an incomplete JSON doc at the end of the buffer. + // Move it to the start of the buffer. + mon->event_buffer.buf_fill_sz =3D sz - start_index; + memmove(buf, buf+start_index, mon->event_buffer.buf_fill_sz); + } + + return ret; +} + +static void virCHReadProcessEvents(virCHMonitor *mon, + int event_monitor_fd) +{ + size_t max_sz =3D CH_EVENT_BUFFER_SZ; + char *buf =3D mon->event_buffer.buffer; + virDomainObj *vm =3D mon->vm; + bool incomplete =3D false; + size_t sz =3D 0; + + memset(buf, 0, max_sz); + do { + ssize_t ret; + + ret =3D read(event_monitor_fd, buf + sz, max_sz - sz); + if (ret =3D=3D 0 || (ret < 0 && errno =3D=3D EINTR)) { + g_usleep(G_USEC_PER_SEC); + continue; + } else if (ret < 0) { + /* + * We should never reach here. read(2) says possible errors + * are EINTR, EAGAIN, EBADF, EFAULT, EINVAL, EIO, EISDIR + * We handle EINTR gracefully. There is some serious issue + * if we encounter any of the other errors(either in our code + * or in the system). Better to bail out. + */ + VIR_ERROR(_("Failed to read ch events!: %1$s"), g_strerror(err= no)); + VIR_FORCE_CLOSE(event_monitor_fd); + abort(); + } + + sz +=3D ret; + mon->event_buffer.buf_fill_sz =3D sz; + + if (virCHProcessEvents(mon) < 0) + VIR_WARN("Failed to parse and process events"); + + if (mon->event_buffer.buf_fill_sz !=3D 0) + incomplete =3D true; + else + incomplete =3D false; + sz =3D mon->event_buffer.buf_fill_sz; + + } while (virDomainObjIsActive(vm) && (sz < max_sz) && incomplete); + + return; +} + static void virCHEventHandlerLoop(void *data) { virCHMonitor *mon =3D data; @@ -51,6 +187,9 @@ static void virCHEventHandlerLoop(void *data) } VIR_DEBUG("Opened the event monitor FIFO(%s)", mon->eventmonitorpath); =20 + mon->event_buffer.buffer =3D g_malloc_n(sizeof(char), CH_EVENT_BUFFER_= SZ); + mon->event_buffer.buf_fill_sz =3D 0; + /* * We would need to wait until VM is initialized. */ @@ -59,9 +198,10 @@ static void virCHEventHandlerLoop(void *data) =20 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 */ + virCHReadProcessEvents(mon, event_monitor_fd); } =20 + g_free(mon->event_buffer.buffer); VIR_FORCE_CLOSE(event_monitor_fd); virObjectUnref(vm); =20 diff --git a/src/ch/ch_events.h b/src/ch/ch_events.h index 4c8a48231d..2e9cdf03bb 100644 --- a/src/ch/ch_events.h +++ b/src/ch/ch_events.h @@ -22,5 +22,7 @@ =20 #include "ch_monitor.h" =20 +#define CH_EVENT_BUFFER_SZ PIPE_BUF + int virCHStartEventHandler(virCHMonitor *mon); void virCHStopEventHandler(virCHMonitor *mon); diff --git a/src/ch/ch_monitor.h b/src/ch/ch_monitor.h index 878a185f29..6b4045d300 100644 --- a/src/ch/ch_monitor.h +++ b/src/ch/ch_monitor.h @@ -99,6 +99,12 @@ struct _virCHMonitor { =20 virThread event_handler_thread; int event_handler_stop; + struct { + // Buffer to hold the data read from pipe + char *buffer; + // Size of the data read from pipe in buffer + size_t buf_fill_sz; + } event_buffer; =20 pid_t pid; =20 --=20 2.34.1