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=CqyqFTIG; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id A5BD95A0626 for ; Tue, 09 Dec 2025 06:13:32 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202512; t=1765257208; bh=TPoFTIXhH1czCc9dU8kDvSpyVw6GQPLqG074sUhkG/Q=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=CqyqFTIGuVQyXpBuo/2uaINeNNSGxosudCjLniCOFGmemKPSvjv0heekc+LM7sn4q Ply9/ENrrckrbEfcZgi63IEw5GJQIZxhrSQEvm7JgzAeLGLI6Vn/tFtO9Mcf7g+Br+ CVhsPy0XS1PbutzsJpMv6QfCWLZFeac2XthdcSj/HPdONXgtgIphPMBHw68VvjzcDC VqzYIP5rXgQAZbpJa3mZhDV5uvR1+9HIzIKIzl9sEQOqpt5KXxepvGjFWHUO86Pi/y 3dYMUuFVGcYDzYVGiCELWTPHV3DT36a2ny/SscX/aKr2VFMC6uL+yB9nsM84PgBjY9 hSXxpm2LVvCcg== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4dQRp04cGxz4wGK; Tue, 09 Dec 2025 16:13:28 +1100 (AEDT) Date: Tue, 9 Dec 2025 16:05:02 +1100 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH v3 01/10] tcp, util: Add function for scaling to linearly interpolated factor, use it Message-ID: References: <20251208072024.3884137-1-sbrivio@redhat.com> <20251208072024.3884137-2-sbrivio@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="ppEHaN6d+D5nipX1" Content-Disposition: inline In-Reply-To: <20251208072024.3884137-2-sbrivio@redhat.com> Message-ID-Hash: B6GUPYVKTTTEV6MJ37C4GLCYFGTGA5L4 X-Message-ID-Hash: B6GUPYVKTTTEV6MJ37C4GLCYFGTGA5L4 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: --ppEHaN6d+D5nipX1 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Dec 08, 2025 at 08:20:14AM +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: @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 > Signed-off-by: Stefano Brivio Reviewed-by: David Gibson > --- > tcp.c | 6 +----- > util.c | 38 ++++++++++++++++++++++++++++++++++++++ > util.h | 1 + > 3 files changed, 40 insertions(+), 5 deletions(-) >=20 > diff --git a/tcp.c b/tcp.c > index bb661ee..026546a 100644 > --- a/tcp.c > +++ b/tcp.c > @@ -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 clamped_scale(sndbuf, sndbuf, SNDBUF_SMALL, SNDBUF_BIG, 50); > =20 > SNDBUF_SET(conn, MIN(INT_MAX, v)); > } > diff --git a/util.c b/util.c > index f32c9cb..2232a24 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); > } > + > +/** > + * clamped_scale() - Scale @x from 100% to f% depending on @y's value > + * @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 (might be less or more than 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 clamped_scale(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; > +} > diff --git a/util.h b/util.h > index 17f5ae0..744880b 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 clamped_scale(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 --ppEHaN6d+D5nipX1 Content-Type: application/pgp-signature; name=signature.asc -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmk3reoACgkQzQJF27ox 2GcVrQ/+LytX7YCn6VkMJ0ua9ylnS1XXTzZiuXOLbsACX6VpxKQtS+0LSnv3vRZf VwPLQDh++1s9CGs/q6NOGZhVF9haaSt/tVH765WyrFWuLp46vL5WqeAEm1yE83a8 KC2zMJFYt+N4mpFXcz18SKLNoVYGtXKbYH1aiuLsEuPRnVGblF5ZM4danCA6w44/ 7F46fxEgLexLi8GnTXe4m+PtAu9ClxBXrbjO3UbPY/F44WVZdrDte9MYVi9FDqF0 2GEMxzQmA1MFGx9T4bRRoYXbB4F5ydNSdC3ydIWvh3C2EuPWi9j0EEcYnUUqWWmB kpbQRMUj7Fyb9I+BHg8r/flX86C/pRcadSI0+VJst3391jh+D7pGp+W1gyAKCG7X HQphQ6n61HRcX0jNGCimWi4nLIP9YPC5fApIPji5LvT6dVv99r4HfJXW9qcBQaTT Ihl/k4Drxjv/Rstt2CYg4WDxLiawpzXzWtdYpZA+D3/tc4H5qfJeZh5dfPSm/S6b yfBlams75LAYCHTW384Nof+Xpi9XleC+/njRBaClgLyp+Lqv2bGirv7uoENNwv8m oIHm3euz14nUUYT/SvEtItYj5FZDcbchsSHdmzBEmhtjkk+RN+SVtqXY4UM/x1Os KtVf4YbM7vCfY5IOlLqF3tCig9OszOtwvzNz4Fj6awV3cZBXHYg= =nFpi -----END PGP SIGNATURE----- --ppEHaN6d+D5nipX1--