mirror of
https://git.kernel.org/pub/scm/network/iproute2/iproute2.git
synced 2024-11-24 02:13:33 +08:00
lib: Move get_rate(), get_rate64() from tc here
The functions get_rate() and get_rate64() are useful for parsing rate-like values. The DCB tool will find these useful in the maxrate subtool. Move them over to lib so that they can be easily reused. Signed-off-by: Petr Machata <me@pmachata.org> Signed-off-by: David Ahern <dsahern@gmail.com>
This commit is contained in:
parent
aaeda2a768
commit
f3be0e6366
@ -162,6 +162,8 @@ int get_be64(__be64 *val, const char *arg, int base);
|
||||
int get_be32(__be32 *val, const char *arg, int base);
|
||||
int get_be16(__be16 *val, const char *arg, int base);
|
||||
int get_addr64(__u64 *ap, const char *cp);
|
||||
int get_rate(unsigned int *rate, const char *str);
|
||||
int get_rate64(__u64 *rate, const char *str);
|
||||
|
||||
int hex2mem(const char *buf, uint8_t *mem, int count);
|
||||
char *hexstring_n2a(const __u8 *str, int len, char *buf, int blen);
|
||||
|
79
lib/utils.c
79
lib/utils.c
@ -513,6 +513,85 @@ int get_addr64(__u64 *ap, const char *cp)
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* See http://physics.nist.gov/cuu/Units/binary.html */
|
||||
static const struct rate_suffix {
|
||||
const char *name;
|
||||
double scale;
|
||||
} suffixes[] = {
|
||||
{ "bit", 1. },
|
||||
{ "Kibit", 1024. },
|
||||
{ "kbit", 1000. },
|
||||
{ "mibit", 1024.*1024. },
|
||||
{ "mbit", 1000000. },
|
||||
{ "gibit", 1024.*1024.*1024. },
|
||||
{ "gbit", 1000000000. },
|
||||
{ "tibit", 1024.*1024.*1024.*1024. },
|
||||
{ "tbit", 1000000000000. },
|
||||
{ "Bps", 8. },
|
||||
{ "KiBps", 8.*1024. },
|
||||
{ "KBps", 8000. },
|
||||
{ "MiBps", 8.*1024*1024. },
|
||||
{ "MBps", 8000000. },
|
||||
{ "GiBps", 8.*1024.*1024.*1024. },
|
||||
{ "GBps", 8000000000. },
|
||||
{ "TiBps", 8.*1024.*1024.*1024.*1024. },
|
||||
{ "TBps", 8000000000000. },
|
||||
{ NULL }
|
||||
};
|
||||
|
||||
int get_rate(unsigned int *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;
|
||||
/* detect if an overflow happened */
|
||||
if (*rate != floor(bps))
|
||||
return -1;
|
||||
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;
|
||||
}
|
||||
|
||||
static void set_address_type(inet_prefix *addr)
|
||||
{
|
||||
switch (addr->family) {
|
||||
|
79
tc/tc_util.c
79
tc/tc_util.c
@ -164,32 +164,6 @@ char *sprint_tc_classid(__u32 h, char *buf)
|
||||
return buf;
|
||||
}
|
||||
|
||||
/* See http://physics.nist.gov/cuu/Units/binary.html */
|
||||
static const struct rate_suffix {
|
||||
const char *name;
|
||||
double scale;
|
||||
} suffixes[] = {
|
||||
{ "bit", 1. },
|
||||
{ "Kibit", 1024. },
|
||||
{ "kbit", 1000. },
|
||||
{ "mibit", 1024.*1024. },
|
||||
{ "mbit", 1000000. },
|
||||
{ "gibit", 1024.*1024.*1024. },
|
||||
{ "gbit", 1000000000. },
|
||||
{ "tibit", 1024.*1024.*1024.*1024. },
|
||||
{ "tbit", 1000000000000. },
|
||||
{ "Bps", 8. },
|
||||
{ "KiBps", 8.*1024. },
|
||||
{ "KBps", 8000. },
|
||||
{ "MiBps", 8.*1024*1024. },
|
||||
{ "MBps", 8000000. },
|
||||
{ "GiBps", 8.*1024.*1024.*1024. },
|
||||
{ "GBps", 8000000000. },
|
||||
{ "TiBps", 8.*1024.*1024.*1024.*1024. },
|
||||
{ "TBps", 8000000000000. },
|
||||
{ NULL }
|
||||
};
|
||||
|
||||
/* Parse a percent e.g: '30%'
|
||||
* return: 0 = ok, -1 = error, 1 = out of range
|
||||
*/
|
||||
@ -273,59 +247,6 @@ int get_percent_rate64(__u64 *rate, const char *str, const char *dev)
|
||||
return get_rate64(rate, r_str);
|
||||
}
|
||||
|
||||
int get_rate(unsigned int *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;
|
||||
/* detect if an overflow happened */
|
||||
if (*rate != floor(bps))
|
||||
return -1;
|
||||
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 tc_print_rate(enum output_type t, const char *key, const char *fmt,
|
||||
unsigned long long rate)
|
||||
{
|
||||
|
@ -76,9 +76,7 @@ struct qdisc_util *get_qdisc_kind(const char *str);
|
||||
struct filter_util *get_filter_kind(const char *str);
|
||||
|
||||
int get_qdisc_handle(__u32 *h, const char *str);
|
||||
int get_rate(unsigned int *rate, const char *str);
|
||||
int get_percent_rate(unsigned int *rate, const char *str, const char *dev);
|
||||
int get_rate64(__u64 *rate, const char *str);
|
||||
int get_percent_rate64(__u64 *rate, const char *str, const char *dev);
|
||||
int get_size(unsigned int *size, const char *str);
|
||||
int get_size_and_cell(unsigned int *size, int *cell_log, char *str);
|
||||
|
Loading…
Reference in New Issue
Block a user