All of lore.kernel.org
 help / color / mirror / Atom feed
From: Petr Machata <me@pmachata.org>
To: netdev@vger.kernel.org, dsahern@gmail.com, stephen@networkplumber.org
Cc: Po.Liu@nxp.com, toke@toke.dk, dave.taht@gmail.com,
	edumazet@google.com, tahiliani@nitk.edu.in, leon@kernel.org,
	Petr Machata <me@pmachata.org>
Subject: [PATCH iproute2-next v2 7/7] lib: Move get_size() from tc here
Date: Sat,  5 Dec 2020 22:13:35 +0100	[thread overview]
Message-ID: <a9c329b19046144dbe06359d8aaeab8db84b9f41.1607201857.git.me@pmachata.org> (raw)
In-Reply-To: <cover.1607201857.git.me@pmachata.org>

The function get_size() serves for parsing of sizes using a handly notation
that supports units and their prefixes, such as 10Kbit. This will be useful
for the DCB buffer size parsing. Move the function from TC to the general
library, so that it can be reused.

Signed-off-by: Petr Machata <me@pmachata.org>
---
 include/utils.h |  1 +
 lib/utils.c     | 35 +++++++++++++++++++++++++++++++++++
 tc/tc_util.c    | 35 -----------------------------------
 tc/tc_util.h    |  1 -
 4 files changed, 36 insertions(+), 36 deletions(-)

diff --git a/include/utils.h b/include/utils.h
index e2073844f2ef..1704392525a2 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -164,6 +164,7 @@ 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 get_size(unsigned int *size, 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);
diff --git a/lib/utils.c b/lib/utils.c
index 1237ae40246c..de875639c608 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -592,6 +592,41 @@ int get_rate64(__u64 *rate, const char *str)
 	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;
+}
+
 static void set_address_type(inet_prefix *addr)
 {
 	switch (addr->family) {
diff --git a/tc/tc_util.c b/tc/tc_util.c
index 3a133ad84ff9..48065897cee7 100644
--- a/tc/tc_util.c
+++ b/tc/tc_util.c
@@ -258,41 +258,6 @@ char *sprint_ticks(__u32 ticks, char *buf)
 	return sprint_time(tc_core_tick2time(ticks), buf);
 }
 
-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;
-}
-
 int get_size_and_cell(unsigned int *size, int *cell_log, char *str)
 {
 	char *slash = strchr(str, '/');
diff --git a/tc/tc_util.h b/tc/tc_util.h
index 675fb34269f6..b197bcdd7b80 100644
--- a/tc/tc_util.h
+++ b/tc/tc_util.h
@@ -78,7 +78,6 @@ struct filter_util *get_filter_kind(const char *str);
 int get_qdisc_handle(__u32 *h, const char *str);
 int get_percent_rate(unsigned int *rate, const char *str, const char *dev);
 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);
 int get_linklayer(unsigned int *val, const char *arg);
 
-- 
2.25.1


  parent reply	other threads:[~2020-12-05 21:15 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-05 21:13 [PATCH iproute2-next v2 0/7] Move rate and size parsing and output to lib Petr Machata
2020-12-05 21:13 ` [PATCH iproute2-next v2 1/7] Move the use_iec declaration to the tools Petr Machata
2020-12-05 21:13 ` [PATCH iproute2-next v2 2/7] lib: Move print_rate() from tc here; modernize Petr Machata
2020-12-05 21:13 ` [PATCH iproute2-next v2 3/7] lib: Move sprint_size() from tc here, add print_size() Petr Machata
2020-12-05 21:13 ` [PATCH iproute2-next v2 4/7] lib: sprint_size(): Uncrustify the code a bit Petr Machata
2020-12-05 21:13 ` [PATCH iproute2-next v2 5/7] lib: print_color_rate(): Fix formatting small rates in IEC mode Petr Machata
2020-12-05 21:13 ` [PATCH iproute2-next v2 6/7] lib: Move get_rate(), get_rate64() from tc here Petr Machata
2020-12-05 21:13 ` Petr Machata [this message]
2020-12-09  2:34 ` [PATCH iproute2-next v2 0/7] Move rate and size parsing and output to lib David Ahern

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a9c329b19046144dbe06359d8aaeab8db84b9f41.1607201857.git.me@pmachata.org \
    --to=me@pmachata.org \
    --cc=Po.Liu@nxp.com \
    --cc=dave.taht@gmail.com \
    --cc=dsahern@gmail.com \
    --cc=edumazet@google.com \
    --cc=leon@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=stephen@networkplumber.org \
    --cc=tahiliani@nitk.edu.in \
    --cc=toke@toke.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.