From 72fc2040f5d96183ae99a2651cf43938d31a4f42 Mon Sep 17 00:00:00 2001 From: "osdl.org!shemminger" Date: Wed, 9 Jun 2004 22:03:03 +0000 Subject: [PATCH] Add more complete rate values including IEC syntax. (Logical change 1.26) --- tc/tc_util.c | 48 +++++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/tc/tc_util.c b/tc/tc_util.c index b533cd22..e4c78823 100644 --- a/tc/tc_util.c +++ b/tc/tc_util.c @@ -98,8 +98,9 @@ char * sprint_tc_classid(__u32 h, char *buf) } /* - * NB: rates are in Kilo = 1000 but sizes are in Kilo = 1024 - * according to standard usage. + * NB: rates are scaled differently depending on bits or bytes. + * if bits are requested then k == 1000 + * for bytes k = 1024 */ int get_rate(unsigned *rate, const char *str) { @@ -109,23 +110,28 @@ int get_rate(unsigned *rate, const char *str) if (p == str) return -1; - if (*p) { - if (strcasecmp(p, "kbps") == 0) - bps *= 1000; - else if (strcasecmp(p, "gbps") == 0) - bps *= 1000000000.; - else if (strcasecmp(p, "gbit") == 0) - bps = (bps * 1000000000.)/8; - else if (strcasecmp(p, "mbps") == 0) - bps *= 1000000.; - else if (strcasecmp(p, "mbit") == 0) - bps = (bps * 1000000.)/8; - else if (strcasecmp(p, "kbit") == 0) - bps = (bps * 1000.) / 8; - else if (strcasecmp(p, "bps") != 0) - return -1; - } else + if (*p == 0 || strcasecmp(p, "bit") == 0) bps /= 8; + else if (strcasecmp(p, "kbit") == 0) + bps = (bps * 1000.) / 8; + else if (strcasecmp(p, "mbit") == 0) + bps = (bps * 1000000.)/8; + else if (strcasecmp(p, "gbit") == 0) + bps = (bps * 1000000000.)/8; + else if (strcasecmp(p, "kibit") == 0) + bps *= 1024 / 8; + else if (strcasecmp(p, "mibit") == 0) + bps *= 1024*1024/8; + else if (strcasecmp(p, "gibit") == 0) + bps *= 1024*1024*1024/8; + else if (strcasecmp(p, "kbps") == 0) + bps *= 1024; + else if (strcasecmp(p, "mbps") == 0) + bps *= 1024*1024; + else if (strcasecmp(p, "gbps") == 0) + bps *= 1024*1024*1024; + else if (strcasecmp(p, "bps") != 0) + return -1; *rate = bps; return 0; @@ -166,11 +172,11 @@ int print_rate(char *buf, int len, __u32 rate) double tmp = (double)rate*8; if (tmp >= 999999 && fabs(1000000.*rint(tmp/1000000.) - tmp) < 1000) - snprintf(buf, len, "%gMbit", rint(tmp/1000000.)); + snprintf(buf, len, "%gmbit", rint(tmp/1000000.)); else if (tmp >= 990 && fabs(1000.*rint(tmp/1000.) - tmp) < 10) - snprintf(buf, len, "%gKbit", rint(tmp/1000.)); + snprintf(buf, len, "%gkbit", rint(tmp/1000.)); else - snprintf(buf, len, "%ubps", rate); + snprintf(buf, len, "%ubit", rate); return 0; }