From nobody Sun Dec 22 09:29:13 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 1733132906118415.36265751477924; Mon, 2 Dec 2024 01:48:26 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 996) id F06E51600; Mon, 2 Dec 2024 04:48:24 -0500 (EST) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id B3DBF1826; Mon, 2 Dec 2024 04:46:21 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 996) id 6167B1364; Mon, 2 Dec 2024 04:46:11 -0500 (EST) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lists.libvirt.org (Postfix) with ESMTP id 350E61806 for ; Mon, 2 Dec 2024 04:46:06 -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 C949E2064AE8; 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 C949E2064AE8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1733132765; bh=bnYEUrxuvkxELh1lOta9SeQ0s7J44A936L1depdtD0g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=W+SsoXzWTJjqPCwDQXck2GABMWdCaa2h9XjfPa454L2Ahmx00/3jhPsCKpZKdoUby 0m4f5WatpSNXjYPe/xRm3PgKypvNiFj1c6KLsyWMuhbP1JiZ1mqt7HzyZT/EPKhW+X WD/hoUMBFWs4Bq0G9+XNg/XIlZi+c2dEJKokHhQs= From: Purna Pavan Chandra Aekkaladevi To: devel@lists.libvirt.org Subject: [PATCH v4 3/5] ch: events: Read and parse cloud-hypervisor events Date: Mon, 2 Dec 2024 09:46:00 +0000 Message-Id: <20241202094602.288683-4-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: I3RGRTVSHNFQJFOIXBRTUHYEOLDNDSQE X-Message-ID-Hash: I3RGRTVSHNFQJFOIXBRTUHYEOLDNDSQE 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: 1733132907549116600 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 | 137 +++++++++++++++++++++++++++++++++++++++++++- src/ch/ch_events.h | 2 + src/ch/ch_monitor.h | 6 ++ 3 files changed, 144 insertions(+), 1 deletion(-) diff --git a/src/ch/ch_events.c b/src/ch/ch_events.c index 247f99cef0..a4c2fc4130 100644 --- a/src/ch/ch_events.c +++ b/src/ch/ch_events.c @@ -45,6 +45,137 @@ static int virCHEventStopProcess(virDomainObj *vm, return 0; } =20 +/** + * virCHProcessEvents: + * @mon: the CH monitor object + * + * Parse the events from the event buffer and process them + * Example event: + * { + * "timestamp": { + * "secs": 0, + * "nanos": 29228206 + * }, + * "source": "vm", + * "event": "booted", + * "properties": null + * } + * + * Returns: 0 on success, -1 on failure + */ +static int virCHProcessEvents(virCHMonitor *mon) +{ + virDomainObj *vm =3D mon->vm; + 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; + + /* 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("%s: Invalid JSON event doc: %s", + vm->def->name, 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) +{ + /* Event json string must always terminate with null char. + * So, reserve one byte for '\0' at the end. + */ + size_t max_sz =3D CH_EVENT_BUFFER_SZ - 1; + 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(_("%1$s: Failed to read ch events!: %2$s"), + vm->def->name, g_strerror(errno)); + virCHEventStopProcess(vm, VIR_DOMAIN_SHUTOFF_FAILED); + virCHStopEventHandler(mon); + return; + } + + sz +=3D ret; + mon->event_buffer.buf_fill_sz =3D sz; + + if (virCHProcessEvents(mon) < 0) + VIR_WARN("%s: Failed to parse and process events", vm->def->na= me); + + 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; @@ -56,6 +187,9 @@ static void virCHEventHandlerLoop(void *data) =20 VIR_DEBUG("%s: Event handler loop thread starting", vm->def->name); =20 + mon->event_buffer.buffer =3D g_malloc_n(sizeof(char), CH_EVENT_BUFFER_= SZ); + mon->event_buffer.buf_fill_sz =3D 0; + while ((event_monitor_fd =3D open(mon->eventmonitorpath, O_RDONLY)) < = 0) { if (errno =3D=3D EINTR) { /* 100 milli seconds */ @@ -74,10 +208,11 @@ static void virCHEventHandlerLoop(void *data) =20 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 */ + virCHReadProcessEvents(mon, event_monitor_fd); } =20 end: + g_clear_pointer(&mon->event_buffer.buffer, g_free); VIR_FORCE_CLOSE(event_monitor_fd); virObjectUnref(vm); VIR_DEBUG("%s: Event handler loop thread exiting", vm->def->name); 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 6d154652fa..a84e348938 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 into buffer */ + size_t buf_fill_sz; + } event_buffer; =20 pid_t pid; =20 --=20 2.34.1