Skip to contentSkip to conversation

inetpeer: ensure to set the maximum tokens the first time

PATCH In review Nicolas Dichtel 3 participants 9 messages Sep 27, 2012

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 inetpeer: ensure to set the maximum tokens the first time

When jiffies wraps around (for example, 5 minutes after the boot, see INITIAL_JIFFIES) and peer has just been created, now - peer->rate_last can be < XRLIM_BURST_FACTOR * timeout, so token is not set to the maximum value, thus some icmp packets can be unexpectedly dropped.

With this patch, it's still possible that last_rate and rate_tokens are 0 at the same time after jiffies wraps round, but the probability is very low and the only consequence is to let some ICMP packets bypass the filter.

---
 net/ipv4/inetpeer.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
net/ipv4/inetpeer.c +7 -3
index e1e0a4e..92fec02 100644
@@ -559,10 +559,14 @@ bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout)
 
 	token = peer->rate_tokens;
 	now = jiffies;
-	token += now - peer->rate_last;
-	peer->rate_last = now;
-	if (token > XRLIM_BURST_FACTOR * timeout)
+	if (!peer->rate_last && !token)
 		token = XRLIM_BURST_FACTOR * timeout;
+	else {
+		token += now - peer->rate_last;
+		if (token > XRLIM_BURST_FACTOR * timeout)
+			token = XRLIM_BURST_FACTOR * timeout;
+	}
+	peer->rate_last = now;
 	if (token >= timeout) {
 		token -= timeout;
 		rc = true;
Signature · 1 line
1.7.12
#1 lore raw
Quoted 26 lines · Nicolas Dichtel

When jiffies wraps around (for example, 5 minutes after the boot, see INITIAL_JIFFIES) and peer has just been created, now - peer->rate_last can be < XRLIM_BURST_FACTOR * timeout, so token is not set to the maximum value, thus some icmp packets can be unexpectedly dropped.

With this patch, it's still possible that last_rate and rate_tokens are 0 at the same time after jiffies wraps round, but the probability is very low and the only consequence is to let some ICMP packets bypass the filter.

---
 net/ipv4/inetpeer.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
net/ipv4/inetpeer.c +1 -3
index e1e0a4e..92fec02 100644
@@ -559,10 +559,14 @@ bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout)
 
 	token = peer->rate_tokens;
 	now = jiffies;
-	token += now - peer->rate_last;
-	peer->rate_last = now;
-	if (token > XRLIM_BURST_FACTOR * timeout)
+	if (!peer->rate_last && !token)

net/ipv4/inetpeer.c bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout) · line 572

Quoted 10 lines
net/ipv4/inetpeer.c +6 -0
 		token = XRLIM_BURST_FACTOR * timeout;
+	else {
+		token += now - peer->rate_last;
+		if (token > XRLIM_BURST_FACTOR * timeout)
+			token = XRLIM_BURST_FACTOR * timeout;
+	}
+	peer->rate_last = now;
 	if (token >= timeout) {
 		token -= timeout;
 		rc = true;

I am sorry I dont understand your patch at all.

Why not init rate_last to a more sensible value ?

net/ipv4/inetpeer.c +1 -1
index e1e0a4e..25ed555 100644
@@ -510,7 +510,7 @@ relookup:
 					secure_ipv6_id(daddr->addr.a6));
 		p->metrics[RTAX_LOCK-1] = INETPEER_METRICS_NEW;
 		p->rate_tokens = 0;
-		p->rate_last = 0;
+		p->rate_last = jiffies;
 		INIT_LIST_HEAD(&p->gc_list);
 
 		/* Link the node. */
#2 lore raw
Quoted 48 lines · Eric Dumazet
Quoted 26 lines · Nicolas Dichtel

When jiffies wraps around (for example, 5 minutes after the boot, see INITIAL_JIFFIES) and peer has just been created, now - peer->rate_last can be < XRLIM_BURST_FACTOR * timeout, so token is not set to the maximum value, thus some icmp packets can be unexpectedly dropped.

With this patch, it's still possible that last_rate and rate_tokens are 0 at the same time after jiffies wraps round, but the probability is very low and the only consequence is to let some ICMP packets bypass the filter.

---
  net/ipv4/inetpeer.c | 10 +++++++---
  1 file changed, 7 insertions(+), 3 deletions(-)
