netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Elise Lennion <elise.lennion@gmail.com>
To: pablo@netfilter.org
Cc: netfilter-devel@vger.kernel.org
Subject: [PATCH nft] statement: Avoid rounding bytes in get_rate()
Date: Sun, 12 Feb 2017 10:45:34 -0200	[thread overview]
Message-ID: <20170212124534.GA2981@lennorien.com> (raw)

get_rate() is used to print quotas and limits and currently rounds the
number of bytes:

$ nft add quota filter https-quota 4000 kbytes
$ nft list ruleset
table ip filter {
	quota https-quota {
		3 mbytes
	}
}

This may be a problem when loading your configuration after saving it
with 'list ruleset'. With this patch the values are represented in a
greater unit only when there is no rest in the conversion:

$ nft add quota filter https-quota2 2048 kbytes
$ nft list ruleset
table ip filter {
	quota https-quota {
		4000 kbytes
	}

	quota https-quota2 {
		2 mbytes
	}
}

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
---
 src/statement.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)

diff --git a/src/statement.c b/src/statement.c
index 3beb86a..7ffd25f 100644
--- a/src/statement.c
+++ b/src/statement.c
@@ -300,20 +300,15 @@ static const char *data_unit[] = {
 
 const char *get_rate(uint64_t byte_rate, uint64_t *rate)
 {
-	uint64_t res, prev, rest;
 	int i;
 
-	res = prev = byte_rate;
-	for (i = 0;; i++) {
-		rest = res % 1024;
-		res /= 1024;
-		if (res <= 1 && rest != 0)
+	for (i = 0; data_unit[i + 1] != NULL; i++) {
+		if (byte_rate % 1024)
 			break;
-		if (data_unit[i + 1] == NULL)
-			break;
-		prev = res;
+		byte_rate /= 1024;
 	}
-	*rate = prev;
+
+	*rate = byte_rate;
 	return data_unit[i];
 }
 
-- 
2.7.4


             reply	other threads:[~2017-02-12 12:45 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-12 12:45 Elise Lennion [this message]
2017-02-12 13:56 ` [PATCH nft] statement: Avoid rounding bytes in get_rate() Pablo Neira Ayuso

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=20170212124534.GA2981@lennorien.com \
    --to=elise.lennion@gmail.com \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pablo@netfilter.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).