From nobody Mon Feb 9 02:51:17 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1571225867; cv=none; d=zoho.com; s=zohoarc; b=ZBBiMDp81ISNhoyztQ1Ny6Zo7TqJK7ME5DVg5Zy3+84TRzHsKR17vuaWb2k9Wc5UQCKt0Xy18uJGnW8rW2hFW7aUXKBqRpxHYTNSatyaPtp6KNFQX4YmItRC6b95ze0tUwyLpO/g14pEA2p3of/8IdmZaVUtcBZuH2JlASLV6Dk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571225867; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=H4UwENGg/xDRcIGXfn/1STKjacb9dOwnaYuxWknrEvw=; b=FZ6skwxLC7vgcCxJEVXsgGgOKJpIkvLpN/bv9a+8dlGwsgVN8k1tHcrXPd39Udrmo+MsMWGyfhZJQcobkZioEatyRjdLcMmFC+wLlQHguFjBqvUeRC65tj+VS5VRjv6ZWLLofLq8xJHyyRJbi+yfwtUtBpJnHNoakHjOTOPWkEE= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1571225867496616.3892264894062; Wed, 16 Oct 2019 04:37:47 -0700 (PDT) Received: from localhost ([::1]:41310 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iKhce-0000K3-6s for importer@patchew.org; Wed, 16 Oct 2019 07:37:44 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:42671) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iKhUA-00010n-Lb for qemu-devel@nongnu.org; Wed, 16 Oct 2019 07:29:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iKhU9-0005P6-A0 for qemu-devel@nongnu.org; Wed, 16 Oct 2019 07:28:58 -0400 Received: from mga07.intel.com ([134.134.136.100]:55307) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iKhU9-0005Ng-1A for qemu-devel@nongnu.org; Wed, 16 Oct 2019 07:28:57 -0400 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 16 Oct 2019 04:28:52 -0700 Received: from unknown (HELO localhost.localdomain) ([10.239.13.19]) by fmsmga001.fm.intel.com with ESMTP; 16 Oct 2019 04:28:50 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.67,303,1566889200"; d="scan'208";a="208393375" From: Zhang Chen To: Jason Wang , Paolo Bonzini , qemu-dev Subject: [RFC PATCH 3/4] net/awd.c: Load advanced watch dog worker thread job Date: Wed, 16 Oct 2019 19:22:08 +0800 Message-Id: <20191016112209.9024-4-chen.zhang@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191016112209.9024-1-chen.zhang@intel.com> References: <20191016112209.9024-1-chen.zhang@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 134.134.136.100 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Zhang Chen , Zhang Chen Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Zhang Chen This patch load pulse_timer and timeout_timer in the new iothread. The pulse timer will send pulse info to awd_node, and the timeout timer will check the reply pulse from awd_node. If timeout occur, it will send opt_script's data to the notification_node. Signed-off-by: Zhang Chen --- net/awd.c | 188 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) diff --git a/net/awd.c b/net/awd.c index ad3d39c982..ea7f628437 100644 --- a/net/awd.c +++ b/net/awd.c @@ -40,17 +40,137 @@ typedef struct AwdState { char *awd_node; char *notification_node; char *opt_script; + char *opt_script_data; uint32_t pulse_interval; uint32_t timeout; CharBackend chr_awd_node; CharBackend chr_notification_node; + SocketReadState awd_rs; + + QEMUTimer *pulse_timer; + QEMUTimer *timeout_timer; IOThread *iothread; + GMainContext *worker_context; } AwdState; =20 typedef struct AwdClass { ObjectClass parent_class; } AwdClass; =20 +static int awd_chr_send(AwdState *s, + const uint8_t *buf, + uint32_t size) +{ + int ret =3D 0; + uint32_t len =3D htonl(size); + + if (!size) { + return 0; + } + + ret =3D qemu_chr_fe_write_all(&s->chr_awd_node, (uint8_t *)&len, + sizeof(len)); + if (ret !=3D sizeof(len)) { + goto err; + } + + ret =3D qemu_chr_fe_write_all(&s->chr_awd_node, (uint8_t *)buf, + size); + if (ret !=3D size) { + goto err; + } + + return 0; + +err: + return ret < 0 ? ret : -EIO; +} + +static int awd_chr_can_read(void *opaque) +{ + return AWD_READ_LEN_MAX; +} + +static void awd_node_in(void *opaque, const uint8_t *buf, int size) +{ + AwdState *s =3D AWD(opaque); + int ret; + + ret =3D net_fill_rstate(&s->awd_rs, buf, size); + if (ret =3D=3D -1) { + qemu_chr_fe_set_handlers(&s->chr_awd_node, NULL, NULL, NULL, NULL, + NULL, NULL, true); + error_report("advanced-watchdog get pulse error"); + } +} + +static void awd_send_pulse(void *opaque) +{ + AwdState *s =3D opaque; + char buf[] =3D "advanced-watchdog pulse"; + + awd_chr_send(s, (uint8_t *)buf, sizeof(buf)); +} + +static void awd_regular_pulse(void *opaque) +{ + AwdState *s =3D opaque; + + awd_send_pulse(s); + timer_mod(s->pulse_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + + s->pulse_interval); +} + +static void awd_timeout(void *opaque) +{ + AwdState *s =3D opaque; + int ret =3D 0; + + ret =3D qemu_chr_fe_write_all(&s->chr_notification_node, + (uint8_t *)s->opt_script_data, + strlen(s->opt_script_data)); + if (ret) { + error_report("advanced-watchdog notification failure"); + } +} + +static void awd_timer_init(AwdState *s) +{ + AioContext *ctx =3D iothread_get_aio_context(s->iothread); + + s->timeout_timer =3D aio_timer_new(ctx, QEMU_CLOCK_VIRTUAL, SCALE_MS, + awd_timeout, s); + + s->pulse_timer =3D aio_timer_new(ctx, QEMU_CLOCK_VIRTUAL, SCALE_MS, + awd_regular_pulse, s); + + if (!s->pulse_interval) { + s->pulse_interval =3D AWD_PULSE_INTERVAL_DEFAULT; + } + + if (!s->timeout) { + s->timeout =3D AWD_TIMEOUT_DEFAULT; + } + + timer_mod(s->pulse_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + + s->pulse_interval); +} + +static void awd_timer_del(AwdState *s) +{ + if (s->pulse_timer) { + timer_del(s->pulse_timer); + timer_free(s->pulse_timer); + s->pulse_timer =3D NULL; + } + + if (s->timeout_timer) { + timer_del(s->timeout_timer); + timer_free(s->timeout_timer); + s->timeout_timer =3D NULL; + } + } + static char *awd_get_node(Object *obj, Error **errp) { AwdState *s =3D AWD(obj); @@ -177,6 +297,22 @@ out: error_propagate(errp, local_err); } =20 +static void awd_rs_finalize(SocketReadState *awd_rs) +{ + AwdState *s =3D container_of(awd_rs, AwdState, awd_rs); + + if (!s->server) { + char buf[] =3D "advanced-watchdog reply pulse"; + + awd_chr_send(s, (uint8_t *)buf, sizeof(buf)); + } + + timer_mod(s->timeout_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + + s->timeout); + + error_report("advanced-watchdog got message : %s", awd_rs->buf); +} + static int find_and_check_chardev(Chardev **chr, char *chr_name, Error **errp) @@ -197,6 +333,41 @@ static int find_and_check_chardev(Chardev **chr, return 0; } =20 +static void awd_iothread(AwdState *s) +{ + object_ref(OBJECT(s->iothread)); + s->worker_context =3D iothread_get_g_main_context(s->iothread); + + qemu_chr_fe_set_handlers(&s->chr_awd_node, awd_chr_can_read, + awd_node_in, NULL, NULL, + s, s->worker_context, true); + + awd_timer_init(s); +} + +static int get_opt_script_data(AwdState *s) +{ + FILE *opt_fd; + long fsize; + + opt_fd =3D fopen(s->opt_script, "r"); + if (opt_fd =3D=3D NULL) { + error_report("advanced-watchdog can't read " + "opt_script: %s", s->opt_script); + return -1; + } + + fseek(opt_fd, 0, SEEK_END); + fsize =3D ftell(opt_fd); + fseek(opt_fd, 0, SEEK_SET); + s->opt_script_data =3D malloc(fsize + 1); + fread(s->opt_script_data, 1, fsize, opt_fd); + + fclose(opt_fd); + + return 0; +} + static void awd_complete(UserCreatable *uc, Error **errp) { AwdState *s =3D AWD(uc); @@ -224,6 +395,16 @@ static void awd_complete(UserCreatable *uc, Error **er= rp) return; } =20 + if (get_opt_script_data(s)) { + error_setg(errp, "advanced-watchdog can't get " + "opt script data: %s", s->opt_script); + return; + } + + net_socket_rs_init(&s->awd_rs, awd_rs_finalize, false); + + awd_iothread(s); + return; } =20 @@ -272,6 +453,13 @@ static void awd_finalize(Object *obj) { AwdState *s =3D AWD(obj); =20 + qemu_chr_fe_deinit(&s->chr_awd_node, false); + qemu_chr_fe_deinit(&s->chr_notification_node, false); + + if (s->iothread) { + awd_timer_del(s); + } + g_free(s->awd_node); g_free(s->notification_node); } --=20 2.17.1