net/ipv4/inetpeer.c +1 -3
index e1e0a4e..92fec02 100644
@@ -559,10 +559,14 @@ bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout)
 
  	token = peer->rate_tokens;
  	now = jiffies;
-	token += now - peer->rate_last;
-	peer->rate_last = now;
-	if (token > XRLIM_BURST_FACTOR * timeout)
+	if (!peer->rate_last && !token)

net/ipv4/inetpeer.c bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout) · line 572

Quoted 10 lines
net/ipv4/inetpeer.c +6 -0
  		token = XRLIM_BURST_FACTOR * timeout;
+	else {
+		token += now - peer->rate_last;
+		if (token > XRLIM_BURST_FACTOR * timeout)
+			token = XRLIM_BURST_FACTOR * timeout;
+	}
+	peer->rate_last = now;
  	if (token >= timeout) {
  		token -= timeout;
  		rc = true;

I am sorry I dont understand your patch at all.

Why not init rate_last to a more sensible value ?

net/ipv4/inetpeer.c +0 -0
index e1e0a4e..25ed555 100644

net/ipv4/inetpeer.c relookup: · line 513

Quoted 5 lines
net/ipv4/inetpeer.c +1 -1
@@ -510,7 +510,7 @@ relookup:
  					secure_ipv6_id(daddr->addr.a6));
  		p->metrics[RTAX_LOCK-1] = INETPEER_METRICS_NEW;
  		p->rate_tokens = 0;
-		p->rate_last = 0;
+		p->rate_last = jiffies;

inet_getpeer(...,1) is called just before inet_peer_xrlim_allow(). So the result in inet_peer_xrlim_allow():
token = peer->rate_tokens; => 0

   	now = jiffies;
	token += now - peer->rate_last; => token += jiffies - jiffies => 0

So we have no token and packet is dropped.

Am I wrong?

#3 lore raw
Quoted 56 lines · Nicolas Dichtel
Quoted 48 lines · Eric Dumazet
Quoted 26 lines · Nicolas Dichtel

When jiffies wraps around (for example, 5 minutes after the boot, see INITIAL_JIFFIES) and peer has just been created, now - peer->rate_last can be < XRLIM_BURST_FACTOR * timeout, so token is not set to the maximum value, thus some icmp packets can be unexpectedly dropped.

With this patch, it's still possible that last_rate and rate_tokens are 0 at the same time after jiffies wraps round, but the probability is very low and the only consequence is to let some ICMP packets bypass the filter.

---
  net/ipv4/inetpeer.c | 10 +++++++---
  1 file changed, 7 insertions(+), 3 deletions(-)
net/ipv4/inetpeer.c +1 -3
index e1e0a4e..92fec02 100644
@@ -559,10 +559,14 @@ bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout)
 
  	token = peer->rate_tokens;
  	now = jiffies;
-	token += now - peer->rate_last;
-	peer->rate_last = now;
-	if (token > XRLIM_BURST_FACTOR * timeout)
+	if (!peer->rate_last && !token)

net/ipv4/inetpeer.c bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout) · line 572

Quoted 10 lines
net/ipv4/inetpeer.c +6 -0
  		token = XRLIM_BURST_FACTOR * timeout;
+	else {
+		token += now - peer->rate_last;
+		if (token > XRLIM_BURST_FACTOR * timeout)
+			token = XRLIM_BURST_FACTOR * timeout;
+	}
+	peer->rate_last = now;
  	if (token >= timeout) {
  		token -= timeout;
  		rc = true;

I am sorry I dont understand your patch at all.

Why not init rate_last to a more sensible value ?

net/ipv4/inetpeer.c +0 -0
index e1e0a4e..25ed555 100644

net/ipv4/inetpeer.c relookup: · line 513

Quoted 5 lines
net/ipv4/inetpeer.c +1 -1
@@ -510,7 +510,7 @@ relookup:
  					secure_ipv6_id(daddr->addr.a6));
  		p->metrics[RTAX_LOCK-1] = INETPEER_METRICS_NEW;
  		p->rate_tokens = 0;
-		p->rate_last = 0;
+		p->rate_last = jiffies;

inet_getpeer(...,1) is called just before inet_peer_xrlim_allow(). So the result in inet_peer_xrlim_allow():

Quoted 6 lines
    	token = peer->rate_tokens; => 0
    	now = jiffies;
	token += now - peer->rate_last; => token += jiffies - jiffies => 0

