From: Phil Sutter <phil@nwl.cc>
To: Pablo Neira Ayuso <pablo@netfilter.org>
Cc: netfilter-devel@vger.kernel.org, Cyril <boite.pour.spam@gmail.com>
Subject: [libnftnl PATCH] expr: limit: Prepare for odd time units
Date: Sat, 13 Apr 2024 05:05:00 +0200 [thread overview]
Message-ID: <20240413030500.15593-1-phil@nwl.cc> (raw)
When limit->unit is not a known timespan, use the largest possible unit
and print the value along with it. This enables libnftnl debug output to
correctly print arbitrary quotients, like '3/5 minutes' for instance.
Link: https://bugzilla.netfilter.org/show_bug.cgi?id=1214
Suggested-by: Cyril <boite.pour.spam@gmail.com>
Signed-off-by: Phil Sutter <phil@nwl.cc>
---
src/expr/limit.c | 50 +++++++++++++++++++++++++++++++++++++-----------
1 file changed, 39 insertions(+), 11 deletions(-)
diff --git a/src/expr/limit.c b/src/expr/limit.c
index 9d025920586b2..5b821081eb20d 100644
--- a/src/expr/limit.c
+++ b/src/expr/limit.c
@@ -158,16 +158,28 @@ nftnl_expr_limit_parse(struct nftnl_expr *e, struct nlattr *attr)
return 0;
}
-static const char *get_unit(uint64_t u)
+static const char *get_time(uint64_t seconds, uint64_t *val)
{
- switch (u) {
- case 1: return "second";
- case 60: return "minute";
- case 60 * 60: return "hour";
- case 60 * 60 * 24: return "day";
- case 60 * 60 * 24 * 7: return "week";
+ static const struct {
+ unsigned int size;
+ const char *name;
+ } units[] = {
+ { 0, "second" },
+ { 60, "minute" },
+ { 60, "hour" },
+ { 24, "day" },
+ { 7, "week" }
+ };
+ int i;
+
+ for (i = 1; i < array_size(units); i++) {
+ if (seconds % units[i].size)
+ break;
+ seconds /= units[i].size;
}
- return "error";
+ if (val)
+ *val = seconds;
+ return units[i - 1].name;
}
static const char *limit_to_type(enum nft_limit_type type)
@@ -186,10 +198,26 @@ nftnl_expr_limit_snprintf(char *buf, size_t len,
uint32_t flags, const struct nftnl_expr *e)
{
struct nftnl_expr_limit *limit = nftnl_expr_data(e);
+ unsigned int offset = 0;
+ const char *time_unit;
+ uint64_t time_val;
+ int ret;
+
+ ret = snprintf(buf, len, "rate %"PRIu64"/", limit->rate);
+ SNPRINTF_BUFFER_SIZE(ret, len, offset);
+
+ time_unit = get_time(limit->unit, &time_val);
+ if (time_val > 1) {
+ ret = snprintf(buf + offset, len, "%"PRIu64" ", time_val);
+ SNPRINTF_BUFFER_SIZE(ret, len, offset);
+ }
+
+ ret = snprintf(buf + offset, len, "%s burst %u type %s flags 0x%x ",
+ time_unit, limit->burst, limit_to_type(limit->type),
+ limit->flags);
+ SNPRINTF_BUFFER_SIZE(ret, len, offset);
- return snprintf(buf, len, "rate %"PRIu64"/%s burst %u type %s flags 0x%x ",
- limit->rate, get_unit(limit->unit), limit->burst,
- limit_to_type(limit->type), limit->flags);
+ return offset;
}
static struct attr_policy limit_attr_policy[__NFTNL_EXPR_LIMIT_MAX] = {
--
2.43.0
next reply other threads:[~2024-04-13 3:05 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-13 3:05 Phil Sutter [this message]
2024-07-09 10:09 ` [libnftnl PATCH] expr: limit: Prepare for odd time units Phil Sutter
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=20240413030500.15593-1-phil@nwl.cc \
--to=phil@nwl.cc \
--cc=boite.pour.spam@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