Skip to contentSkip to conversation

Question about the hashlimit network mask patch

PATCH Jan Engelhardt 4 participants 7 messages Mar 26, 2008 – Apr 2, 2008 5 messages not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#5 lore raw
Quoted 8 lines · Iavor Stoev

xtables-1.5.3 release works flawlessly but I found a strange behavior with srcmask option.

If you want to use /32 as a mask it doesn't work, however /31 works fine:

xt_hashlimit.c 2008-03-25 08:59:26.000000000 -0400 line 471

Quoted 7 lines
xt_hashlimit.c 2008-03-25 08:59:26.000000000 -0400 renamed from xt_hashlimit.c.orig 2008-03-25 08:59:10.000000000 -0400 +1 -1
@@ -466,7 +466,7 @@
 
 static inline __be32 maskl(__be32 a, unsigned int l)
 {
-       return htonl(ntohl(a) & ~(~(u_int32_t)0 >> l));
+       return htonl(ntohl(a) & ~(~(u_int32_t)0 >> (l-1)));
 }
 

I hate this. It's just stupid that x>>32 is not 0, but, surprisingly, x.

This crap is _so_ common that the networking code is *sprinkled* with x==32 exceptions and it happens we still fall into the trap.

So this patch is needed ... but I had submitted one that entirely got rid of such pitfalls anyway ("Deploy a prefix len...") but that was not merged unfrotunately.

====
[NETFILTER]: xt_hashlimit: add workaround for >>32 case

Hardware surprisingly does nothing when a 32-bit right-shift is to be done. Worse yet, compilers do not even work around it.

net/netfilter/xt_hashlimit.c +4 -1
index 5418ce5..1072174 100644
@@ -466,7 +466,10 @@ static inline void rateinfo_recalc(struct dsthash_ent *dh, unsigned long now)
 
 static inline __be32 maskl(__be32 a, unsigned int l)
 {
-	return htonl(ntohl(a) & ~(~(u_int32_t)0 >> l));
+	if (l == 32)
+		return a;
+	else
+		return htonl(ntohl(a) & ~(~(u_int32_t)0 >> l));
 }
 
 #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
#6 lore raw
Quoted 42 lines · Jan Engelhardt
Quoted 9 lines · Iavor Stoev

xtables-1.5.3 release works flawlessly but I found a strange behavior with srcmask option.

If you want to use /32 as a mask it doesn't work, however /31 works fine:

xt_hashlimit.c 2008-03-25 08:59:26.000000000 -0400 line 471

Quoted 7 lines
xt_hashlimit.c 2008-03-25 08:59:26.000000000 -0400 renamed from xt_hashlimit.c.orig 2008-03-25 08:59:10.000000000 -0400 +1 -1
@@ -466,7 +466,7 @@
 
 static inline __be32 maskl(__be32 a, unsigned int l)
 {
-       return htonl(ntohl(a) & ~(~(u_int32_t)0 >> l));
+       return htonl(ntohl(a) & ~(~(u_int32_t)0 >> (l-1)));
 }
 

I hate this. It's just stupid that x>>32 is not 0, but, surprisingly, x.

This crap is _so_ common that the networking code is *sprinkled* with x==32 exceptions and it happens we still fall into the trap.

So this patch is needed ... but I had submitted one that entirely got rid of such pitfalls anyway ("Deploy a prefix len...") but that was not merged unfrotunately.

====
[NETFILTER]: xt_hashlimit: add workaround for >>32 case

Hardware surprisingly does nothing when a 32-bit right-shift is to be done. Worse yet, compilers do not even work around it.

net/netfilter/xt_hashlimit.c +0 -0
index 5418ce5..1072174 100644
@@ -466,7 +466,10 @@ static inline void rateinfo_recalc(struct dsthash_ent *dh, unsigned long now)
 

net/netfilter/xt_hashlimit.c static inline void rateinfo_recalc(struct dsthash_ent *dh, unsigned long now) · line 475

Quoted 10 lines
net/netfilter/xt_hashlimit.c +4 -1
 static inline __be32 maskl(__be32 a, unsigned int l)
 {
-       return htonl(ntohl(a) & ~(~(u_int32_t)0 >> l));
+       if (l == 32)
+               return a;
+       else
+               return htonl(ntohl(a) & ~(~(u_int32_t)0 >> l));
 }
 
 #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)

