A bug in team driver
Subsystems (3)
The problem was found on RHEL7.2 but is still present in the latest upstream kernel (according to visual sources inspection).
While using roundrobin runner we have noticed that after sending on team0 about 2.1 billion packets we started seeing 50% packet drop on team0 (according to 'netstat -i'). This number suggested 'signed int' overflow and indeed, inspecting the sources I have noticed the following in
drivers/net/team/team_mode_roundrobin.c
---------------------------------------
struct rr_priv {
unsigned int sent_packets; <--------- unsigned int
};
static struct rr_priv *rr_priv(struct team *team)
{
return (struct rr_priv *) &team->mode_priv;
}static bool rr_transmit(struct team *team, struct sk_buff *skb)
{
struct team_port *port;
int port_index;
port_index = team_num_to_port_index(team,
rr_priv(team)->sent_packets++);---
we have 'unsigned int sent_packets' but we call team_num_to_port_index where 'num' is 'int'
include/linux/if_team.h
-----------------------
static inline int team_num_to_port_index(struct team *team, int num) <-- signed int
{
int en_port_count = ACCESS_ONCE(team->en_port_count);
if (unlikely(!en_port_count))
return 0;
return num % en_port_count;
}As soon as sent_packets becomes larger than MAXINT (=2**31-1), team_num_to_port_index() can return negative number as num becomes negative and remainder (num % en_port_count) is either 0 or negative. This leads to looking up incorrect hash-bucket and dropping packets.
We have easily duplicated this in roundrobin mode with two ports. After reaching 2**31 packets sent on team0 every second packet was dropped.
Rebuilding the kernel after changing
team_num_to_port_index(struct team *team, int num) -> team_num_to_port_index(struct team *team, unsigned int num)
and running the test again does not show packet drop anymore.
The same subroutine is used in team_mode_loadbalance.c:lb_hash_select_tx_port but we pass 'unsigned char hash' to team_num_to_port_index(), so there should be no overflow. I did not test that mode in my tests.
Regards,
Alex
Signature · 4 lines
------------------------------------------------------------------ Alex Sidorenko email: [email protected] ERT Linux Hewlett-Packard Enterprise (Canada) ------------------------------------------------------------------
Quoted 63 lines · Alex Sidorenko
The problem was found on RHEL7.2 but is still present in the latest upstream kernel (according to visual sources inspection).
While using roundrobin runner we have noticed that after sending on team0 about 2.1 billion packets we started seeing 50% packet drop on team0
(according to 'netstat -i'). This number suggested 'signed int' overflow and indeed, inspecting the sources I have noticed the following in
drivers/net/team/team_mode_roundrobin.c
---------------------------------------
structrr_priv{
unsigned intsent_packets; <--------- unsigned
int
};static struct
rr_priv*rr_priv(struct team *team){ return (struct rr_priv *) &team->mode_priv; }static bool
rr_transmit(struct team *team, struct sk_buff *skb){ struct team_port *port; int port_index; port_index = team_num_to_port_index(team, rr_priv(team)->sent_packets++);---
we have 'unsigned int
sent_packets' but we callteam_num_to_port_indexwhere 'num' is 'int'
include/linux/if_team.h
-----------------------
static inline intteam_num_to_port_index(struct team *team, int num)<-- signed int{ int en_port_count = ACCESS_ONCE(team->en_port_count); if (unlikely(!en_port_count)) return 0; return num % en_port_count; }As soon as
sent_packetsbecomes larger than MAXINT (=2**31-1),team_num_to_port_index()can return negative number as num becomes negative and remainder
(num %en_port_count) is either 0 or negative. This leads to looking up incorrect hash-bucket and dropping packets.We have easily duplicated this in roundrobin mode with two ports. After reaching 2**31 packets sent on team0 every second packet was dropped.
Rebuilding the kernel after changing
team_num_to_port_index(struct team *team, int num)->team_num_to_port_index(struct team *team, unsigned int num)
Quoted 6 lines
and running the test again does not show packet drop anymore.
The same subroutine is used in team_mode_loadbalance.c:lb_hash_select_tx_port but we pass 'unsigned char hash' to
team_num_to_port_index(), so there should be no overflow. I did not test that mode in my tests.
Good catch ! Can you send an official patch to fix this ?
Thanks.
Quoted 1 line
PATCH 1/1 Fixing a bug in team driver due to incorrect 'unsigned int' to 'int' conversion
Roundrobin runner of team driver uses 'unsigned int' variable to count the number of sent_packets. Later it is passed to a subroutine team_num_to_port_index(struct team *team, int num) as 'num' and when we reach MAXINT (2**31-1), 'num' becomes negative.
This leads to using incorrect hash-bucket for port lookup and as a result, packets are dropped. The fix consists of changing 'int num' to 'unsigned int num'. Testing of a fixed kernel shows that there is no packet drop anymore.
--- include/linux/if_team.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/if_team.h +1 -1
@@ -245,7 +245,7 @@ static inline struct team_port *team_get_port_by_index(struct team *team,return NULL ; } -static inline int team_num_to_port_index (struct team *team,int num) +static inline int team_num_to_port_index (struct team *team,unsigned int num) {int en_port_count =ACCESS_ONCE (team->en_port_count);
Signature · 10 lines
2.7.4 -- ------------------------------------------------------------------ Alex Sidorenko email: [email protected] ERT Linux Hewlett-Packard Enterprise (Canada) ------------------------------------------------------------------
Re: [PATCH net] Fixing a bug in team driver due to incorrect 'unsigned int' to 'int' conversion
Quoted 10 lines · Alex Sidorenko
Roundrobin runner of team driver uses 'unsigned int' variable to count the number of
sent_packets. Later it is passed to a subroutineteam_num_to_port_index(struct team *team, int num)as 'num' and when we reach MAXINT (2**31-1), 'num' becomes negative.This leads to using incorrect hash-bucket for port lookup and as a result, packets are dropped. The fix consists of changing 'int num' to 'unsigned int num'. Testing of a fixed kernel shows that there is no packet drop anymore.
Signed-off-by Alex Sidorenko <[email protected]>
Note that lines in your changelog are longer than the norm ( Documentation/SubmittingPatches around line 619 )
- The body of the explanation, line wrapped at 75 columns, which will be copied to the permanent changelog to describe this patch.
Otherwise, patch looks, welcome to the club Alex !
David Miller
maintainerRe: [PATCH net] Fixing a bug in team driver due to incorrect 'unsigned int' to 'int' conversion
In reply to Alex SidorenkoFrom: Alex Sidorenko <[email protected]>
Date: Wed, 05 Oct 2016 09:06:04 -0400
Quoted 10 lines
Roundrobin runner of team driver uses 'unsigned int' variable to count the number of
sent_packets. Later it is passed to a subroutineteam_num_to_port_index(struct team *team, int num)as 'num' and when we reach MAXINT (2**31-1), 'num' becomes negative.This leads to using incorrect hash-bucket for port lookup and as a result, packets are dropped. The fix consists of changing 'int num' to 'unsigned int num'. Testing of a fixed kernel shows that there is no packet drop anymore.
Signed-off-by Alex Sidorenko <[email protected]>
This patch has been corrupted by your email client, for example it has transformed TAB charactes into spaces.
Please fix this up, email a test patch to yourself, and only resubmit this patch to the mailing list when you are able to successfully apply the test patch you send to yourself.
Thanks.