mirror of
https://git.kernel.org/pub/scm/network/iproute2/iproute2.git
synced 2024-11-27 11:54:53 +08:00
1a22ad2721
Since moving get_rate() and get_size() from tc to lib, on some systems we fail to link because of missing math lib. Move the functions that require math lib to their own c file and add -lm to dcb that now use those functions. ../lib/libutil.a(utils.o): In function `get_rate': utils.c:(.text+0x10dc): undefined reference to `floor' ../lib/libutil.a(utils.o): In function `get_size': utils.c:(.text+0x1394): undefined reference to `floor' ../lib/libutil.a(json_print.o): In function `sprint_size': json_print.c:(.text+0x14c0): undefined reference to `rint' json_print.c:(.text+0x14f4): undefined reference to `rint' json_print.c:(.text+0x157c): undefined reference to `rint' Fixes:f3be0e6366
("lib: Move get_rate(), get_rate64() from tc here") Fixes:44396bdfcc
("lib: Move get_size() from tc here") Fixes:adbe5de966
("lib: Move sprint_size() from tc here, add print_size()") Signed-off-by: Roi Dayan <roid@nvidia.com> Reviewed-by: Petr Machata <petrm@nvidia.com> Tested-by: Petr Machata <petrm@nvidia.com> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
124 lines
2.3 KiB
C
124 lines
2.3 KiB
C
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <math.h>
|
|
#include <asm/types.h>
|
|
|
|
#include "utils.h"
|
|
|
|
/* 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;
|
|
}
|
|
|
|
int get_size(unsigned int *size, const char *str)
|
|
{
|
|
double sz;
|
|
char *p;
|
|
|
|
sz = strtod(str, &p);
|
|
if (p == str)
|
|
return -1;
|
|
|
|
if (*p) {
|
|
if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k") == 0)
|
|
sz *= 1024;
|
|
else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g") == 0)
|
|
sz *= 1024*1024*1024;
|
|
else if (strcasecmp(p, "gbit") == 0)
|
|
sz *= 1024*1024*1024/8;
|
|
else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m") == 0)
|
|
sz *= 1024*1024;
|
|
else if (strcasecmp(p, "mbit") == 0)
|
|
sz *= 1024*1024/8;
|
|
else if (strcasecmp(p, "kbit") == 0)
|
|
sz *= 1024/8;
|
|
else if (strcasecmp(p, "b") != 0)
|
|
return -1;
|
|
}
|
|
|
|
*size = sz;
|
|
|
|
/* detect if an overflow happened */
|
|
if (*size != floor(sz))
|
|
return -1;
|
|
|
|
return 0;
|
|
}
|