From nobody Tue Jan 21 07:33:09 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 1737133905257922.8926516059573; Fri, 17 Jan 2025 09:11:45 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 996) id 9B5711B60; Fri, 17 Jan 2025 12:11:44 -0500 (EST) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 7801F1913; Fri, 17 Jan 2025 12:11:16 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 996) id A88EF190C; 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 DCA8D18E6 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 7C25B20591BB; 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 7C25B20591BB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1737133871; bh=lazX7MXUeUrnkR66Vtz3nEnFVNhO8x4MJiRTOZcFDSw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Et0pDAUTbZmuVWUK6NItgHRfebeKngnO1m/1clMokXSHzFEuB35PhmEeHSoAo689i sI8tLWxSutVV/rNY0UNLd1TcYbyxKgXNOJVRXSCH/UGEwazKabQiVFmM1MDTwGZSAE rUiKlCqrvAgWl0RPt4m5Zm2S20nHB2X7tlvOligo= From: Purna Pavan Chandra Aekkaladevi To: devel@lists.libvirt.org Subject: [PATCH v5 3/5] ch: events: Read and parse cloud-hypervisor events Date: Fri, 17 Jan 2025 17:11:07 +0000 Message-Id: <20250117171109.639464-4-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: JVVPBODVSEZG4EY5SWV5I6TDYAFEYVWM X-Message-ID-Hash: JVVPBODVSEZG4EY5SWV5I6TDYAFEYVWM 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: 1737133906952019000 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 --- po/POTFILES | 1 + src/ch/ch_events.c | 141 +++++++++++++++++++++++++++++++++++++++++++- src/ch/ch_events.h | 2 + src/ch/ch_monitor.h | 6 ++ 4 files changed, 147 insertions(+), 3 deletions(-) diff --git a/po/POTFILES b/po/POTFILES index 484b34c3df..d4b3de781b 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -22,6 +22,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 index b6bbce2db0..c5c703ded7 100644 --- a/src/ch/ch_events.c +++ b/src/ch/ch_events.c @@ -20,8 +20,6 @@ =20 #include =20 -#include - #include "ch_domain.h" #include "ch_events.h" #include "ch_process.h" @@ -31,6 +29,137 @@ VIR_LOG_INIT("ch.ch_events"); =20 =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; + + 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_ERROR(_("%1$s: Invalid JSON event doc: %2$s"), + vm->def->name, json_start); + return -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 0; +} + +static int virCHReadProcessEvents(virCHMonitor *mon) +{ + /* 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; + int event_monitor_fd =3D mon->eventmonitorfd; + + 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). + */ + VIR_ERROR(_("%1$s: Failed to read ch events!: %2$s"), + vm->def->name, g_strerror(errno)); + return -1; + } + + sz +=3D ret; + mon->event_buffer.buf_fill_sz =3D sz; + + if (virCHProcessEvents(mon) < 0) { + VIR_ERROR(_("%1$s: Failed to parse and process events"), + vm->def->name); + return -1; + } + + 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 0; +} + static void virCHEventHandlerLoop(void *data) { virCHMonitor *mon =3D data; @@ -41,11 +170,17 @@ 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 (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 */ + if (virCHReadProcessEvents(mon) < 0) { + virCHStopEventHandler(mon); + } } =20 + g_clear_pointer(&mon->event_buffer.buffer, g_free); virObjectUnref(vm); VIR_DEBUG("%s: Event handler loop thread exiting", vm->def->name); return; 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 b9092f22b8..185de0dbfd 100644 --- a/src/ch/ch_monitor.h +++ b/src/ch/ch_monitor.h @@ -101,6 +101,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