From nobody Sun Jul 26 11:01:58 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1782998276; cv=none; d=zohomail.com; s=zohoarc; b=FVu5aeJZHLMvAA29ITxJgrMV1dqMSBOwPFuxAYDfM+a/e0JXlnCX12JOxgWykcO5wxgprBugRsSxRXEkj9Luea7c8u2ACVKRWnV0BvwlrkUlPjilCp93ZBRH+P7wxAEWAnQ1gxrkKOrdEQdlawtky6VEq9WHXnjKEuD7rqOXbPs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1782998276; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=bOmDtT3Mr9ISOC1mGVQ9xbshhTvrX0+CeB3rP9gccXM=; b=D36wj5+HrVEQLS5pw0tkWCemKK+1WVS4pYQwfpeeABX634ASpJVI6QQzqr3pdS2j6Zd9f6gl+bD6zu8G3A8MXOMMV73H15CT12KMmZRiB/byRakpYo4VanA9IyGPKKhctg14eUkaRgXN0rRMkqUsbE0dEH2YRge34fUO/JRpd4Q= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1782998276807747.4320835953046; Thu, 2 Jul 2026 06:17:56 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wfHI5-0001hF-Oc; Thu, 02 Jul 2026 09:17:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wfHI4-0001fR-1i for qemu-devel@nongnu.org; Thu, 02 Jul 2026 09:17:16 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wfHI2-0008Q0-AB for qemu-devel@nongnu.org; Thu, 02 Jul 2026 09:17:15 -0400 Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-60-NVkqCLPHNZyHYSR0pjDStQ-1; Thu, 02 Jul 2026 09:17:09 -0400 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D714118974FC; Thu, 2 Jul 2026 13:16:42 +0000 (UTC) Received: from kdutta-thinkpadp1gen7.bengluru.csb (unknown [10.74.81.96]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id AAA9C1800845; Thu, 2 Jul 2026 13:16:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1782998233; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=bOmDtT3Mr9ISOC1mGVQ9xbshhTvrX0+CeB3rP9gccXM=; b=D5wSLErg4n+jWqSIH49xS0z4r7abrjVtGVKXYoYYHmo5wa3Wt9tmIGV9nAGyG64Ql711V4 weyX731zg1ClMitiv2AAHxJOO/4H5Ghp76q9ZaACU1Zo//bGXq5xyb9GsV3RzJmwrwUbzI 65Y8pYTQc+xvY14KPOrwJcJmsP+NlGU= X-MC-Unique: NVkqCLPHNZyHYSR0pjDStQ-1 X-Mimecast-MFC-AGG-ID: NVkqCLPHNZyHYSR0pjDStQ_1782998228 From: Koushik Dutta To: qemu-devel@nongnu.org Cc: Zhao Liu , "Michael S. Tsirkin" , Jason Wang , Stefano Garzarella , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Eugenio=20P=C3=A9rez?= Subject: [PATCH v9 1/2] [PATCH 1/2] Introduce virtio_net_handle_tx_dispatch() to unify TX path handling. This dispatcher dynamically selects between timer-based and BH-based TX processing based on configuration. Date: Thu, 2 Jul 2026 18:46:31 +0530 Message-ID: <20260702131632.1180309-2-kdutta@redhat.com> In-Reply-To: <20260702131632.1180309-1-kdutta@redhat.com> References: <20260702131632.1180309-1-kdutta@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 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=lists1p.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=kdutta@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: 8 X-Spam_score: 0.8 X-Spam_bar: / X-Spam_report: (0.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.445, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_SBL_CSS=3.335, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1782998278244158501 Content-Type: text/plain; charset="utf-8" Previously, the tx=3Dtimer selected between two completely separate code paths at queue creation time. This refactoring introduces a runtime dispatch mechanism while maintaining identical behavior. This is a preparatory patch with no functional changes, making it easier to add dynamic TX notification coalescing in a subsequent patch. Signed-off-by: Koushik Dutta --- hw/net/virtio-net.c | 44 ++++++++++++++++++++++------------ include/hw/virtio/virtio-net.h | 1 + 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c index 2a5d642a64..319842cf28 100644 --- a/hw/net/virtio-net.c +++ b/hw/net/virtio-net.c @@ -1002,6 +1002,8 @@ static void virtio_net_set_features(VirtIODevice *vde= v, } } =20 +static void virtio_net_tx_timer(void *opaque); + static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd, struct iovec *iov, unsigned int iov_c= nt) { @@ -2817,7 +2819,6 @@ detach: return -EINVAL; } =20 -static void virtio_net_tx_timer(void *opaque); =20 static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq) { @@ -2973,6 +2974,22 @@ static void virtio_net_tx_bh(void *opaque) } } =20 +static void virtio_net_handle_tx_dispatch(VirtIODevice *vdev, VirtQueue *v= q) +{ + VirtIONet *n =3D VIRTIO_NET(vdev); + VirtIONetQueue *q =3D &n->vqs[vq2q(virtio_get_queue_index(vq))]; + + if (n->tx_timer_activate) { + if (q->tx_timer =3D=3D NULL) { + q->tx_timer =3D timer_new_ns(QEMU_CLOCK_VIRTUAL, + virtio_net_tx_timer, q); + } + virtio_net_handle_tx_timer(vdev, vq); + } else { + virtio_net_handle_tx_bh(vdev, vq); + } +} + static void virtio_net_add_queue(VirtIONet *n, int index) { VirtIODevice *vdev =3D VIRTIO_DEVICE(n); @@ -2980,20 +2997,13 @@ static void virtio_net_add_queue(VirtIONet *n, int = index) n->vqs[index].rx_vq =3D virtio_add_queue(vdev, n->net_conf.rx_queue_si= ze, virtio_net_handle_rx); =20 - if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) { - n->vqs[index].tx_vq =3D - virtio_add_queue(vdev, n->net_conf.tx_queue_size, - virtio_net_handle_tx_timer); - n->vqs[index].tx_timer =3D timer_new_ns(QEMU_CLOCK_VIRTUAL, - virtio_net_tx_timer, - &n->vqs[index]); - } else { - n->vqs[index].tx_vq =3D - virtio_add_queue(vdev, n->net_conf.tx_queue_size, - virtio_net_handle_tx_bh); - n->vqs[index].tx_bh =3D qemu_bh_new_guarded(virtio_net_tx_bh, &n->= vqs[index], - &DEVICE(vdev)->mem_reent= rancy_guard); - } + n->vqs[index].tx_vq =3D + virtio_add_queue(vdev, n->net_conf.tx_queue_size, + virtio_net_handle_tx_dispatch); + + n->vqs[index].tx_bh =3D + qemu_bh_new_guarded(virtio_net_tx_bh, &n->vqs[index], + &DEVICE(vdev)->mem_reentrancy_guard); =20 n->vqs[index].tx_waiting =3D 0; n->vqs[index].n =3D n; @@ -3970,6 +3980,10 @@ static void virtio_net_device_realize(DeviceState *d= ev, Error **errp) error_printf("Defaulting to \"bh\""); } =20 + if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer") =3D=3D 0) { + n->tx_timer_activate =3D true; + } + n->net_conf.tx_queue_size =3D MIN(virtio_net_max_tx_queue_size(n), n->net_conf.tx_queue_size); =20 diff --git a/include/hw/virtio/virtio-net.h b/include/hw/virtio/virtio-net.h index 371e376428..a4eb3f407e 100644 --- a/include/hw/virtio/virtio-net.h +++ b/include/hw/virtio/virtio-net.h @@ -230,6 +230,7 @@ struct VirtIONet { struct EBPFRSSContext ebpf_rss; uint32_t nr_ebpf_rss_fds; char **ebpf_rss_fds; + bool tx_timer_activate; }; =20 size_t virtio_net_handle_ctrl_iov(VirtIODevice *vdev, --=20 2.53.0 From nobody Sun Jul 26 11:01:58 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1782998264; cv=none; d=zohomail.com; s=zohoarc; b=hEinBSiF8054+gabFzlXAhTMWCdlTGcv9CjNeM6YJ60RRNBmxV+pNptmhQPp8zfzTZs0PATglyP7LEbEvO+MHLdpydtt3hxpPDt9RVYGBiGrrHKTZQSwpkwJUqheTvh8rfRJnfbdUO9AVJ30YbXKx7USsy7fdxliQTqxkhWFpW4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1782998264; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=k8AXq2mYNb1GDo+DjVREdz3EV6SUym7jqkW4S2swevw=; b=ZG/XPj3BgEH1jfdswRgxiv74YzXDjxNwDrZC48QogNaSs4K+Js97G9tNcImM3jMkJoupbYW8CWhViuUNIrx9pg0o1ekDDSPZEOUi/24CWFQBO6QJrO2Npp1TovDeyhM1LX4Dr+Fr4OZ5fhbCY+t8OCqgqwu3lkV70Ym1tJkkX9Y= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1782998264040835.373854540412; Thu, 2 Jul 2026 06:17:44 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wfHID-0001ic-ER; Thu, 02 Jul 2026 09:17:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wfHI5-0001h4-9t for qemu-devel@nongnu.org; Thu, 02 Jul 2026 09:17:17 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wfHI2-0008Q8-VB for qemu-devel@nongnu.org; Thu, 02 Jul 2026 09:17:17 -0400 Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-278-sJFNVHRrMj2O-w5qok4l6Q-1; Thu, 02 Jul 2026 09:17:10 -0400 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B70FD18C0719; Thu, 2 Jul 2026 13:16:46 +0000 (UTC) Received: from kdutta-thinkpadp1gen7.bengluru.csb (unknown [10.74.81.96]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 916F218007FD; Thu, 2 Jul 2026 13:16:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1782998234; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=k8AXq2mYNb1GDo+DjVREdz3EV6SUym7jqkW4S2swevw=; b=cURJPcmvthx1lixyXYrVLClo3ByDv/0JQS62OcYKXYjgYPvGYy5b4XtDayP8aP4kglrJqU 5VjvggbQowUvQ8wr9Es9gNblSf4lR/xdKxG+1cbPucKA4lJj0MTEpC2MA30ohaE1VriOLU tyw5pV2ayTN1BmomQ2k31ZAbL6xww8Q= X-MC-Unique: sJFNVHRrMj2O-w5qok4l6Q-1 X-Mimecast-MFC-AGG-ID: sJFNVHRrMj2O-w5qok4l6Q_1782998229 From: Koushik Dutta To: qemu-devel@nongnu.org Cc: Zhao Liu , "Michael S. Tsirkin" , Jason Wang , Stefano Garzarella , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Eugenio=20P=C3=A9rez?= Subject: [PATCH v9 2/2] [PATCH 2/2] Implement VirtIO Network Notification Coalescing (VIRTIO_NET_F_NOTF_COAL). This allows guests to reduce interrupt overhead by configuring coalescing parameters via ethtool -C for both RX and TX paths. Date: Thu, 2 Jul 2026 18:46:32 +0530 Message-ID: <20260702131632.1180309-3-kdutta@redhat.com> In-Reply-To: <20260702131632.1180309-1-kdutta@redhat.com> References: <20260702131632.1180309-1-kdutta@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 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=lists1p.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=kdutta@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -24 X-Spam_score: -2.5 X-Spam_bar: -- X-Spam_report: (-2.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.445, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-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: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1782998266306158500 The feature supports two coalescing modes: - Time-based: delay notifications up to N microseconds - Count-based: delay until N packets are processed Implementation details: - Added VIRTIO_NET_CTRL_NOTF_COAL class handling in control virtqueue - RX path: batches notifications based on packet count or timeout - TX path: leverages the unified dispatcher to dynamically enable timer-based coalescing when guest configures it via ethtool - Coalescing parameters persist across live migration Note: When tx=3Dtimer is configured at VM launch, the coalescing feature is automatically disabled because tx=3Dtimer already introduces a fixed 150=C2=B5s delay for packet batching, making notification coalescing redund= ant. Signed-off-by: Koushik Dutta --- hw/core/machine.c | 1 + hw/net/virtio-net.c | 176 +++++++++++++++++++++++++++++++-- include/hw/virtio/virtio-net.h | 7 ++ net/passt.c | 1 + net/tap.c | 1 + net/vhost-user.c | 1 + net/vhost-vdpa.c | 1 + 7 files changed, 182 insertions(+), 6 deletions(-) diff --git a/hw/core/machine.c b/hw/core/machine.c index 4d8b15d99e..55ee886f41 100644 --- a/hw/core/machine.c +++ b/hw/core/machine.c @@ -43,6 +43,7 @@ GlobalProperty hw_compat_11_0[] =3D { { "chardev-vc", "encoding", "cp437" }, { "tpm-crb", "cap-chunk", "off" }, { "tpm-crb", "x-allow-chunk-migration", "off" }, + { TYPE_VIRTIO_NET, "vq_notf_coal", "off" }, }; const size_t hw_compat_11_0_len =3D G_N_ELEMENTS(hw_compat_11_0); =20 diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c index 319842cf28..4de6aa3b23 100644 --- a/hw/net/virtio-net.c +++ b/hw/net/virtio-net.c @@ -157,6 +157,16 @@ static void flush_or_purge_queued_packets(NetClientSta= te *nc) * - we could suppress RX interrupt if we were so inclined. */ =20 +static void virtio_net_rx_notify(void *opaque) +{ + VirtIONetQueue *q =3D opaque; + VirtIONet *n =3D q->n; + VirtIODevice *vdev =3D VIRTIO_DEVICE(n); + + q->rx_pkt_cnt =3D 0; + virtio_notify(vdev, q->rx_vq); +} + static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config) { VirtIONet *n =3D VIRTIO_NET(vdev); @@ -1004,6 +1014,56 @@ static void virtio_net_set_features(VirtIODevice *vd= ev, =20 static void virtio_net_tx_timer(void *opaque); =20 +static int virtio_net_handle_coal(VirtIONet *n, uint8_t cmd, + struct iovec *iov, unsigned int iov_cnt) +{ + struct virtio_net_ctrl_coal coal; + VirtIONetQueue *q; + size_t s; + int i; + + s =3D iov_to_buf(iov, iov_cnt, 0, &coal, sizeof(coal)); + if (s !=3D sizeof(coal)) { + return VIRTIO_NET_ERR; + } + + if (cmd =3D=3D VIRTIO_NET_CTRL_NOTF_COAL_RX_SET) { + for (i =3D 0; i < n->max_queue_pairs; i++) { + q =3D &n->vqs[i]; + q->rx_coal_usecs =3D le32_to_cpu(coal.max_usecs); + q->rx_coal_packets =3D le32_to_cpu(coal.max_packets); + if (q->rx_coal_usecs > 0) { + if (!q->rx_timer) { + q->rx_timer =3D timer_new_ns(QEMU_CLOCK_VIRTUAL, + virtio_net_rx_notify, + q); + } + } else { + g_clear_pointer(&q->rx_timer, timer_free); + } + } + } else if (cmd =3D=3D VIRTIO_NET_CTRL_NOTF_COAL_TX_SET) { + for (i =3D 0; i < n->max_queue_pairs; i++) { + q =3D &n->vqs[i]; + q->tx_coal_usecs =3D le32_to_cpu(coal.max_usecs); + q->tx_coal_packets =3D le32_to_cpu(coal.max_packets); + /* Converted us to ns */ + n->tx_timeout =3D q->tx_coal_usecs * 1000; + if (q->tx_coal_usecs > 0) { + if (!q->tx_timer) { + q->tx_timer =3D timer_new_ns(QEMU_CLOCK_VIRTUAL, + virtio_net_tx_timer, + q); + } + } else { + g_clear_pointer(&q->tx_timer, timer_free); + } + } + } + + return VIRTIO_NET_OK; +} + static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd, struct iovec *iov, unsigned int iov_c= nt) { @@ -1583,6 +1643,8 @@ size_t virtio_net_handle_ctrl_iov(VirtIODevice *vdev, status =3D virtio_net_handle_mq(n, ctrl.cmd, iov, out_num); } else if (ctrl.class =3D=3D VIRTIO_NET_CTRL_GUEST_OFFLOADS) { status =3D virtio_net_handle_offloads(n, ctrl.cmd, iov, out_num); + } else if (ctrl.class =3D=3D VIRTIO_NET_CTRL_NOTF_COAL) { + status =3D virtio_net_handle_coal(n, ctrl.cmd, iov, out_num); } =20 s =3D iov_from_buf(in_sg, in_num, 0, &status, sizeof(status)); @@ -2042,7 +2104,23 @@ static ssize_t virtio_net_receive_rcu(NetClientState= *nc, const uint8_t *buf, } =20 virtqueue_flush(q->rx_vq, i); - virtio_notify(vdev, q->rx_vq); + + /* rx coalescing */ + q->rx_pkt_cnt +=3D i; + if (q->rx_coal_usecs =3D=3D 0 || q->rx_pkt_cnt >=3D q->rx_coal_packets= ) { + if (q->rx_timer) { + timer_del(q->rx_timer); + } + virtio_net_rx_notify(q); + } else { + if (q->rx_timer) { + if (!timer_pending(q->rx_timer)) { + timer_mod(q->rx_timer, + qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + + q->rx_coal_usecs * 1000); + } + } + } =20 return size; =20 @@ -2900,6 +2978,12 @@ static void virtio_net_tx_timer(void *opaque) if (ret =3D=3D -EBUSY || ret =3D=3D -EINVAL) { return; } + if (q->tx_pkt_cnt < ret) { + q->tx_pkt_cnt =3D 0; + } else { + q->tx_pkt_cnt -=3D ret; + } + /* * If we flush a full burst of packets, assume there are * more coming and immediately rearm @@ -2919,6 +3003,7 @@ static void virtio_net_tx_timer(void *opaque) ret =3D virtio_net_flush_tx(q); if (ret > 0) { virtio_queue_set_notification(q->tx_vq, 0); + q->tx_pkt_cnt -=3D ret; q->tx_waiting =3D 1; timer_mod(q->tx_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout); @@ -2985,6 +3070,15 @@ static void virtio_net_handle_tx_dispatch(VirtIODevi= ce *vdev, VirtQueue *vq) virtio_net_tx_timer, q); } virtio_net_handle_tx_timer(vdev, vq); + } else if (q->tx_coal_usecs > 0 && q->tx_timer) { + q->tx_pkt_cnt++; + if (q->tx_pkt_cnt < q->tx_coal_packets) { + virtio_net_handle_tx_timer(vdev, vq); + } else { + q->tx_pkt_cnt =3D 0; + timer_del(q->tx_timer); + virtio_net_handle_tx_bh(vdev, vq); + } } else { virtio_net_handle_tx_bh(vdev, vq); } @@ -3006,6 +3100,12 @@ static void virtio_net_add_queue(VirtIONet *n, int i= ndex) &DEVICE(vdev)->mem_reentrancy_guard); =20 n->vqs[index].tx_waiting =3D 0; + n->vqs[index].rx_pkt_cnt =3D 0; + n->vqs[index].tx_pkt_cnt =3D 0; + n->vqs[index].rx_coal_usecs =3D 0; + n->vqs[index].tx_coal_usecs =3D 0; + n->vqs[index].rx_coal_packets =3D 0; + n->vqs[index].tx_coal_packets =3D 0; n->vqs[index].n =3D n; } =20 @@ -3018,10 +3118,12 @@ static void virtio_net_del_queue(VirtIONet *n, int = index) qemu_purge_queued_packets(nc); =20 virtio_del_queue(vdev, index * 2); - if (q->tx_timer) { - timer_free(q->tx_timer); - q->tx_timer =3D NULL; - } else { + + /* Cleanup coalescing timers */ + g_clear_pointer(&q->rx_timer, timer_free); + g_clear_pointer(&q->tx_timer, timer_free); + + if (q->tx_bh) { qemu_bh_delete(q->tx_bh); q->tx_bh =3D NULL; } @@ -3098,6 +3200,13 @@ static void virtio_net_get_features(VirtIODevice *vd= ev, uint64_t *features, virtio_features_or(features, features, n->host_features_ex); =20 virtio_add_feature_ex(features, VIRTIO_NET_F_MAC); + if (n->tx_timer_activate) { + virtio_clear_feature_ex(features, VIRTIO_NET_F_NOTF_COAL); + } else { + if (!virtio_has_feature(*features, VIRTIO_NET_F_NOTF_COAL)) { + *features &=3D ~(1ULL << VIRTIO_NET_F_NOTF_COAL); + } + } =20 if (!peer_has_vnet_hdr(n)) { virtio_clear_feature_ex(features, VIRTIO_NET_F_CSUM); @@ -3252,6 +3361,27 @@ static int virtio_net_post_load_device(void *opaque,= int version_id) } =20 virtio_net_commit_rss_config(n); + + for (i =3D 0; i < n->max_queue_pairs; i++) { + VirtIONetQueue *q =3D &n->vqs[i]; + if (q->rx_coal_usecs > 0) { + if (!q->rx_timer) { + q->rx_timer =3D timer_new_ns(QEMU_CLOCK_VIRTUAL, + virtio_net_rx_notify, + q); + } + } + + if (q->tx_coal_usecs > 0) { + n->tx_timeout =3D q->tx_coal_usecs * 1000; + if (!q->tx_timer) { + q->tx_timer =3D timer_new_ns(QEMU_CLOCK_VIRTUAL, + virtio_net_tx_timer, + q); + } + } + } + return 0; } =20 @@ -3271,13 +3401,41 @@ static int virtio_net_post_load_virtio(VirtIODevice= *vdev) return 0; } =20 +static bool virtio_net_queue_notf_coal_needed(void *opaque) +{ + VirtIONetQueue *q =3D opaque; + VirtIONet *n =3D q->n; + + return virtio_vdev_has_feature(VIRTIO_DEVICE(n), VIRTIO_NET_F_NOTF_COA= L); +} + +static const VMStateDescription vmstate_virtio_net_queue_notf_coal =3D { + .name =3D "virtio-net-queue-tx_waiting/notf-coal", + .version_id =3D 1, + .minimum_version_id =3D 1, + .needed =3D virtio_net_queue_notf_coal_needed, + .fields =3D (const VMStateField[]) { + VMSTATE_UINT32(rx_coal_usecs, VirtIONetQueue), + VMSTATE_UINT32(tx_coal_usecs, VirtIONetQueue), + VMSTATE_UINT32(rx_coal_packets, VirtIONetQueue), + VMSTATE_UINT32(tx_coal_packets, VirtIONetQueue), + VMSTATE_UINT32(rx_pkt_cnt, VirtIONetQueue), + VMSTATE_UINT32(tx_pkt_cnt, VirtIONetQueue), + VMSTATE_END_OF_LIST() + }, +}; + /* tx_waiting field of a VirtIONetQueue */ static const VMStateDescription vmstate_virtio_net_queue_tx_waiting =3D { .name =3D "virtio-net-queue-tx_waiting", .fields =3D (const VMStateField[]) { VMSTATE_UINT32(tx_waiting, VirtIONetQueue), VMSTATE_END_OF_LIST() - }, + }, + .subsections =3D (const VMStateDescription * const []) { + &vmstate_virtio_net_queue_notf_coal, + NULL + } }; =20 static bool max_queue_pairs_gt_1(void *opaque, int version_id) @@ -3982,6 +4140,10 @@ static void virtio_net_device_realize(DeviceState *d= ev, Error **errp) =20 if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer") =3D=3D 0) { n->tx_timer_activate =3D true; + if (n->host_features & (1ULL << VIRTIO_NET_F_NOTF_COAL)) { + warn_report("virtio-net: 'vq_notf_coal' is incompatible with '= tx=3Dtimer'," + "disabling notification coalescing"); + } } =20 n->net_conf.tx_queue_size =3D MIN(virtio_net_max_tx_queue_size(n), @@ -4272,6 +4434,8 @@ static const Property virtio_net_properties[] =3D { VIRTIO_NET_F_GUEST_USO6, true), DEFINE_PROP_BIT64("host_uso", VirtIONet, host_features, VIRTIO_NET_F_HOST_USO, true), + DEFINE_PROP_BIT64("vq_notf_coal", VirtIONet, host_features, + VIRTIO_NET_F_NOTF_COAL, false), DEFINE_PROP_ON_OFF_AUTO_BIT64("hash-ipv4", VirtIONet, rss_data.specified_hash_types, VIRTIO_NET_HASH_REPORT_IPv4 - 1, diff --git a/include/hw/virtio/virtio-net.h b/include/hw/virtio/virtio-net.h index a4eb3f407e..550aaea2be 100644 --- a/include/hw/virtio/virtio-net.h +++ b/include/hw/virtio/virtio-net.h @@ -159,12 +159,19 @@ typedef struct VirtIONetQueue { VirtQueue *rx_vq; VirtQueue *tx_vq; QEMUTimer *tx_timer; + QEMUTimer *rx_timer; QEMUBH *tx_bh; uint32_t tx_waiting; struct { VirtQueueElement *elem; } async_tx; struct VirtIONet *n; + uint32_t rx_coal_usecs; /* RX interrupt coalescing timeout (microsec= onds) */ + uint32_t rx_coal_packets; /* RX packet count threshold for coalescing = */ + uint32_t rx_pkt_cnt; /* Current RX packet count since last notifi= cation */ + uint32_t tx_coal_usecs; /* TX interrupt coalescing timeout (microsec= onds) */ + uint32_t tx_coal_packets; /* TX packet count threshold for coalescing = */ + uint32_t tx_pkt_cnt; /* Current TX packet count since last notifi= cation */ } VirtIONetQueue; =20 struct VirtIONet { diff --git a/net/passt.c b/net/passt.c index 45440c399b..43b36ed8c5 100644 --- a/net/passt.c +++ b/net/passt.c @@ -52,6 +52,7 @@ static const int user_feature_bits[] =3D { VIRTIO_NET_F_GUEST_USO4, VIRTIO_NET_F_GUEST_USO6, VIRTIO_NET_F_HOST_USO, + VIRTIO_NET_F_NOTF_COAL, =20 /* This bit implies RARP isn't sent by QEMU out of band */ VIRTIO_NET_F_GUEST_ANNOUNCE, diff --git a/net/tap.c b/net/tap.c index 57ffb09885..e6ddbc1eb1 100644 --- a/net/tap.c +++ b/net/tap.c @@ -63,6 +63,7 @@ static const int kernel_feature_bits[] =3D { VIRTIO_F_NOTIFICATION_DATA, VIRTIO_NET_F_RSC_EXT, VIRTIO_NET_F_HASH_REPORT, + VIRTIO_NET_F_NOTF_COAL, VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO, VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO, VHOST_INVALID_FEATURE_BIT diff --git a/net/vhost-user.c b/net/vhost-user.c index 2d0fc49b4d..f1e9b7a038 100644 --- a/net/vhost-user.c +++ b/net/vhost-user.c @@ -54,6 +54,7 @@ static const int user_feature_bits[] =3D { VIRTIO_NET_F_GUEST_USO4, VIRTIO_NET_F_GUEST_USO6, VIRTIO_NET_F_HOST_USO, + VIRTIO_NET_F_NOTF_COAL, =20 /* This bit implies RARP isn't sent by QEMU out of band */ VIRTIO_NET_F_GUEST_ANNOUNCE, diff --git a/net/vhost-vdpa.c b/net/vhost-vdpa.c index f1523697e2..0dcd6fb9f1 100644 --- a/net/vhost-vdpa.c +++ b/net/vhost-vdpa.c @@ -70,6 +70,7 @@ static const int vdpa_feature_bits[] =3D { VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_RX_EXTRA, VIRTIO_NET_F_CTRL_VLAN, + VIRTIO_NET_F_NOTF_COAL, VIRTIO_NET_F_CTRL_VQ, VIRTIO_NET_F_GSO, VIRTIO_NET_F_GUEST_CSUM, --=20 2.53.0