]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
parser: extend limit syntax
authorJeremy Sowden <jeremy@azazel.net>
Fri, 29 Oct 2021 20:40:09 +0000 (21:40 +0100)
committerPablo Neira Ayuso <pablo@netfilter.org>
Wed, 3 Nov 2021 11:48:19 +0000 (12:48 +0100)
The documentation describes the syntax of limit statements thus:

  limit rate [over] packet_number / TIME_UNIT [burst packet_number packets]
  limit rate [over] byte_number BYTE_UNIT / TIME_UNIT [burst byte_number BYTE_UNIT]

  TIME_UNIT := second | minute | hour | day
  BYTE_UNIT := bytes | kbytes | mbytes

From this one might infer that a limit may be specified by any of the
following:

  limit rate 1048576/second
  limit rate 1048576 mbytes/second

  limit rate 1048576 / second
  limit rate 1048576 mbytes / second

However, the last does not currently parse:

  $ sudo /usr/sbin/nft add filter input limit rate 1048576 mbytes / second
  Error: wrong rate format
  add filter input limit rate 1048576 mbytes / second
                   ^^^^^^^^^^^^^^^^^^^^^^^^^

Extend the `limit_rate_bytes` parser rule to support it, and add some
new Python test-cases.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
src/parser_bison.y
tests/py/any/limit.t
tests/py/any/limit.t.json
tests/py/any/limit.t.payload

index cf1e139d42f39bda3219f9106ca46357f0061fef..65fd35a36cde0ca42eb78f0ac06812817c8c5ba3 100644 (file)
@@ -3268,6 +3268,11 @@ limit_rate_bytes :       NUM     STRING
                                $$.rate = rate * $1;
                                $$.unit = unit;
                        }
+                       |       limit_bytes SLASH time_unit
+                       {
+                               $$.rate = $1;
+                               $$.unit = $3;
+                       }
                        ;
 
 limit_bytes            :       NUM     BYTES           { $$ = $1; }
index 0110e77f2e85d1b97d4cd13fd8b82e34f8a6271c..86e8d43009b9c42a238fad15d6026b924e3c8f3f 100644 (file)
@@ -25,6 +25,11 @@ limit rate 10230 mbytes/second;ok
 limit rate 1023000 mbytes/second;ok
 limit rate 512 kbytes/second burst 5 packets;fail
 
+limit rate 1 bytes / second;ok;limit rate 1 bytes/second
+limit rate 1 kbytes / second;ok;limit rate 1 kbytes/second
+limit rate 1 mbytes / second;ok;limit rate 1 mbytes/second
+limit rate 1 gbytes / second;fail
+
 limit rate 1025 bytes/second burst 512 bytes;ok
 limit rate 1025 kbytes/second burst 1023 kbytes;ok
 limit rate 1025 mbytes/second burst 1025 kbytes;ok
index 8bab7e3d79b4e5d0a8a605ffee7102a8e7926c1e..b41ae60a3bd65c89cf9bcf019fc8247f0ac6f4a7 100644 (file)
     }
 ]
 
+# limit rate 1 bytes / second
+[
+    {
+        "limit": {
+            "burst": 5,
+            "burst_unit": "bytes",
+            "per": "second",
+            "rate": 1,
+            "rate_unit": "bytes"
+        }
+    }
+]
+
+# limit rate 1 kbytes / second
+[
+    {
+        "limit": {
+            "burst": 5,
+            "burst_unit": "bytes",
+            "per": "second",
+            "rate": 1,
+            "rate_unit": "kbytes"
+        }
+    }
+]
+
+# limit rate 1 mbytes / second
+[
+    {
+        "limit": {
+            "burst": 5,
+            "burst_unit": "bytes",
+            "per": "second",
+            "rate": 1,
+            "rate_unit": "mbytes"
+        }
+    }
+]
+
 # limit rate 1025 bytes/second burst 512 bytes
 [
     {
index dc6cea9b284609cb6a1f4d957634a9b0a139c461..3bd85f4ebf45adc884d72b2ac717c6e3afe63389 100644 (file)
@@ -46,6 +46,19 @@ ip test-ip4 output
 ip test-ip4 output
   [ limit rate 1072693248000/second burst 5 type bytes flags 0x0 ]
 
+# limit rate 1 bytes / second
+ip
+  [ limit rate 1/second burst 5 type bytes flags 0x0 ]
+
+# limit rate 1 kbytes / second
+ip
+  [ limit rate 1024/second burst 5 type bytes flags 0x0 ]
+
+# limit rate 1 mbytes / second
+ip
+  [ limit rate 1048576/second burst 5 type bytes flags 0x0 ]
+
+
 # limit rate 1025 bytes/second burst 512 bytes
 ip test-ip4 output
   [ limit rate 1025/second burst 512 type bytes flags 0x0 ]