From nobody Tue Nov 10 15:51:47 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4jPB3R4cgJz6yfry for ; Tue, 10 Nov 2026 15:51:47 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jPB3R3xXLz4VGt for ; Tue, 10 Nov 2026 15:51:47 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794325907; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=2jWhEvl3S7r+7IKR7ffSj33KXKKU3g/fcNQ+xpvGBWE=; b=ENEy3WzWRG2JqRt0WfFGCiI0aWXyLPlfmUpxNfTmNwkrxbhmnM0D0/yIcQrCMZt5LsiJ4j FHEUmfjIkNxCJLddphBNHx9tZAYeT9Eh0w+OPpkLe8YzoeUoVnlmHUMxeRnM41/2Fp7VWl tyc/J1lhWXefXc2vOY1Qu9fFw10g4Hb4FeoIbisyxKieoEUVjKqxDPY4u5mQrWwikoIESI CAWA6jT+qv+wRwVI9eHQwc1yhJGe1TmYv1EnjxmqJUnyvvT/dyE2kFeTEIPuiEHX7ayU2o y4VhV9KwIk6WdeELU17K6VRbOM9ZjRvvhZ4olz6TgpKAdSD/e9XHmVKG/f/oKg== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1794325907; b=d1wEAZ1S9SUa5E9dd9UKT3MIauJElPloJE/qKJYhPhmsiMeesIIX3iSnecFnu1cRHo8pnl k+IkToTwfFgjTZd/FXlrmTREvmEu8Gh8mUMk7XAPXnjC9I18xUOwIm+iBE9docAg4+TUa8 u0saYGzh6bg77s9+n7+NWXsS8Z2oopFADeVDYWAOC/4SW8nWQFgU8xjCA57/VvUcxrpZ50 Tb5EiflVdnmPdizaZc/Oxv4KEqiiC7LFf1cfu0LACmmnDfKlAxgQkAewvqG1f8hpL+Hx5q PiVpzLQB8ya97VPqhWXECQPpT+ZXIg1/FN8JcjCFIQZwjxkaTaiG8Qw53b0TmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794325907; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=2jWhEvl3S7r+7IKR7ffSj33KXKKU3g/fcNQ+xpvGBWE=; b=SjbQABFWcvH4DNdiwLZgmykw82owHYbmF99iD6DCww75d5bHchSZh+3AhYX0WKacyZeCrS ceVvffOpck7jjF9e8vEqgBG/jdu5xw0a3dAz5j7vicgPNWI9ErCw8oJLujGuKVyJNxGYKb +RJuCkEDM3H2WIhsz5exVAbepMI0RH3GKmNDZ4nvbDWu1sUCNrXGrOQkYHXV8O2uDtTvih 2zPqMprEYLx8Th8zEp0RhFjKFFTKzWXFvVB9b1iGKZJLau2Lav1DU0R/haJS1VNnbWaJn1 TDDQtiHnyyfPHQgpV+72v++bFjOROL98UXwCZUa28pRyfmQma5qZjx+SPEQ+7g== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4jPB3R3JxGzbR7 for ; Tue, 10 Nov 2026 15:51:47 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 9ac4 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Tue, 10 Nov 2026 15:51:47 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Andrew Gallatin Subject: git: fe63435bdfae - main - iflib: rate limit deferred tx ring drain List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all X-BeenThere: dev-commits-src-all@freebsd.org List-Id: Commit messages for all branches of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: gallatin X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: fe63435bdfae0ec55092efbfa807e324bfb82d75 Auto-Submitted: auto-generated Date: Tue, 10 Nov 2026 15:51:47 +0000 Message-Id: <6af33d93.9ac4.4c057647@gitrepo.freebsd.org> The branch main has been updated by gallatin: URL: https://cgit.FreeBSD.org/src/commit/?id=fe63435bdfae0ec55092efbfa807e324bfb82d75 commit fe63435bdfae0ec55092efbfa807e324bfb82d75 Author: Andrew Gallatin AuthorDate: 2026-11-10 15:42:11 +0000 Commit: Andrew Gallatin CommitDate: 2026-11-10 15:42:11 +0000 iflib: rate limit deferred tx ring drain When simple_tx gives up & return ENOBUFS because the tx ring is full, we were doing a GROUPTASK_ENQUEUE(&txq->ift_task) each time. On a tx ring that has overflowed and is being hammered on by a fast UDP sender, this can result in a massive increase in context switches (I've seen over 30M ctxw/sec due to this, and the ctxsw codepath taking roughly 50% CPU). This change removes the GROUPTASK_ENQUEUE() and replaces it with a call to a callout which drains the tx ring. The callout time is determined based on the link speed, estimating how long it would take for a large fraction of of a ring containing full-MTU packets to drain. This removes the 30M ctxsw / sec in my test case and reduces it to roughly a doubling of ctxsws. Reviewed by: kbowling Sponsored by: Netflix Differential Revision: https://reviews.freebsd.org/D61313 --- sys/net/iflib.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 10 deletions(-) diff --git a/sys/net/iflib.c b/sys/net/iflib.c index 7f1e16b183ac..bae5271d38c2 100644 --- a/sys/net/iflib.c +++ b/sys/net/iflib.c @@ -197,6 +197,7 @@ static int iflib_simple_transmit_txq_select(if_t ifp, struct mbuf *m); static int iflib_simple_transmit_txq_select_v2(if_t ifp, struct mbuf *m); static void iflib_simple_if_start(if_t ifp); static void iflib_simple_txq_drain(iflib_txq_t txq); +static void iflib_tx_drain_schedule(iflib_txq_t txq); typedef struct iflib_filter_info { driver_filter_t *ifi_filter; @@ -458,6 +459,7 @@ struct iflib_txq { qidx_t ift_pad; uint16_t ift_id; struct callout ift_timer; + struct callout ift_drain_timer; #ifdef DEV_NETMAP struct callout ift_netmap_timer; #endif /* DEV_NETMAP */ @@ -703,6 +705,10 @@ static u_int iflib_simple_drain_quota_thread = 65536; SYSCTL_UINT(_net_iflib, OID_AUTO, simple_drain_quota_thread, CTLFLAG_RWTUN, &iflib_simple_drain_quota_thread, 0, "maximum packets sent per simple tx deferral ring drain from another thread"); +static u_int iflib_tx_drain_min_us = 1; +SYSCTL_UINT(_net_iflib, OID_AUTO, tx_drain_min_us, CTLFLAG_RWTUN, + &iflib_tx_drain_min_us, 0, + "Minimum time to wait to run tx drain callout"); static u_int iflib_max_producers = 8; static bool iflib_single_llc __read_mostly; static bool iflib_producer_gate __read_mostly; @@ -2700,6 +2706,8 @@ iflib_init_locked(if_ctx_t ctx) STATE_LOCK(ctx); iflib_set_running(ctx, false); STATE_UNLOCK(ctx); + for (i = 0; i < scctx->isc_ntxqsets; i++) + callout_drain(&ctx->ifc_txqs[i].ift_drain_timer); IFDI_INTR_DISABLE(ctx); /* @@ -2871,6 +2879,8 @@ iflib_stop(if_ctx_t ctx) STATE_LOCK(ctx); iflib_set_running(ctx, false); STATE_UNLOCK(ctx); + for (i = 0; i < scctx->isc_ntxqsets; i++) + callout_drain(&txq[i].ift_drain_timer); if (stop_hardware) { ctx->ifc_datapath_state = IFLIB_DP_STOPPING; @@ -3928,8 +3938,9 @@ defrag: m_freem(*m_headp); DBG_COUNTER_INC(tx_frees); *m_headp = NULL; - } - if ((txq->ift_task.gt_task.ta_flags & TASK_ENQUEUED) == 0) + iflib_tx_drain_schedule(txq); + } else if ((txq->ift_task.gt_task.ta_flags & + TASK_ENQUEUED) == 0) GROUPTASK_ENQUEUE(&txq->ift_task); return (ENOBUFS); } @@ -5977,6 +5988,7 @@ iflib_tqg_detach(if_ctx_t ctx) tqg = qgroup_if_io_tqg; for (txq = ctx->ifc_txqs, i = 0; i < NTXQSETS(ctx); i++, txq++) { callout_drain(&txq->ift_timer); + callout_drain(&txq->ift_drain_timer); #ifdef DEV_NETMAP callout_drain(&txq->ift_netmap_timer); #endif /* DEV_NETMAP */ @@ -6554,6 +6566,7 @@ iflib_queues_alloc(if_ctx_t ctx) device_get_nameunit(dev), txq->ift_id); mtx_init(&txq->ift_mtx, txq->ift_mtx_name, NULL, MTX_DEF); callout_init_mtx(&txq->ift_timer, &txq->ift_mtx, 0); + callout_init(&txq->ift_drain_timer, 1); txq->ift_timer.c_cpu = cpu; #ifdef DEV_NETMAP callout_init_mtx(&txq->ift_netmap_timer, &txq->ift_mtx, 0); @@ -8077,6 +8090,44 @@ iflib_txq_producer_exit(iflib_txq_t txq, bool pinned) sched_unpin(); } +static void +iflib_tx_drain_timer(void *arg) +{ + iflib_txq_t txq = arg; + + if (iflib_is_running(txq->ift_ctx)) + iflib_simple_txq_drain(txq); +} + +static void +iflib_tx_drain_schedule(iflib_txq_t txq) +{ + uint64_t baudrate, usec; + if_t ifp; + + mtx_assert(&txq->ift_mtx, MA_OWNED); + if (!iflib_is_running(txq->ift_ctx) || + callout_pending(&txq->ift_drain_timer)) + return; + + /* + * Leave time for half a full ring to drain: one MTU per + * descriptor. Assume worst case of 1 ring handling all + * traffic. Cap at 1/2 of the iflib timer period. + */ + ifp = txq->ift_ctx->ifc_ifp; + baudrate = if_getbaudrate(ifp); + baudrate = MAX(UINT64_C(1), baudrate); + usec = (uint64_t)txq->ift_size * if_getmtu(ifp) * + 8 * sbttous(SBT_1S) / baudrate / 2; + usec = MIN(usec, TICKS_2_USEC(iflib_timer_default / 2)); + usec = MAX(usec, 1); + callout_reset_sbt_on(&txq->ift_drain_timer, + ustosbt(MAX(usec, iflib_tx_drain_min_us)), 0, + iflib_tx_drain_timer, txq, + txq->ift_task.gt_cpu, C_PREL(2)); +} + /* Consumes the mbuf in all cases */ static int iflib_simple_encap(iflib_txq_t txq, struct mbuf *m, int *bytes, int *pkts, @@ -8135,9 +8186,7 @@ iflib_simple_drbr_drain(iflib_txq_t txq, u_int quota, int *bytes, int *pkts, } if (!drbr_empty(ifp, txq->ift_drbr)) { txq->ift_drbr_stall++; - if (quota != iflib_simple_drain_quota && - (txq->ift_task.gt_task.ta_flags & TASK_ENQUEUED) == 0) - GROUPTASK_ENQUEUE(&txq->ift_task); + iflib_tx_drain_schedule(txq); } } @@ -8386,7 +8435,6 @@ iflib_simple_if_start(if_t ifp) { if_ctx_t ctx; iflib_txq_t txq; - bool retry; int bytes_sent = 0, pkt_sent = 0, mcast_sent = 0; ctx = if_getsoftc(ifp); @@ -8398,12 +8446,8 @@ iflib_simple_if_start(if_t ifp) &mcast_sent); if (txq->ift_db_pending != 0) (void)iflib_txd_db_check(txq, true); - retry = !drbr_empty(ifp, txq->ift_drbr); mtx_unlock(&txq->ift_mtx); - if (retry) - GROUPTASK_ENQUEUE(&txq->ift_task); - if_inc_counter(ifp, IFCOUNTER_OBYTES, bytes_sent); if_inc_counter(ifp, IFCOUNTER_OPACKETS, pkt_sent); if (mcast_sent)