From a303853e844ce4eb755399f811b0d8a8c71c6b88 Mon Sep 17 00:00:00 2001 From: Eric Dumazet Date: Mon, 3 Jun 2013 05:51:33 +0000 Subject: [PATCH] get_rate: detect 32bit overflows On Mon, 2013-06-03 at 16:36 +0100, Ben Hutchings wrote: > Oops, I read this as being strtol() currently, not strtod(). Currently > '1.5gbit' will work, but this change will break that. So I think you > need to keep bps as a double. Arg > Then here I think the check should be *rate != floor(bps), i.e. accept > rounding down of a non-integer number of bytes but any other change is > assumed to be overflow. Thanks Ben, here is v4 then ;) [PATCH v4] get_rate: detect 32bit overflows Current rate limit is 34.359.738.360 bit per second, and unfortunately 40Gbps links are above it. overflows in get_rate() are currently not detected, and some users are confused. Let's detect this and complain. Note that some qdisc are ready to get extended range, but this will need additional attributes and new iproute2 With help from Ben Hutchings Signed-off-by: Eric Dumazet Reviewed-by: Ben Hutchings --- tc/tc_util.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/tc/tc_util.c b/tc/tc_util.c index 8e62a010..8114c97c 100644 --- a/tc/tc_util.c +++ b/tc/tc_util.c @@ -152,19 +152,23 @@ int get_rate(unsigned *rate, const char *str) if (p == str) return -1; - if (*p == '\0') { - *rate = bps / 8.; /* assume bits/sec */ - return 0; - } - for (s = suffixes; s->name; ++s) { if (strcasecmp(s->name, p) == 0) { - *rate = (bps * s->scale) / 8.; - return 0; + bps *= s->scale; + p += strlen(p); + break; } } - return -1; + if (*p) + return -1; /* unknown suffix */ + + bps /= 8; /* -> bytes per second */ + *rate = bps; + /* detect if an overflow happened */ + if (*rate != floor(bps)) + return -1; + return 0; } void print_rate(char *buf, int len, __u32 rate)