Hi Jan,

As far as i understand the maskl() function is called on every match. wouldn't it be more appropriate(and faster) if all these checks and bitshifts are done
in userspace by the iptables utility, so the kernel sees only the "prepared" mask ready for AND-ing.
Of course this would require some mask_show() function in iptables to convert it to bit count,
when we want to show the rules.

Regards,
Niki

#7 lore raw
In reply to Jan Engelhardt

net/netfilter/xt_hashlimit.c static inline void rateinfo_recalc(struct dsthash_ent *dh, unsigned long now) · line 472

Quoted 1 line · Jan Engelhardt
net/netfilter/xt_hashlimit.c +1 -0
+		return htonl(ntohl(a) & ~(~(u_int32_t)0 >> l));

BTW, isn't this way faster:

	return a & htonl(~(~(u_int32_t)0 >> l));

-- Michal Miroslaw

#8 lore raw
Quoted 6 lines · Michał Mirosław

net/netfilter/xt_hashlimit.c static inline void rateinfo_recalc(struct dsthash_ent *dh, unsigned long now) · line 472

Quoted 1 line · Jan Engelhardt
net/netfilter/xt_hashlimit.c +1 -0
+		return htonl(ntohl(a) & ~(~(u_int32_t)0 >> l));

BTW, isn't this way faster:

	return a & htonl(~(~(u_int32_t)0 >> l));

Faster (and safe) is, and it was part of a previously suggested patches,

static inline __be32 maskl(__be32 a, unsigned int l)

{
	return a & prefixlen_netmask_map[l].ip;
}

where prefixlen_netmask_map is a precomputed list of mask addresses. --
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#9 lore raw
In reply to Jan Engelhardt
Quoted 4 lines · Jan Engelhardt

[NETFILTER]: xt_hashlimit: add workaround for >>32 case

Hardware surprisingly does nothing when a 32-bit right-shift is to be done. Worse yet, compilers do not even work around it.

Thats because the C standard states that the result is undefined. Anyways, I think this patch is slightly nicer because it gets rid of the double negation and the %32 == 0 special-casing for IPv6.

Do you want to add an ACKed-by?

commit 830213d52cb7a7e003335003bd56bf82d6153dcf

Author: Patrick McHardy <[email protected]>
Date:   Tue Apr 1 14:48:04 2008 +0200

[NETFILTER]: xt_hashlimit: fix mask calculation

Shifts larger than the data type are undefined, don't try to shift an u32 by 32. Also remove some special-casing of bitmasks divisible by 32.

Based on patch by Jan Engelhardt <[email protected]>.

