Skip to contentSkip to conversation

statement: Avoid rounding bytes in get_rate()

PATCH In review Elise Lennion 2 participants 2 messages Feb 12, 2017 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 statement: Avoid rounding bytes in get_rate()

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
	}
}
---
 src/statement.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
src/statement.c +5 -10
index 3beb86a..7ffd25f 100644
@@ -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];
 }
 
Signature · 1 line
2.7.4
#1 lore raw
Quoted 22 lines · Elise Lennion

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
	}
Quoted 4 lines
	quota https-quota2 {
		2 mbytes
	}
}

also applied, thanks Elise.