From: Stephen Hemminger Date: Fri, 10 Jan 2014 06:44:17 +0000 (-0800) Subject: Merge branch 'master' into net-next-for-3.13 X-Git-Tag: v3.14.0~27^2~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ef056b2190d6fba5a59b373bd4569ebff33597b8;p=thirdparty%2Fiproute2.git Merge branch 'master' into net-next-for-3.13 --- ef056b2190d6fba5a59b373bd4569ebff33597b8 diff --cc tc/tc_util.c index 2f979dfaa,67f69486b..15fb05344 --- a/tc/tc_util.c +++ b/tc/tc_util.c @@@ -171,55 -171,24 +171,49 @@@ int get_rate(unsigned *rate, const cha return 0; } +int get_rate64(__u64 *rate, const char *str) +{ + char *p; + double bps = strtod(str, &p); + const struct rate_suffix *s; + + if (p == str) + return -1; + + for (s = suffixes; s->name; ++s) { + if (strcasecmp(s->name, p) == 0) { + bps *= s->scale; + p += strlen(p); + break; + } + } + + if (*p) + return -1; /* unknown suffix */ + + bps /= 8; /* -> bytes per second */ + *rate = bps; + return 0; +} + void print_rate(char *buf, int len, __u64 rate) { - double tmp = (double)rate*8; extern int use_iec; + unsigned long kilo = use_iec ? 1024 : 1000; + const char *str = use_iec ? "i" : ""; + int i = 0; + static char *units[5] = {"", "K", "M", "G", "T"}; - if (use_iec) { - if (tmp >= 1000.0*1024.0*1024.0*1024.0) - snprintf(buf, len, "%.0fGibit", tmp/(1024.0*1024.0*1024.0)); - else if (tmp >= 1000.0*1024.0*1024.0) - snprintf(buf, len, "%.0fMibit", tmp/(1024.0*1024.0)); - else if (tmp >= 1000.0*1024) - snprintf(buf, len, "%.0fKibit", tmp/1024); - else - snprintf(buf, len, "%.0fbit", tmp); - } else { - if (tmp >= 1000.0*1000000000.0) - snprintf(buf, len, "%.0fGbit", tmp/1000000000.0); - else if (tmp >= 1000.0*1000000.0) - snprintf(buf, len, "%.0fMbit", tmp/1000000.0); - else if (tmp >= 1000.0 * 1000.0) - snprintf(buf, len, "%.0fKbit", tmp/1000.0); - else - snprintf(buf, len, "%.0fbit", tmp); + rate <<= 3; /* bytes/sec -> bits/sec */ + + for (i = 0; i < ARRAY_SIZE(units); i++) { + if (rate < kilo) + break; + if (((rate % kilo) != 0) && rate < 1000*kilo) + break; + rate /= kilo; } + snprintf(buf, len, "%.0f%s%sbit", (double)rate, units[i], str); } char * sprint_rate(__u64 rate, char *buf)