From patchwork Fri Dec 24 02:37:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Rao, Lei" X-Patchwork-Id: 12698763 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id DF916C433F5 for ; Fri, 24 Dec 2021 02:39:50 +0000 (UTC) Received: from localhost ([::1]:47510 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n0aUn-0000mL-9Q for qemu-devel@archiver.kernel.org; Thu, 23 Dec 2021 21:39:49 -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 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" 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 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.88,231,1635231600"; d="scan'208";a="551359884" 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 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 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" 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" #define TYPE_FILTER_MIRROR "filter-mirror" typedef struct MirrorState MirrorState; @@ -42,20 +43,21 @@ struct MirrorState { bool vnet_hdr; }; -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 = NETFILTER(s); int ret = 0; - ssize_t size = 0; uint32_t len = 0; - char *buf; - - size = iov_size(iov, iovcnt); - if (!size) { - return 0; - } len = htonl(size); ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)&len, sizeof(len)); @@ -80,10 +82,7 @@ static int filter_send(MirrorState *s, } } - buf = g_malloc(size); - iov_to_buf(iov, iovcnt, 0, buf, size); ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)buf, size); - g_free(buf); if (ret != size) { goto err; } @@ -94,6 +93,48 @@ err: return ret < 0 ? ret : -EIO; } +static void coroutine_fn filter_send_co(void *opaque) +{ + FilterSendCo *data = opaque; + + data->ret = _filter_send(data->s, data->buf, data->size); + data->done = true; + g_free(data->buf); + aio_wait_kick(); +} + +static int filter_send(MirrorState *s, + const struct iovec *iov, + int iovcnt) +{ + ssize_t size = iov_size(iov, iovcnt); + char *buf = NULL; + + if (!size) { + return 0; + } + + buf = g_malloc(size); + iov_to_buf(iov, iovcnt, 0, buf, size); + + FilterSendCo data = { + .s = s, + .size = size, + .buf = buf, + .ret = 0, + }; + + Coroutine *co = 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)