netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH iproute2] tc: Fix rounding in tc_calc_xmittime and tc_calc_xmitsize.
@ 2025-02-05 21:16 Jonathan Lennox
  2025-02-17  6:14 ` Stephen Hemminger
  0 siblings, 1 reply; 29+ messages in thread
From: Jonathan Lennox @ 2025-02-05 21:16 UTC (permalink / raw)
  To: netdev

The logic in tc that converts between sizes and times for a given rate (the
functions tc_calc_xmittime and tc_calc_xmitsize) suffers from double rounding,
with intermediate values getting cast to unsigned int.

As a result, for example, on my test system (where tick_in_usec=15.625,
clock_factor=1, and hz=1000000000) for a bitrate of 1Gbps, all tc htb burst
values between 0 and 999 get encoded as 0; all values between 1000 and 1999
get encoded as 15 (equivalent to 960 bytes); all values between 2000 and 2999
as 31 (1984 bytes); etc.

The attached patch changes this so these calculations are done entirely in
floating-point, and only rounded to integer values when the value is returned.
It also changes tc_calc_xmittime to round its calculated value up, rather than
down, to ensure that the calculated time is actually sufficient for the requested
size.

This is a userspace-only fix to tc; no kernel changes are necessary.

(Please let me know if anything is wrong with this patch, this is my first
time submitting to any Linux kernel mailing lists.)

---
tc/tc_core.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/tc/tc_core.c b/tc/tc_core.c
index 37547e9b..ba8d5bf0 100644
--- a/tc/tc_core.c
+++ b/tc/tc_core.c
@@ -23,11 +23,16 @@
static double tick_in_usec = 1;
static double clock_factor = 1;

-static unsigned int tc_core_time2tick(unsigned int time)
+static double tc_core_time2tick_d(double time)
{
	return time * tick_in_usec;
}

+static double tc_core_tick2time_d(double tick)
+{
+	return tick / tick_in_usec;
+}
+
unsigned int tc_core_tick2time(unsigned int tick)
{
	return tick / tick_in_usec;
@@ -45,12 +50,12 @@ unsigned int tc_core_ktime2time(unsigned int ktime)

unsigned int tc_calc_xmittime(__u64 rate, unsigned int size)
{
-	return tc_core_time2tick(TIME_UNITS_PER_SEC*((double)size/(double)rate));
+	return ceil(tc_core_time2tick_d(TIME_UNITS_PER_SEC*((double)size/(double)rate)));
}

unsigned int tc_calc_xmitsize(__u64 rate, unsigned int ticks)
{
-	return ((double)rate*tc_core_tick2time(ticks))/TIME_UNITS_PER_SEC;
+	return ((double)rate*tc_core_tick2time_d(ticks))/TIME_UNITS_PER_SEC;
}

/*

^ permalink raw reply related	[flat|nested] 29+ messages in thread

end of thread, other threads:[~2025-03-26 19:08 UTC | newest]

Thread overview: 29+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-05 21:16 [PATCH iproute2] tc: Fix rounding in tc_calc_xmittime and tc_calc_xmitsize Jonathan Lennox
2025-02-17  6:14 ` Stephen Hemminger
2025-02-18 20:10   ` Jonathan Lennox
2025-02-18 20:10   ` [PATCH iproute2 v2] " Jonathan Lennox
2025-02-24  3:06     ` David Ahern
2025-02-24 16:36       ` Jonathan Lennox
2025-02-24 16:58         ` David Ahern
2025-02-24 18:42           ` [PATCH iproute2 v3] " Jonathan Lennox
2025-02-26 16:06             ` David Ahern
2025-02-26 18:55               ` Jonathan Lennox
2025-02-26 18:53           ` Jonathan Lennox
2025-02-28 15:50             ` patchwork-bot+netdevbpf
2025-03-03 18:39               ` Pedro Tammela
2025-03-03 19:43                 ` Jonathan Lennox
2025-03-03 20:35                   ` Pedro Tammela
2025-03-03 22:13                     ` Jonathan Lennox
2025-03-04 17:51                       ` Pedro Tammela
2025-03-04 19:38                         ` [PATCH net-next] tc-tests: Update tc police action tests for tc buffer size rounding fixes Jonathan Lennox
2025-03-11  9:16                           ` Paolo Abeni
2025-03-11  9:49                             ` Jakub Kicinski
2025-03-11 11:15                               ` Jamal Hadi Salim
2025-03-12 17:42                                 ` Jonathan Lennox
2025-03-26 11:39                                 ` Jakub Kicinski
2025-03-26 19:04                                   ` Pedro Tammela
2025-03-26 19:07                                     ` Pedro Tammela
2025-03-12 16:47                         ` [PATCH net-next v2] " Jonathan Lennox
2025-03-12 17:48                         ` [PATCH net-next v3] " Jonathan Lennox
2025-03-12 17:48                           ` Jonathan Lennox
2025-03-19 17:50                           ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).