net/netfilter/xt_hashlimit.c +5 -18
index dc29007..40d344b 100644
@@ -466,38 +466,25 @@ static inline void rateinfo_recalc(struct dsthash_ent *dh, unsigned long now)
 
 static inline __be32 maskl(__be32 a, unsigned int l)
 {
-	return htonl(ntohl(a) & ~(~(u_int32_t)0 >> l));
+	return l ? htonl(ntohl(a) & ~0 << (32 - l)) : 0;
 }
 
 #if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
 static void hashlimit_ipv6_mask(__be32 *i, unsigned int p)
 {
 	switch (p) {
-	case 0:
-		i[0] = i[1] = 0;
-		i[2] = i[3] = 0;
-		break;
-	case 1 ... 31:
+	case 0 ... 31:
 		i[0] = maskl(i[0], p);
 		i[1] = i[2] = i[3] = 0;
 		break;
-	case 32:
-		i[1] = i[2] = i[3] = 0;
-		break;
-	case 33 ... 63:
+	case 32 ... 63:
 		i[1] = maskl(i[1], p - 32);
 		i[2] = i[3] = 0;
 		break;
-	case 64:
-		i[2] = i[3] = 0;
-		break;
-	case 65 ... 95:
+	case 64 ... 95:
 		i[2] = maskl(i[2], p - 64);
 		i[3] = 0;
-	case 96:
-		i[3] = 0;
-		break;
-	case 97 ... 127:
+	case 96 ... 127:
 		i[3] = maskl(i[3], p - 96);
 		break;
 	case 128:
#10 lore raw
Quoted 6 lines · Patrick McHardy
Quoted 4 lines · Jan Engelhardt

[NETFILTER]: xt_hashlimit: add workaround for >>32 case

Hardware surprisingly does nothing when a 32-bit right-shift is to be done. Worse yet, compilers do not even work around it.

Quoted 6 lines

Thats because the C standard states that the result is undefined. Anyways, I think this patch is slightly nicer because it gets rid of the double negation and the %32 == 0 special-casing for IPv6.

Do you want to add an ACKed-by?

The special casing for %32==0 in the IPv6 block was not a workaround for '>>32', but speed. Assuming the C code is already perfect and gets transformed 1:1 into assembly without any further optimization, the cost of the operation is 4 simple 'mov' instructions for %32==0, whereas calling maskl() will involve a fair number of operations (at least 27 by the count). [With your patch, maskl() is now 28 + a branch.]

Not that it really matters to me, I submitted patch (no replies yet) that replaces it with the pfxlen static map anyway :->

Quoted 36 lines

#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE) static void hashlimit_ipv6_mask(__be32 *i, unsigned int p)

{
	switch (p) {
net/netfilter/xt_hashlimit.c +1 -5
-	case 0:
-		i[0] = i[1] = 0;
-		i[2] = i[3] = 0;
-		break;
-	case 1 ... 31:
+	case 0 ... 31:
		i[0] = maskl(i[0], p);
		i[1] = i[2] = i[3] = 0;
		break;
net/netfilter/xt_hashlimit.c +1 -4
-	case 32:
-		i[1] = i[2] = i[3] = 0;
-		break;
-	case 33 ... 63:
+	case 32 ... 63:
		i[1] = maskl(i[1], p - 32);
		i[2] = i[3] = 0;
		break;
net/netfilter/xt_hashlimit.c +1 -4
-	case 64:
-		i[2] = i[3] = 0;
-		break;
-	case 65 ... 95:
+	case 64 ... 95:
		i[2] = maskl(i[2], p - 64);
		i[3] = 0;
net/netfilter/xt_hashlimit.c +1 -4
-	case 96:
-		i[3] = 0;
-		break;
-	case 97 ... 127:
+	case 96 ... 127:
		i[3] = maskl(i[3], p - 96);
		break;
	case 128:
#11 lore raw
Quoted 13 lines · Jan Engelhardt
Quoted 11 lines · Patrick McHardy
Quoted 4 lines · Jan Engelhardt

[NETFILTER]: xt_hashlimit: add workaround for >>32 case

Hardware surprisingly does nothing when a 32-bit right-shift is to be done. Worse yet, compilers do not even work around it.

Thats because the C standard states that the result is undefined. Anyways, I think this patch is slightly nicer because it gets rid of the double negation and the %32 == 0 special-casing for IPv6.

Quoted 4 lines
Quoted 1 line

Do you want to add an ACKed-by?

The special casing for %32==0 in the IPv6 block was not a workaround for '>>32', but speed.

I'm aware of that.

Quoted 1 line

Assuming the C code is already perfect and gets transformed 1:1 into assembly without any further optimization, the cost of the operation is 4 simple 'mov' instructions for %32==0, whereas calling maskl() will involve a fair number of operations (at least 27 by the count). [With your patch, maskl() is now 28 + a branch.]

The "optimization" results in bigger code and more branches itself. Just removing it without fixing maskl reduces code size by over 35%:

net/netfilter/xt_hashlimit.c:
hashlimit_ipv6_mask | -103 # 274 -> 171
1 function changed, 103 bytes removed

Fixing maskl bloats it up again, but still a net win:

  hashlimit_ipv6_mask |   -9 # 274 -> 265, size inlines: 71 -> 148
  hashlimit_init_dst  |   +6 # 589 -> 595, size inlines: 86 -> 106

2 functions changed, 6 bytes added, 9 bytes removed, diff: -3

Just the maskl fix results in:

net/netfilter/xt_hashlimit.c:
hashlimit_ipv6_mask | +95 # 274 -> 369, size inlines: 71 -> 143
hashlimit_init_dst | +6 # 589 -> 595, size inlines: 86 -> 106
2 functions changed, 101 bytes added