So we have no token and packet is dropped.

Am I wrong?

So find the right initializer ?

p->rate_last = jiffies;
p->rate_tokens = TOKENS_INIT;

#4 lore raw
Quoted 6 lines · Eric Dumazet

So find the right initializer ?

p->rate_last = jiffies;
p->rate_tokens = TOKENS_INIT;

Or :

p->rate_last = jiffies - 60*HZ;

since the rates_tokens initial value is protocol/family dependent

#5 lore raw
Quoted 9 lines · Eric Dumazet
Quoted 6 lines · Eric Dumazet

So find the right initializer ?

p->rate_last = jiffies;
p->rate_tokens = TOKENS_INIT;

Quoted 5 lines

Or :

p->rate_last = jiffies - 60*HZ;

since the rates_tokens initial value is protocol/family dependent

Yes, this will avoid to pass the timeout arg to inet_getpeer()

#6 lore raw

PATCH v2 1/1 inetpeer: fix token initialization

In reply to Eric Dumazet

When jiffies wraps around (for example, 5 minutes after the boot, see INITIAL_JIFFIES) and peer has just been created, now - peer->rate_last can be < XRLIM_BURST_FACTOR * timeout, so token is not set to the maximum value, thus some icmp packets can be unexpectedly dropped.

Fix this case by initializing last_rate to 60 seconds in the past.

---
v2: fix initialization of peer instead of adding some tests in
inet_peer_xrlim_allow()

 net/ipv4/inetpeer.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
net/ipv4/inetpeer.c +4 -1
index e1e0a4e..c7527f6 100644
@@ -510,7 +510,10 @@ relookup:
 					secure_ipv6_id(daddr->addr.a6));
 		p->metrics[RTAX_LOCK-1] = INETPEER_METRICS_NEW;
 		p->rate_tokens = 0;
-		p->rate_last = 0;
+		/* 60*HZ is arbitrary, but chosen enough high so that the first
+		 * calculation of tokens is at its maximum.
+		 */
+		p->rate_last = jiffies - 60*HZ;
 		INIT_LIST_HEAD(&p->gc_list);
 
 		/* Link the node. */
Signature · 1 line
1.7.12
#7 lore raw

Re: [PATCH v2] inetpeer: fix token initialization

Quoted 21 lines · Nicolas Dichtel

When jiffies wraps around (for example, 5 minutes after the boot, see INITIAL_JIFFIES) and peer has just been created, now - peer->rate_last can be < XRLIM_BURST_FACTOR * timeout, so token is not set to the maximum value, thus some icmp packets can be unexpectedly dropped.

Fix this case by initializing last_rate to 60 seconds in the past.

---
v2: fix initialization of peer instead of adding some tests in
inet_peer_xrlim_allow()

 net/ipv4/inetpeer.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
net/ipv4/inetpeer.c +0 -0
index e1e0a4e..c7527f6 100644
@@ -510,7 +510,10 @@ relookup:
 					secure_ipv6_id(daddr->addr.a6));

net/ipv4/inetpeer.c relookup: · line 519

Quoted 10 lines
net/ipv4/inetpeer.c +4 -1
 		p->metrics[RTAX_LOCK-1] = INETPEER_METRICS_NEW;
 		p->rate_tokens = 0;
-		p->rate_last = 0;
+		/* 60*HZ is arbitrary, but chosen enough high so that the first
+		 * calculation of tokens is at its maximum.
+		 */
+		p->rate_last = jiffies - 60*HZ;
 		INIT_LIST_HEAD(&p->gc_list);
 
 		/* Link the node. */

Thanks !

maintainer Applied
#8 lore raw

Re: [PATCH v2] inetpeer: fix token initialization

From: Eric Dumazet <[email protected]>
Date: Thu, 27 Sep 2012 16:18:36 +0200

Quoted 12 lines · Nicolas Dichtel
Quoted 11 lines

When jiffies wraps around (for example, 5 minutes after the boot, see INITIAL_JIFFIES) and peer has just been created, now - peer->rate_last can be < XRLIM_BURST_FACTOR * timeout, so token is not set to the maximum value, thus some icmp packets can be unexpectedly dropped.

Fix this case by initializing last_rate to 60 seconds in the past.

---
v2: fix initialization of peer instead of adding some tests in
inet_peer_xrlim_allow()

...

Quoted 1 line

Applied, thanks everyone.