From nobody Sun May 19 10:01:26 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail header.i=@intel.com; spf=pass (zohomail.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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1640313596252184.17115816882153; Thu, 23 Dec 2021 18:39:56 -0800 (PST) Received: from localhost ([::1]:47510 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n0aUs-0000mL-My for importer@patchew.org; Thu, 23 Dec 2021 21:39:54 -0500 Received: from eggs.gnu.org ([209.51.188.92]:47374) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n0aTc-0008WO-ND for qemu-devel@nongnu.org; Thu, 23 Dec 2021 21:38:37 -0500 Received: from mga11.intel.com ([192.55.52.93]:15209) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n0aTY-0008BW-Kr for qemu-devel@nongnu.org; Thu, 23 Dec 2021 21:38:36 -0500 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Dec 2021 18:38:18 -0800 Received: from unknown (HELO leirao-pc.bj.intel.com) ([10.238.156.139]) by orsmga001.jf.intel.com with ESMTP; 23 Dec 2021 18:38:16 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1640313512; x=1671849512; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=kctzOXuZTZx+AIlbQ1aUqK+99mE7TH7tNpJJt0X7ZSA=; b=nHj9HYwxR/HP23tCMtZD85gS0gd2LpuNRDkB2AQT7RpHsAlr0WJPxdWs bkCq1mggZc/rE10Myt0X7V8kKRt/oHacHi6RdISB2XBQE6TLfykbBrhIr vAsDVcehXFhFV3xggg7T96iiw00wpC2FDXQ1YfqyR5VT2ymGgqT9ARqOM NvYt3JTZDtlCV24Au+RUeu06VnDyV1y1FJ0TnFaBWYNh9v7qwEk6cfSIM j0kWI8avcUG3fVDkGqFklUe52R2c/fNb7xfKCdyOip2xExWjNb5k7iCy/ AlTP1261f/pARGrJ4MuwCG9i98Q+yf5xoMusWRpa4WqS7ZvbzULYchVGy w==; X-IronPort-AV: E=McAfee;i="6200,9189,10207"; a="238452450" X-IronPort-AV: E=Sophos;i="5.88,231,1635231600"; d="scan'208";a="238452450" X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.88,231,1635231600"; d="scan'208";a="551359884" From: "Rao, Lei" To: chen.zhang@intel.com, lizhijian@cn.fujitsu.com, jasowang@redhat.com Subject: [PATCH] net/filter: Optimize filter_send to coroutine Date: Fri, 24 Dec 2021 10:37:55 +0800 Message-Id: <20211224023755.532189-1-lei.rao@intel.com> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.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; Received-SPF: pass client-ip=192.55.52.93; envelope-from=lei.rao@intel.com; helo=mga11.intel.com X-Spam_score_int: -72 X-Spam_score: -7.3 X-Spam_bar: ------- X-Spam_report: (-7.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.203, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_HI=-5, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Rao, Lei" , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1640313597412100001 Content-Type: text/plain; charset="utf-8" This patch is to improve the logic of QEMU main thread sleep code in qemu_chr_write_buffer() where it can be blocked and can't run other coroutines during COLO IO stress test. Our approach is to put filter_send() in a coroutine. In this way, filter_send() will call qemu_coroutine_yield() in qemu_co_sleep_ns(), so that it can be scheduled out and QEMU main thread has opportunity to run other tasks. Signed-off-by: Lei Rao Signed-off-by: Zhang Chen Reviewed-by: Li Zhijian --- net/filter-mirror.c | 67 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/net/filter-mirror.c b/net/filter-mirror.c index f20240cc9f..1e9f8b6216 100644 --- a/net/filter-mirror.c +++ b/net/filter-mirror.c @@ -20,6 +20,7 @@ #include "chardev/char-fe.h" #include "qemu/iov.h" #include "qemu/sockets.h" +#include "block/aio-wait.h" =20 #define TYPE_FILTER_MIRROR "filter-mirror" typedef struct MirrorState MirrorState; @@ -42,20 +43,21 @@ struct MirrorState { bool vnet_hdr; }; =20 -static int filter_send(MirrorState *s, - const struct iovec *iov, - int iovcnt) +typedef struct FilterSendCo { + MirrorState *s; + char *buf; + ssize_t size; + bool done; + int ret; +} FilterSendCo; + +static int _filter_send(MirrorState *s, + char *buf, + ssize_t size) { NetFilterState *nf =3D NETFILTER(s); int ret =3D 0; - ssize_t size =3D 0; uint32_t len =3D 0; - char *buf; - - size =3D iov_size(iov, iovcnt); - if (!size) { - return 0; - } =20 len =3D htonl(size); ret =3D qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)&len, sizeof(len= )); @@ -80,10 +82,7 @@ static int filter_send(MirrorState *s, } } =20 - buf =3D g_malloc(size); - iov_to_buf(iov, iovcnt, 0, buf, size); ret =3D qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)buf, size); - g_free(buf); if (ret !=3D size) { goto err; } @@ -94,6 +93,48 @@ err: return ret < 0 ? ret : -EIO; } =20 +static void coroutine_fn filter_send_co(void *opaque) +{ + FilterSendCo *data =3D opaque; + + data->ret =3D _filter_send(data->s, data->buf, data->size); + data->done =3D true; + g_free(data->buf); + aio_wait_kick(); +} + +static int filter_send(MirrorState *s, + const struct iovec *iov, + int iovcnt) +{ + ssize_t size =3D iov_size(iov, iovcnt); + char *buf =3D NULL; + + if (!size) { + return 0; + } + + buf =3D g_malloc(size); + iov_to_buf(iov, iovcnt, 0, buf, size); + + FilterSendCo data =3D { + .s =3D s, + .size =3D size, + .buf =3D buf, + .ret =3D 0, + }; + + Coroutine *co =3D qemu_coroutine_create(filter_send_co, &data); + qemu_coroutine_enter(co); + + while (!data.done) { + aio_poll(qemu_get_aio_context(), true); + } + + return data.ret; + +} + static void redirector_to_filter(NetFilterState *nf, const uint8_t *buf, int len) --=20 2.32.0