From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202512 header.b=fkQ0Nh0u; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 1409B5A0623 for ; Mon, 08 Dec 2025 07:12:59 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202512; t=1765174375; bh=jrrSvgeldj857QbFke+Rw74zOB/e839s/hkz/IO+ACs=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=fkQ0Nh0uQc1dqNqBs/2PFE4dgTUt2g0HjNe05NbjK8nrQVKEmC+uHYbMeQyBs1wgv 0zZ0nuzCbpZQo65FjoJFeN59kX6ZhbmWUwW6+uu7lvBl7jhKIlzNAUyd/3dYh9nscQ O2hteePjd6yzKneTceVqqHhLMw/N8R8zocV9cPTGt5OXCU7Qt9Vjnminuyt4/7ELO3 yqz7KY9xdvHLzl65Z4R7IUzG6IA+Ta1mYMor2nDVWXQ52HXPv83ec7omB/xTjBYMZJ rVAyPCTSlJcu0NjKaULtUwJDQE/7yT1qTlgbJ99frOhvuQNGL97in3nWF7ePIrjxoG K8cAECzYop4ow== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4dPs936tXYz4wM2; Mon, 08 Dec 2025 17:12:55 +1100 (AEDT) Date: Mon, 8 Dec 2025 16:33:17 +1100 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH v2 1/9] tcp, util: Add function for scaling to linearly interpolated factor, use it Message-ID: References: <20251208002229.391162-1-sbrivio@redhat.com> <20251208002229.391162-2-sbrivio@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="eye5E+NNWQLifAX1" Content-Disposition: inline In-Reply-To: <20251208002229.391162-2-sbrivio@redhat.com> Message-ID-Hash: QDR5TE7X5BOYRR2XHDPIAFWJHKFYBF2B X-Message-ID-Hash: QDR5TE7X5BOYRR2XHDPIAFWJHKFYBF2B X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top, Max Chernoff X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: --eye5E+NNWQLifAX1 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Dec 08, 2025 at 01:22:09AM +0100, Stefano Brivio wrote: > Right now, the only need for this kind of function comes from > tcp_get_sndbuf(), which calculates the amount of sending buffer we > want to use depending on its own size: we want to use more of it > if it's smaller, as bookkeeping overhead is usually lower and we rely > on auto-tuning there, and use less of it when it's bigger. >=20 > For this purpose, the new function is overly generic and its name is > a mouthful: @x is the same as @y, that is, we want to use more or > less of the buffer depending on the size of the buffer itself. >=20 > However, an upcoming change will need that generality, as we'll want > to scale the amount of sending buffer we use depending on another > (scaled) factor. >=20 > While at it, now that we have this new function, which makes it simple > to specify a precise usage factor, change the amount of sending buffer > we want to use at and above 4 MiB: 75% looks perfectly safe. >=20 > Signed-off-by: Stefano Brivio Reviewed-by: David Gibson In that it looks correct. Several suggestions for improvements in clarity below, either as followups, or in case a respin is needed anyway. > --- > tcp.c | 8 ++------ > util.c | 38 ++++++++++++++++++++++++++++++++++++++ > util.h | 1 + > 3 files changed, 41 insertions(+), 6 deletions(-) >=20 > diff --git a/tcp.c b/tcp.c > index bb661ee..37012cc 100644 > --- a/tcp.c > +++ b/tcp.c > @@ -773,7 +773,7 @@ static void tcp_rtt_dst_check(const struct tcp_tap_co= nn *conn, > } > =20 > /** > - * tcp_get_sndbuf() - Get, scale SO_SNDBUF between thresholds (1 to 0.5 = usage) > + * tcp_get_sndbuf() - Get, scale SO_SNDBUF between thresholds (1 to 0.75= usage) I'd slightly prefer the change to 0.75 be in a separate patch, just so it's easier to tell that change to the helper function itself doesn't change behaviour here. > * @conn: Connection pointer > */ > static void tcp_get_sndbuf(struct tcp_tap_conn *conn) > @@ -788,11 +788,7 @@ static void tcp_get_sndbuf(struct tcp_tap_conn *conn) > return; > } > =20 > - v =3D sndbuf; > - if (v >=3D SNDBUF_BIG) > - v /=3D 2; > - else if (v > SNDBUF_SMALL) > - v -=3D v * (v - SNDBUF_SMALL) / (SNDBUF_BIG - SNDBUF_SMALL) / 2; > + v =3D scale_x_to_y_slope(sndbuf, sndbuf, SNDBUF_SMALL, SNDBUF_BIG, 75); > =20 > SNDBUF_SET(conn, MIN(INT_MAX, v)); > } > diff --git a/util.c b/util.c > index f32c9cb..ff0ba01 100644 > --- a/util.c > +++ b/util.c > @@ -1223,3 +1223,41 @@ void fsync_pcap_and_log(void) > if (log_file !=3D -1) > (void)fsync(log_file); > } > + > +/** > + * scale_x_to_y_slope() - Scale @x from 100% to f% depending on @y's val= ue Would "clamped_scale" work as a more descriptive name? > + * @x: Value to scale > + * @y: Value determining scaling > + * @lo: Lower bound for @y (start of y-axis slope) > + * @hi: Upper bound for @y (end of y-axis slope) > + * @f: Scaling factor, percent Maybe worth clarifying that this can be less than or more than 100% - description below uses >100%, but the usage above is <100%. > + * > + * Return: @x scaled by @f * linear interpolation of @y between @lo and = @hi > + * > + * In pictures: > + * > + * f % -> ,---- * If @y < lo (for example, @y is y0), r= eturn @x > + * /| | > + * / | | * If @lo < @y < @hi (for example, @y is= y1), > + * / | | return @x scaled by a factor linearly > + * (100 + f) / 2 % ->/ | | interpolated between 100% and f% depe= nding on > + * /| | | @y's position between @lo (100%) and = @hi (f%) > + * / | | | > + * / | | | * If @y > @hi (for example, @y is y2), = return > + * 100 % -> -----' | | | @x * @f / 100 > + * | | | | | > + * y0 lo y1 hi y2 Example: @f =3D 150, @lo =3D 10, @hi = =3D 20, @y =3D 15, > + * @x =3D 1000 > + * -> interpolated factor is 125% > + * -> return 1250 > + */ > +long scale_x_to_y_slope(long x, long y, long lo, long hi, long f) > +{ > + if (y < lo) > + return x; > + > + if (y > hi) > + return x * f / 100; > + > + return x - (x * (y - lo) / (hi - lo)) * (100 - f) / 100; There's a subtle tradeoff here. Dividing by (hi - lo) before multiplying by the factor loses some precision in the final result. On the hand, doing all the multiplies first would increase the risk of an overflow. Possible different way of organising this that _might_ be slightly easier to describe: rather than including a scaling factor, instead give upper and lower bounds of the output, so something like: long clamped_scale(long a, long b, long s, long sa, long sb) =3D> returns alue between @a and @b, matching where @s lies between @sa and @sb. > +} > diff --git a/util.h b/util.h > index 17f5ae0..ec75453 100644 > --- a/util.h > +++ b/util.h > @@ -242,6 +242,7 @@ int read_remainder(int fd, const struct iovec *iov, s= ize_t cnt, size_t skip); > void close_open_files(int argc, char **argv); > bool snprintf_check(char *str, size_t size, const char *format, ...); > void fsync_pcap_and_log(void); > +long scale_x_to_y_slope(long x, long y, long lo, long hi, long f); > =20 > /** > * af_name() - Return name of an address family > --=20 > 2.43.0 >=20 --=20 David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson --eye5E+NNWQLifAX1 Content-Type: application/pgp-signature; name=signature.asc -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmk2YxwACgkQzQJF27ox 2GdPKQ/+M18OFxvNPwuvx6d8he70o20wjd6YPuJsJOaH71+9KgnLE5KPeNm1tAzv kcK5hkxZt7oTbHQBNfdg7R0lMRQ7wrCB1G6aMhE6N25R9LcIwQyK9Jf7GoaI0QBl wEA6B67rkAsno3pqOT/xDTQz+j/nqTvXCyoc1knZdZfuFE9iAbT19cYVCiw1E3J6 6YfHc/tbIfSo85CkIlsDm7WxkwN7sef5TPlEyn9manfrSLW2SHlGkdGBQRoARpx2 2yWdtsl32Ccmn9s8vkeiP7JjoFR1TaGLT4FEQ/vjtFypQ5ZY9Ibl6m9niRf0Blt+ lOSuaT175bFVEJOjtyxREcSaBcNxVAZFV0Nj6aiZcd249E6hWQny+5nXWa3RWv1D 9zsYXKFY5hdv+R0Xa8cSRUsXClUFTBB+P6wFrmD5rz5QWt123mqi8WwEPOdd+K5R xW4ReiQOlBVMxLAOQVLd29eK4jRhI60TMsnx4Dh2JXpag2BSMI8D9BD9Th+4PuP9 yDSoluIKu1uf2mieGO5ycJMa0HI793p6S3kDVLsqG6VVc2nioR748hpawg7unylI rbSwrB2D0d2yMxLqFVyj7kxeRs9mJjGeDdIv9cHLVs4xQr0gxL3/OEGm4ftLMSaF 9J/TtHy4NzOTP8k9P95BQLy4hvPhNUaotjbrZ6Ob2bGvXH9RB+M= =uQGO -----END PGP SIGNATURE----- --eye5E+NNWQLifAX1--