]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
statement: Avoid rounding bytes in get_rate()
authorElise Lennion <elise.lennion@gmail.com>
Sun, 12 Feb 2017 12:45:34 +0000 (10:45 -0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Sun, 12 Feb 2017 13:52:41 +0000 (14:52 +0100)
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>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
src/statement.c

index 3beb86ab42639ea6dd3b05ae661b7616e736a560..7ffd25f98ea6879a40c47b495e4f1a7f16df896f 100644 (file)
@@ -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];
 }