From patchwork Tue Aug 1 08:22:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heng Qi X-Patchwork-Id: 13335895 X-Patchwork-Delegate: kuba@kernel.org Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3DB5E1F952 for ; Tue, 1 Aug 2023 08:22:46 +0000 (UTC) Received: from out30-98.freemail.mail.aliyun.com (out30-98.freemail.mail.aliyun.com [115.124.30.98]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A11CA0 for ; Tue, 1 Aug 2023 01:22:44 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R131e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046059;MF=hengqi@linux.alibaba.com;NM=1;PH=DS;RN=13;SR=0;TI=SMTPD_---0VoopI6q_1690878160; Received: from localhost(mailfrom:hengqi@linux.alibaba.com fp:SMTPD_---0VoopI6q_1690878160) by smtp.aliyun-inc.com; Tue, 01 Aug 2023 16:22:41 +0800 From: Heng Qi To: "Michael S. Tsirkin" , Jason Wang , netdev@vger.kernel.org, virtualization@lists.linux-foundation.org Cc: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend , Xuan Zhuo Subject: [RFC PATCH 4/6] virtio-net: support rx netdim Date: Tue, 1 Aug 2023 16:22:33 +0800 Message-Id: <20230801082235.21634-5-hengqi@linux.alibaba.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20230801082235.21634-1-hengqi@linux.alibaba.com> References: <20230801082235.21634-1-hengqi@linux.alibaba.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC By comparing the traffic information in the complete napi processes, let the virtio-net driver automatically adjust the coalescing moderation parameters of each receive queue. Signed-off-by: Heng Qi --- drivers/net/virtio_net.c | 97 ++++++++++++++++++++++++++++++++++------ 1 file changed, 84 insertions(+), 13 deletions(-) diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c index 91b284567c96..069e68a52598 100644 --- a/drivers/net/virtio_net.c +++ b/drivers/net/virtio_net.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -163,8 +164,14 @@ struct receive_queue { struct virtnet_rq_stats stats; + /* The number of rx notifications */ + u16 calls; + struct virtnet_interrupt_coalesce intr_coal; + /* Dynamic Iterrupt Moderation */ + struct dim dim; + /* Chain pages by the private ptr. */ struct page *pages; @@ -290,6 +297,9 @@ struct virtnet_info { u8 duplex; u32 speed; + /* Is dynamic interrupt moderation enabled? */ + bool rx_dim_enabled; + /* Interrupt coalescing settings */ struct virtnet_interrupt_coalesce intr_coal_tx; struct virtnet_interrupt_coalesce intr_coal_rx; @@ -1821,6 +1831,7 @@ static void skb_recv_done(struct virtqueue *rvq) struct virtnet_info *vi = rvq->vdev->priv; struct receive_queue *rq = &vi->rq[vq2rxq(rvq)]; + rq->calls++; virtqueue_napi_schedule(&rq->napi, rvq); } @@ -1958,6 +1969,21 @@ static void virtnet_poll_cleantx(struct receive_queue *rq) } } +static void virtnet_rx_dim_work(struct work_struct *work); + +static void virtnet_rx_dim_update(struct virtnet_info *vi, struct receive_queue *rq) +{ + struct virtnet_rq_stats *stats = &rq->stats; + struct dim_sample cur_sample = {}; + + u64_stats_update_begin(&rq->stats.syncp); + dim_update_sample(rq->calls, stats->packets, + stats->bytes, &cur_sample); + u64_stats_update_end(&rq->stats.syncp); + + net_dim(&rq->dim, cur_sample); +} + static int virtnet_poll(struct napi_struct *napi, int budget) { struct receive_queue *rq = @@ -1966,6 +1992,7 @@ static int virtnet_poll(struct napi_struct *napi, int budget) struct send_queue *sq; unsigned int received; unsigned int xdp_xmit = 0; + bool napi_complete; virtnet_poll_cleantx(rq); @@ -1975,8 +2002,11 @@ static int virtnet_poll(struct napi_struct *napi, int budget) xdp_do_flush(); /* Out of packets? */ - if (received < budget) - virtqueue_napi_complete(napi, rq->vq, received); + if (received < budget) { + napi_complete = virtqueue_napi_complete(napi, rq->vq, received); + if (napi_complete && vi->rx_dim_enabled) + virtnet_rx_dim_update(vi, rq); + } if (xdp_xmit & VIRTIO_XDP_TX) { sq = virtnet_xdp_get_sq(vi); @@ -1996,6 +2026,7 @@ static void virtnet_disable_queue_pair(struct virtnet_info *vi, int qp_index) virtnet_napi_tx_disable(&vi->sq[qp_index].napi); napi_disable(&vi->rq[qp_index].napi); xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq); + cancel_work_sync(&vi->rq[qp_index].dim.work); } static int virtnet_enable_queue_pair(struct virtnet_info *vi, int qp_index) @@ -2013,6 +2044,9 @@ static int virtnet_enable_queue_pair(struct virtnet_info *vi, int qp_index) if (err < 0) goto err_xdp_reg_mem_model; + INIT_WORK(&vi->rq[qp_index].dim.work, virtnet_rx_dim_work); + vi->rq[qp_index].dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE; + virtnet_napi_enable(vi->rq[qp_index].vq, &vi->rq[qp_index].napi); virtnet_napi_tx_enable(vi, vi->sq[qp_index].vq, &vi->sq[qp_index].napi); @@ -3077,20 +3111,33 @@ static int virtnet_send_tx_notf_coal_cmds(struct virtnet_info *vi, static int virtnet_send_rx_notf_coal_cmds(struct virtnet_info *vi, struct ethtool_coalesce *ec) { + bool rx_ctrl_dim_on = !!ec->use_adaptive_rx_coalesce; struct scatterlist sgs_rx; - vi->ctrl->coal_rx.rx_usecs = cpu_to_le32(ec->rx_coalesce_usecs); - vi->ctrl->coal_rx.rx_max_packets = cpu_to_le32(ec->rx_max_coalesced_frames); - sg_init_one(&sgs_rx, &vi->ctrl->coal_rx, sizeof(vi->ctrl->coal_rx)); - - if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL, - VIRTIO_NET_CTRL_NOTF_COAL_RX_SET, - &sgs_rx)) + if (rx_ctrl_dim_on && (ec->rx_coalesce_usecs != vi->intr_coal_rx.max_usecs || + ec->rx_max_coalesced_frames != vi->intr_coal_rx.max_packets)) return -EINVAL; - /* Save parameters */ - vi->intr_coal_rx.max_usecs = ec->rx_coalesce_usecs; - vi->intr_coal_rx.max_packets = ec->rx_max_coalesced_frames; + if (rx_ctrl_dim_on) { + if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) + vi->rx_dim_enabled = true; + else + return -EOPNOTSUPP; + } else if (!rx_ctrl_dim_on) { + vi->rx_dim_enabled = false; + vi->ctrl->coal_rx.rx_usecs = cpu_to_le32(ec->rx_coalesce_usecs); + vi->ctrl->coal_rx.rx_max_packets = cpu_to_le32(ec->rx_max_coalesced_frames); + sg_init_one(&sgs_rx, &vi->ctrl->coal_rx, sizeof(vi->ctrl->coal_rx)); + + if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL, + VIRTIO_NET_CTRL_NOTF_COAL_RX_SET, + &sgs_rx)) + return -EINVAL; + + /* Save parameters */ + vi->intr_coal_rx.max_usecs = ec->rx_coalesce_usecs; + vi->intr_coal_rx.max_packets = ec->rx_max_coalesced_frames; + } return 0; } @@ -3172,6 +3219,9 @@ static int virtnet_send_notf_coal_vq_cmds(struct virtnet_info *vi, int err; if (ec->rx_coalesce_usecs || ec->rx_max_coalesced_frames) { + if (vi->rx_dim_enabled) + return -EBUSY; + err = virtnet_send_rx_notf_coal_vq_cmd(vi, queue, ec->rx_coalesce_usecs, ec->rx_max_coalesced_frames); @@ -3190,6 +3240,27 @@ static int virtnet_send_notf_coal_vq_cmds(struct virtnet_info *vi, return 0; } +static void virtnet_rx_dim_work(struct work_struct *work) +{ + struct dim *dim = container_of(work, struct dim, work); + struct receive_queue *rq = container_of(dim, + struct receive_queue, dim); + struct virtnet_info *vi = rq->vq->vdev->priv; + struct net_device *dev = vi->dev; + struct dim_cq_moder update_moder; + int qnum = rq - vi->rq, err; + + update_moder = net_dim_get_rx_moderation(dim->mode, dim->profile_ix); + err = virtnet_send_rx_notf_coal_vq_cmd(vi, qnum, + update_moder.usec, + update_moder.pkts); + if (err) + pr_debug("%s: Failed to send dim parameters on rxq%d\n", + dev->name, (int)(rq - vi->rq)); + + dim->state = DIM_START_MEASURE; +} + static int virtnet_coal_params_supported(struct ethtool_coalesce *ec) { /* usecs coalescing is supported only if VIRTIO_NET_F_NOTF_COAL @@ -3453,7 +3524,7 @@ static int virtnet_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info) static const struct ethtool_ops virtnet_ethtool_ops = { .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES | - ETHTOOL_COALESCE_USECS, + ETHTOOL_COALESCE_USECS | ETHTOOL_COALESCE_USE_ADAPTIVE_RX, .get_drvinfo = virtnet_get_drvinfo, .get_link = ethtool_op_get_link, .get_ringparam = virtnet_get_ringparam,