Skip to contentSkip to conversation

extensions: add xt_statistics random mode translation

PATCH In review Florian Westphal 2 participants 4 messages Dec 1, 2022 netfilter

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 extensions: add xt_statistics random mode translation

Use meta random and bitops to replicate what xt_statistics is doing.

---
 extensions/libxt_statistic.c      | 8 +++++++-
 extensions/libxt_statistic.txlate | 2 +-
 2 files changed, 8 insertions(+), 2 deletions(-)
extensions/libxt_statistic.c +7 -1
index 4f3341a3d116..37915adc8bc3 100644
@@ -141,13 +141,19 @@ static int statistic_xlate(struct xt_xlate *xl,
 
 	switch (info->mode) {
 	case XT_STATISTIC_MODE_RANDOM:
-		return 0;
+		xt_xlate_add(xl, "meta random & %u %s %u",
+			     INT_MAX,
+			     info->flags & XT_STATISTIC_INVERT ? ">=" : "<",
+			     info->u.random.probability);
+		break;
 	case XT_STATISTIC_MODE_NTH:
 		xt_xlate_add(xl, "numgen inc mod %u %s%u",
 			     info->u.nth.every + 1,
 			     info->flags & XT_STATISTIC_INVERT ? "!= " : "",
 			     info->u.nth.packet);
 		break;
+	default:
+		return 0;
 	}
 
 	return 1;
extensions/libxt_statistic.txlate +1 -1
index 3196ff20b90d..627120c598a6 100644
@@ -5,4 +5,4 @@ iptables-translate -A OUTPUT -m statistic --mode nth ! --every 10 --packet 5
 nft 'add rule ip filter OUTPUT numgen inc mod 10 != 5 counter'
 
 iptables-translate -A OUTPUT -m statistic --mode random --probability 0.1
-nft # -A OUTPUT -m statistic --mode random --probability 0.1
+nft 'add rule ip filter OUTPUT meta random & 2147483647 < 214748365 counter'
Signature · 1 line
2.37.4
reviewer
#1 lore raw
Quoted 2 lines · Florian Westphal

Use meta random and bitops to replicate what xt_statistics is doing.

I didn't know about 'meta random', even though it's a bit older than numgen. What's the difference to 'numgen random'? I'm asking because I once tried to fix the same issue using the latter[1], it was never applied, though.

Maybe you could reuse gcd_div() from my patch to reduce nominal values?

Cheers, Phil

[1] https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/

author maintainer
#2 lore raw
Quoted 6 lines · Phil Sutter
Quoted 2 lines · Florian Westphal

Use meta random and bitops to replicate what xt_statistics is doing.

I didn't know about 'meta random', even though it's a bit older than numgen. What's the difference to 'numgen random'?

META_RANDOM is simpler. its really just setting a 32bit register to a 32bit random value.

No modulus, offset or anything like that is supported.

For most users, numgen random is much better because you can generate a random number within a given range.

But this translation really does match exactly what xt_statistics is doing.

Quoted 5 lines

I'm asking because I
once tried to fix the same issue using the latter[1], it was never applied, though.

Maybe you could reuse gcd_div() from my patch to reduce nominal values?

Why? If you prefer numgen, maybe just rebase your patch and push it out?

reviewer
#3 lore raw
Quoted 11 lines · Florian Westphal
Quoted 6 lines · Phil Sutter
Quoted 2 lines · Florian Westphal

Use meta random and bitops to replicate what xt_statistics is doing.

I didn't know about 'meta random', even though it's a bit older than numgen. What's the difference to 'numgen random'?

META_RANDOM is simpler. its really just setting a 32bit register to a 32bit random value.

Quoted 7 lines

No modulus, offset or anything like that is supported.

For most users, numgen random is much better because you can generate a random number within a given range.

But this translation really does match exactly what xt_statistics is doing.

OK, cool!

Quoted 7 lines
Quoted 5 lines

I'm asking because I
once tried to fix the same issue using the latter[1], it was never applied, though.

Maybe you could reuse gcd_div() from my patch to reduce nominal values?

Why? If you prefer numgen, maybe just rebase your patch and push it out?

No, I don't care whether meta or numgen. I just recall some probability/mask values would trim down nicely and make things more readable. Just push your patch please, I'll play with gcd() if time allows.

Thanks, Phil