ipv4: Hash-based multipath routing
Subsystems (5)
cover ipv4: Hash-based multipath routing
This patch series enhances the IPv4 multipath code, adding support for hash-based multipath.
The multipath algorithm is a per-route attribute (RTA_MP_ALGO) with some degree of binary compatibility with the old implementation (2.6.12 - 2.6.22), but without source level compatibility since attributes have different names:
RT_MP_ALG_L3_HASH:
L3 hash-based distribution. This was IP_MP_ALG_NONE, which with the route cache behaved somewhat like L3-based distribution. This is now the default.
RT_MP_ALG_PER_PACKET:
Per-packet distribution. Was IP_MP_ALG_RR. Uses round-robin.
RT_MP_ALG_DRR, RT_MP_ALG_RANDOM, RT_MP_ALG_WRANDOM:
Unsupported values, but reserved because they existed in 2.6.12 - 2.6.22.
RT_MP_ALG_L4_HASH:
L4 hash-based distribution. This is new.
The traditional modulo approach was replaced by a threshold-based approach, described in RFC 2992. This reduces disruption in case of link failures or route changes.
To better support anycast environments where PMTU usually breaks with multipath, certain ICMP packets are hashed using the header within the payload, ensuring that ICMP packets are routed over the same path as the flow they belong to.
As a side effect, the multipath spinlock was removed and the code got faster. I measured ip_mkroute_input (excl. __mkroute_input) on a Xeon X3350 (2.66GHz) with two paths and L3 hashing:
1 thread:
Before: ~199.8 cycles(tsc)
After: ~75.2 cycles(tsc)
4 threads:
Before: ~393.9 cycles(tsc)
After: ~77.8 cycles(tsc)
If this patch is accepted, a follow-up patch to iproute2 will also be submitted.
Best regards,
Peter Nørlund
Peter Nørlund (3):
ipv4: Lock-less per-packet multipath
ipv4: L3 and L4 hash-based multipath routing
ipv4: ICMP packet inspection for multipath
include/net/ip_fib.h | 10 ++- include/net/route.h | 5 + include/uapi/linux/rtnetlink.h | 14 ++++ net/ipv4/Kconfig | 1 net/ipv4/fib_frontend.c | 4 + net/ipv4/fib_semantics.c | 146 +++++++++++++++++++++++++--------------- net/ipv4/icmp.c | 29 +++++++- net/ipv4/route.c | 108 +++++++++++++++++++++++++++--- net/ipv4/xfrm4_policy.c | 2 - 9 files changed, 246 insertions(+), 73 deletions(-)
A message that is not in the archive (sent off-list or not yet received): 1434571686-5149-1-git-send-email-pch-chEQUL3jiZBWk0Htik3J/[email protected]
PATCH 1/3 ipv4: Lock-less per-packet multipath
The current multipath attempted to be quasi random, but in most cases it behaved just like a round robin balancing. This patch refactors the algorithm to be exactly that and in doing so, avoids the spin lock.
The new design paves the way for hash-based multipath, replacing the modulo with thresholds, minimizing disruption in case of failing paths or route replacements.
--- include/net/ip_fib.h | 6 +-- net/ipv4/Kconfig | 1 + net/ipv4/fib_semantics.c | 116 ++++++++++++++++++++++++++--------------------- 3 files changed, 68 insertions(+), 55 deletions(-)
include/net/ip_fib.h +3 -3
@@ -76,8 +76,8 @@ struct fib_nh {unsigned int nh_flags;unsigned char nh_scope;#ifdef CONFIG_IP_ROUTE_MULTIPATH -int nh_weight; -int nh_power; +int nh_mp_weight; +atomic_t nh_mp_upper_bound;#endif #ifdef CONFIG_IP_ROUTE_CLASSID __u32 nh_tclassid;
@@ -115,7 +115,7 @@ struct fib_info {#define fib_advmss fib_metrics[RTAX_ADVMSS-1]int fib_nhs;#ifdef CONFIG_IP_ROUTE_MULTIPATH -int fib_power; +int fib_mp_weight;#endif struct rcu_head rcu;struct fib_nh fib_nh[0 ];
net/ipv4/Kconfig +1 -0
@@ -81,6 +81,7 @@ config IP_MULTIPLE_TABLES config IP_ROUTE_MULTIPATH bool "IP: equal cost multipath" depends on IP_ADVANCED_ROUTER + select BITREVERSE help Normally, the routing tables specify a single action to be taken in a deterministic manner for a given packet. If you say Y here
net/ipv4/fib_semantics.c +64 -52
202 lines not on this page · raw message
Signature · 1 line
2.1.4
A message that is not in the archive (sent off-list or not yet received): 1434571686-5149-2-git-send-email-pch-chEQUL3jiZBWk0Htik3J/[email protected]
Re: [PATCH net-next 1/3] ipv4: Lock-less per-packet multipath
Quoted 31 lines · Peter Nørlund
The current multipath attempted to be quasi random, but in most cases it behaved just like a round robin balancing. This patch refactors the algorithm to be exactly that and in doing so, avoids the spin lock.
The new design paves the way for hash-based multipath, replacing the modulo with thresholds, minimizing disruption in case of failing paths or route replacements.
--- include/net/ip_fib.h | 6 +-- net/ipv4/Kconfig | 1 + net/ipv4/fib_semantics.c | 116 ++++++++++++++++++++++++++--------------------- 3 files changed, 68 insertions(+), 55 deletions(-)include/net/ip_fib.h +2
-2@@ -76,8 +76,8 @@ struct fib_nh {unsigned int nh_flags;unsigned char nh_scope;#ifdef CONFIG_IP_ROUTE_MULTIPATH -int nh_weight; -int nh_power; +int nh_mp_weight; +atomic_t nh_mp_upper_bound;#endif #ifdef CONFIG_IP_ROUTE_CLASSID __u32 nh_tclassid;
include/net/ip_fib.h struct fib_info { · line 121
Quoted 8 lines
include/net/ip_fib.h +1
-1@@ -115,7 +115,7 @@ struct fib_info {#define fib_advmss fib_metrics[RTAX_ADVMSS-1]int fib_nhs;#ifdef CONFIG_IP_ROUTE_MULTIPATH -int fib_power; +int fib_mp_weight;#endif struct rcu_head rcu;struct fib_nh fib_nh[0 ];
I could do without some of this renaming. For example you could probably not bother with adding the _mp piece to the name. That way we don't have to track all the nh_weight -> nh_mp_weight changes. Also you could probably just use the name fib_weight since not including the _mp was already the convention for the multipath portions of the structure anyway.
This isn't really improving readability at all so I would say don't bother renaming it.
Quoted 43 lines
net/ipv4/Kconfig +1
-0@@ -81,6 +81,7 @@ config IP_MULTIPLE_TABLES config IP_ROUTE_MULTIPATH bool "IP: equal cost multipath" depends on IP_ADVANCED_ROUTER + select BITREVERSE help Normally, the routing tables specify a single action to be taken in a deterministic manner for a given packet. If you say Y herenet/ipv4/fib_semantics.c +3
-2@@ -15,6 +15,7 @@#include <asm/uaccess.h> #include <linux/bitops.h> +#include <linux/bitrev.h> #include <linux/types.h> #include <linux/kernel.h> #include <linux/jiffies.h> @@ -57,7 +58,7 @@ static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];#ifdef CONFIG_IP_ROUTE_MULTIPATH -static DEFINE_SPINLOCK (fib_multipath_lock); +static DEFINE_PER_CPU(u8, fib_mp_rr_counter);#define for_nexthops (fi) { \ int nhsel; const struct fib_nh *nh; \@@ -261,7 +262,7 @@ static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi) nh->nh_gw != onh->nh_gw || nh->nh_scope != onh->nh_scope ||#ifdef CONFIG_IP_ROUTE_MULTIPATH - nh->nh_weight != onh->nh_weight || + nh->nh_mp_weight != onh->nh_mp_weight ||#endif #ifdef CONFIG_IP_ROUTE_CLASSID nh->nh_tclassid != onh->nh_tclassid ||
net/ipv4/fib_semantics.c static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining) · line 452
Quoted 3 lines
net/ipv4/fib_semantics.c +0
-0@@ -449,6 +450,43 @@ static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining)return remaining >0 ?0 : nhs; }
This is a good example. If we don't do the rename we don't have to review changes like the one above which just add extra overhead to the patch.
Quoted 12 lines
net/ipv4/fib_semantics.c +12
-0+static void fib_rebalance (struct fib_info *fi) +{ +int factor; +int total; +int w; + +if (fi->fib_nhs <2 ) +return ; + + total =0 ; +for_nexthops (fi) { +if (!(nh->nh_flags &RTNH_F_DEAD ))
net/ipv4/fib_semantics.c static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining) · line 474
Quoted 10 lines
net/ipv4/fib_semantics.c +10
-0+ total += nh->nh_mp_weight; + }endfor_nexthops (fi); + +if (likely (total !=0 )) { + factor =DIV_ROUND_UP (total,8388608 ); + total /= factor; + }else { + factor =1 ; + } +
So where does the 8388608 value come from? Is it just here to help restrict the upper_bound to a u8 value?
net/ipv4/fib_semantics.c static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining) · line 484
Quoted 10 lines
net/ipv4/fib_semantics.c +10
-0+ w =0 ; +change_nexthops (fi) { +int upper_bound; + +if (nexthop_nh->nh_flags &RTNH_F_DEAD ) { + upper_bound =-1 ; + }else { + w += nexthop_nh->nh_mp_weight / factor; + upper_bound =DIV_ROUND_CLOSEST (256 * w, total); + }
This is doing some confusing stuff. I assume the whole point is to get the value to convert the upper_bound into a u8 value based on the weight where you end with 256 as the last value, do I have that right?
I'm not really sure that is the best way to go from a code standpoint since fib_nhs is an int which technically means we could support more than 256 next hops for a given route.
Also I assume that it is safe to divide by total because the assumption is that if one or more routes are not dead then total has a non-zero value, do I have that right? If so what is the point of setting the factor to 1 above, unless you are just hacking around a divide by 0 error.
Maybe what you should do above is simply return if total == 0 since it doesn't look like you can safely update anything otherwise.
Quoted 18 lines
net/ipv4/fib_semantics.c +6
-1+ +atomic_set (&nexthop_nh->nh_mp_upper_bound, upper_bound); + }endfor_nexthops (fi); +} +static int fib_get_nhs (struct fib_info *fi,struct rtnexthop *rtnh,int remaining,struct fib_config *cfg) {@@ -461,7 +499,7 @@ static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh, nexthop_nh->nh_flags = (cfg->fc_flags & ~0xFF ) | rtnh->rtnh_flags; nexthop_nh->nh_oif = rtnh->rtnh_ifindex; - nexthop_nh->nh_weight = rtnh->rtnh_hops +1 ; + nexthop_nh->nh_mp_weight = rtnh->rtnh_hops +1 ; attrlen =rtnh_attrlen (rtnh);if (attrlen >0 ) {
net/ipv4/fib_semantics.c struct fib_info *fib_create_info(struct fib_config *cfg) · line 928
Quoted 8 lines
net/ipv4/fib_semantics.c +1
-1@@ -884,7 +922,7 @@ struct fib_info *fib_create_info(struct fib_config *cfg) fi->fib_net->ipv4.fib_num_tclassid_users++;#endif #ifdef CONFIG_IP_ROUTE_MULTIPATH - nh->nh_weight =1 ; + nh->nh_mp_weight =1 ;#endif }
More unnecessary rename noise.
Quoted 117 lines
net/ipv4/fib_semantics.c +19
-46@@ -936,8 +974,15 @@ struct fib_info *fib_create_info(struct fib_config *cfg)change_nexthops (fi) {fib_info_update_nh_saddr (net, nexthop_nh); +#ifdef CONFIG_IP_ROUTE_MULTIPATH + fi->fib_mp_weight += nexthop_nh->nh_mp_weight; +#endif } endfor_nexthops(fi) +#ifdef CONFIG_IP_ROUTE_MULTIPATH +fib_rebalance (fi); +#endif + link_it: ofi =fib_find_info (fi);if (ofi) {@@ -1050,7 +1095,7 @@ int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event, goto nla_put_failure; rtnh->rtnh_flags = nh->nh_flags &0xFF ; - rtnh->rtnh_hops = nh->nh_weight -1 ; + rtnh->rtnh_hops = nh->nh_mp_weight -1 ; rtnh->rtnh_ifindex = nh->nh_oif; if (nh->nh_gw &&@@ -1130,12 +1175,6 @@ int fib_sync_down_dev(struct net_device *dev, int force)else if (nexthop_nh->nh_dev == dev && nexthop_nh->nh_scope != scope) { nexthop_nh->nh_flags |=RTNH_F_DEAD ; -#ifdef CONFIG_IP_ROUTE_MULTIPATH -spin_lock_bh (&fib_multipath_lock); - fi->fib_power -= nexthop_nh->nh_power; - nexthop_nh->nh_power =0 ; -spin_unlock_bh (&fib_multipath_lock); -#endif dead++; }#ifdef CONFIG_IP_ROUTE_MULTIPATH @@ -1149,6 +1188,10 @@ int fib_sync_down_dev(struct net_device *dev, int force) fi->fib_flags |=RTNH_F_DEAD ; ret++; } + +#ifdef CONFIG_IP_ROUTE_MULTIPATH +fib_rebalance (fi); +#endif }return ret;@@ -1254,16 +1297,15 @@ int fib_sync_up(struct net_device *dev) !__in_dev_get_rtnl(dev))continue ; alive++; -spin_lock_bh (&fib_multipath_lock); - nexthop_nh->nh_power =0 ; nexthop_nh->nh_flags &= ~RTNH_F_DEAD ; -spin_unlock_bh (&fib_multipath_lock); }endfor_nexthops (fi) if (alive > 0) { fi->fib_flags &= ~RTNH_F_DEAD ; ret++; } + +fib_rebalance (fi); }return ret;@@ -1276,49 +1318,19 @@ int fib_sync_up(struct net_device *dev)void fib_select_multipath (struct fib_result *res) {struct fib_info *fi = res->fi; -int w; - -spin_lock_bh (&fib_multipath_lock); -if (fi->fib_power <=0 ) { -int power =0 ; -change_nexthops (fi) { -if (!(nexthop_nh->nh_flags &RTNH_F_DEAD )) { - power += nexthop_nh->nh_weight; - nexthop_nh->nh_power = nexthop_nh->nh_weight; - } - }endfor_nexthops (fi); - fi->fib_power = power; -if (power <=0 ) { -spin_unlock_bh (&fib_multipath_lock); -/* Race condition: route has just become dead. */ - res->nh_sel =0 ; -return ; - } - } +u8 w; + w =bitrev8 (this_cpu_inc_return (fib_mp_rr_counter)); -/* w should be random number [0..fi->fib_power-1], -* it is pretty bad approximation. -*/ - - w = jiffies % fi->fib_power; +for_nexthops (fi) { +if (w >=atomic_read (&nh->nh_mp_upper_bound)) +continue ; -change_nexthops (fi) { -if (!(nexthop_nh->nh_flags &RTNH_F_DEAD ) && - nexthop_nh->nh_power) { - w -= nexthop_nh->nh_power; -if (w <=0 ) { - nexthop_nh->nh_power--; - fi->fib_power--; - res->nh_sel = nhsel; -spin_unlock_bh (&fib_multipath_lock); -return ; - } - }
net/ipv4/fib_semantics.c int fib_sync_up(struct net_device *dev) · line 1336
Quoted 10 lines
net/ipv4/fib_semantics.c +3
-2+ res->nh_sel = nhsel; + return; }endfor_nexthops (fi); -/* Race condition: route has just become dead. */ +/* Race condition: route has just become dead */ res->nh_sel =0 ; -spin_unlock_bh (&fib_multipath_lock); }#endif
Instead of doing bitrev8 why not just hash jiffies since that is essentially what you are doing with this counter anyway. You could probably even look at converting from a u8 to something like a u32 which would give you a more pseudo random distribution.
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [PATCH net-next 1/3] ipv4: Lock-less per-packet multipath
Quoted 50 lines · On Thu, 18 Jun 2015 12:42:05 -0700 Alexander Duyck…
Quoted 33 lines · Peter Nørlund
The current multipath attempted to be quasi random, but in most cases it behaved just like a round robin balancing. This patch refactors the algorithm to be exactly that and in doing so, avoids the spin lock.
The new design paves the way for hash-based multipath, replacing the modulo with thresholds, minimizing disruption in case of failing paths or route replacements.
--- include/net/ip_fib.h | 6 +-- net/ipv4/Kconfig | 1 + net/ipv4/fib_semantics.c | 116++++++++++++++++++++++++++--------------------- 3 files changed, 68 insertions(+), 55 deletions(-)
include/net/ip_fib.h +2
-2@@ -76,8 +76,8 @@ struct fib_nh {unsigned int nh_flags;unsigned char nh_scope;#ifdef CONFIG_IP_ROUTE_MULTIPATH -int nh_weight; -int nh_power; +int nh_mp_weight; +atomic_t nh_mp_upper_bound;#endif #ifdef CONFIG_IP_ROUTE_CLASSID __u32 nh_tclassid;include/net/ip_fib.h struct fib_info { · line 121
Quoted 8 lines
include/net/ip_fib.h +1
-1@@ -115,7 +115,7 @@ struct fib_info {#define fib_advmss fib_metrics[RTAX_ADVMSS-1]int fib_nhs;#ifdef CONFIG_IP_ROUTE_MULTIPATH -int fib_power; +int fib_mp_weight;#endif struct rcu_head rcu;struct fib_nh fib_nh[0 ];I could do without some of this renaming. For example you could probably not bother with adding the
_mppiece to the name. That way we don't have to track all thenh_weight->nh_mp_weightchanges. Also you could probably just use the namefib_weightsince not including the_mpwas already the convention for the multipath portions of the structure anyway.
Quoted 3 lines
This isn't really improving readability at all so I would say don't bother renaming it.
Good point. I'll skip the renaming.
Quoted 43 lines
Quoted 43 lines
net/ipv4/Kconfig +1
-0@@ -81,6 +81,7 @@ config IP_MULTIPLE_TABLES config IP_ROUTE_MULTIPATH bool "IP: equal cost multipath" depends on IP_ADVANCED_ROUTER + select BITREVERSE help Normally, the routing tables specify a single action tobe taken in a deterministic manner for a given packet. If you say Y here diff --git
a/net/ipv4/fib_semantics.cb/net/ipv4/fib_semantics.cindex 28ec3c1..8c8df80 100644net/ipv4/fib_semantics.c +2
-1@@ -15,6 +15,7 @@#include <asm/uaccess.h> #include <linux/bitops.h> +#include <linux/bitrev.h> #include <linux/types.h> #include <linux/kernel.h> #include <linux/jiffies.h> @@ -57,7 +58,7 @@ static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE ];#ifdef CONFIG_IP_ROUTE_MULTIPATH -static DEFINE_SPINLOCK (fib_multipath_lock); +static DEFINE_PER_CPU(u8, fib_mp_rr_counter);#define for_nexthops (fi){ \ int nhsel; const
structfib_nh*nh; \ @@ -261,7
+262,7 @@ static inline intnh_comp(const struct fib_info *fi, const struct fib_info *ofi)nh->nh_gw!=onh->nh_gw||nh->nh_scope!=onh->nh_scope|| #ifdefCONFIG_IP_ROUTE_MULTIPATHnet/ipv4/fib_semantics.c +1
-1- nh->nh_weight != onh->nh_weight || + nh->nh_mp_weight != onh->nh_mp_weight ||#endif #ifdef CONFIG_IP_ROUTE_CLASSID nh->nh_tclassid != onh->nh_tclassid ||
Quoted 9 lines
net/ipv4/fib_semantics.c static int fib_count_nexthops(struct rtnexthop · line 452
Quoted 4 lines
net/ipv4/fib_semantics.c +0
-0@@ -449,6 +450,43 @@ static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining) return remaining > 0 ? 0 : nhs; }This is a good example. If we don't do the rename we don't have to review changes like the one above which just add extra overhead to the patch.
Right.
Quoted 17 lines
Quoted 17 lines
net/ipv4/fib_semantics.c +17
-0+static void fib_rebalance (struct fib_info *fi) +{ +int factor; +int total; +int w; + +if (fi->fib_nhs <2 ) +return ; + + total =0 ; +for_nexthops (fi) { +if (!(nh->nh_flags &RTNH_F_DEAD )) + total += nh->nh_mp_weight; + }endfor_nexthops (fi); + +if (likely (total !=0 )) { + factor =DIV_ROUND_UP (total,8388608 );
Quoted 9 lines
net/ipv4/fib_semantics.c static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining) · line 474
Quoted 5 lines
net/ipv4/fib_semantics.c +5
-0+ total /= factor; + }else { + factor =1 ; + } +So where does the 8388608 value come from? Is it just here to help restrict the
upper_boundto a u8 value?
Yes. Although I think it is rare for the weight to be that large, the API supports it, so I'd better make sure nothing weird happens. Or is it too hypothetical? Today, if one were to add two paths, each with a weight above 2^30, the total weight would overflow and become negative. fib_select_multipath would then think the route is dead and always select the first path.
Quoted 7 lines
Quoted 7 lines
net/ipv4/fib_semantics.c +7
-0+ w =0 ; +change_nexthops (fi) { +int upper_bound; + +if (nexthop_nh->nh_flags &RTNH_F_DEAD ) { + upper_bound =-1 ; + }else {
Quoted 9 lines
net/ipv4/fib_semantics.c static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining) · line 484
Quoted 4 lines
net/ipv4/fib_semantics.c +3
-0+ w += nexthop_nh->nh_mp_weight / factor; + upper_bound =DIV_ROUND_CLOSEST (256 * w, total); + }This is doing some confusing stuff. I assume the whole point is to get the value to convert the
upper_boundinto a u8 value based on the weight where you end with 256 as the last value, do I have that right?
Yes - and -1 is used to ensure that dead paths are never selected.
Quoted 4 lines
I'm not really sure that is the best way to go from a code standpoint since
fib_nhsis an int which technically means we could support more than 256 next hops for a given route.
It is true that fib_nhs is an int, but nh_sel is an unsigned char. So although you could technically have more than 256 paths, fib_result would have to be modified to actually support more paths.
Quoted 6 lines
Also I assume that it is safe to divide by total because the assumption is that if one or more routes are not dead then total has a non-zero value, do I have that right? If so what is the point of setting the factor to 1 above, unless you are just hacking around a divide by 0 error.
You are right, it is superfluous. If all paths are dead, we never actually use the factor for anything.
Quoted 19 lines
Maybe what you should do above is simply return if total == 0 since it doesn't look like you can safely update anything otherwise.
Quoted 16 lines
net/ipv4/fib_semantics.c +5
-0+ +atomic_set (&nexthop_nh->nh_mp_upper_bound, upper_bound); + }endfor_nexthops (fi); +} + static int fib_get_nhs(struct fib_info *fi, struct rtnexthop*rtnh, int remaining, struct
fib_config*cfg)
{net/ipv4/fib_semantics.c +0
-0@@ -461,7 +499,7 @@ static int fib_get_nhs(struct fib_info *fi,struct rtnexthop *rtnh,
nexthop_nh->nh_flags=
(cfg->fc_flags& ~0xFF) |rtnh->rtnh_flags;nexthop_nh->nh_oif=rtnh->rtnh_ifindex;net/ipv4/fib_semantics.c +1
-1- nexthop_nh->nh_weight = rtnh->rtnh_hops +1 ; + nexthop_nh->nh_mp_weight = rtnh->rtnh_hops +1 ;
Quoted 13 lines
net/ipv4/fib_semantics.c struct fib_info *fib_create_info(struct · line 928
Quoted 11 lines
attrlen = rtnh_attrlen(rtnh); if (attrlen > 0) {net/ipv4/fib_semantics.c +1
-1@@ -884,7 +922,7 @@ struct fib_info *fib_create_info(struct fib_config *cfg) fi->fib_net->ipv4.fib_num_tclassid_users++;#endif #ifdef CONFIG_IP_ROUTE_MULTIPATH - nh->nh_weight =1 ; + nh->nh_mp_weight =1 ;#endif }More unnecessary rename noise.
Got it.
Quoted 127 lines
Quoted 127 lines
net/ipv4/fib_semantics.c +7
-0@@ -936,8 +974,15 @@ struct fib_info *fib_create_info(struct fib_config *cfg)change_nexthops (fi ) {fib_info_update_nh_saddr (net, nexthop_nh); +#ifdef CONFIG_IP_ROUTE_MULTIPATH + fi->fib_mp_weight += nexthop_nh->nh_mp_weight; +#endif } endfor_nexthops(fi) +#ifdef CONFIG_IP_ROUTE_MULTIPATH +fib_rebalance (fi); +#endif + link_it: ofi =fib_find_info (fi);if (ofi) {net/ipv4/fib_semantics.c +1
-1@@ -1050,7 +1095,7 @@ int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event, goto nla_put_failure; rtnh->rtnh_flags = nh->nh_flags &0xFF ; - rtnh->rtnh_hops = nh->nh_weight -1 ; + rtnh->rtnh_hops = nh->nh_mp_weight -1 ; rtnh->rtnh_ifindex = nh->nh_oif; if (nh->nh_gw &&@@ -1130,12 +1175,6 @@ int fib_sync_down_dev(struct net_device *dev, int force) else if (nexthop_nh->nh_dev == dev && nexthop_nh->nh_scope != scope) { nexthop_nh->nh_flags |=
RTNH_F_DEAD; -#ifdefCONFIG_IP_ROUTE_MULTIPATHnet/ipv4/fib_semantics.c +0
-2-spin_lock_bh (&fib_multipath_lock); - fi->fib_power -=
nexthop_nh->nh_power;net/ipv4/fib_semantics.c +0
-2- nexthop_nh->nh_power =0 ; -
spin_unlock_bh(&fib_multipath_lock); -#endif
dead++;
}
#ifdefCONFIG_IP_ROUTE_MULTIPATHnet/ipv4/fib_semantics.c +6
-18@@ -1149,6 +1188,10 @@ int fib_sync_down_dev(struct net_device *dev, int force) fi->fib_flags |= RTNH_F_DEAD; ret++; } + +#ifdef CONFIG_IP_ROUTE_MULTIPATH +fib_rebalance (fi); +#endif }return ret;@@ -1254,16 +1297,15 @@ int fib_sync_up(struct net_device *dev) !__in_dev_get_rtnl(dev))continue ; alive++; -spin_lock_bh (&fib_multipath_lock); - nexthop_nh->nh_power =0 ; nexthop_nh->nh_flags &= ~RTNH_F_DEAD ; -spin_unlock_bh (&fib_multipath_lock); }endfor_nexthops (fi) if (alive > 0) { fi->fib_flags &= ~RTNH_F_DEAD ; ret++; } + +fib_rebalance (fi); }return ret;@@ -1276,49 +1318,19 @@ int fib_sync_up(struct net_device *dev)void fib_select_multipath (struct fib_result *res) {struct fib_info *fi = res->fi; -int w; - -spin_lock_bh (&fib_multipath_lock); -if (fi->fib_power <=0 ) { -int power =0 ; -change_nexthops (fi) { -if (!(nexthop_nh->nh_flags &RTNH_F_DEAD )) { - power += nexthop_nh->nh_weight; - nexthop_nh->nh_power = nexthop_nh->nh_weight; - } - }endfor_nexthops (fi); - fi->fib_power = power; -if (power <=0 ) { -spin_unlock_bh (&fib_multipath_lock); -/* Race condition: route has just become dead. */
net/ipv4/fib_semantics.c +5
-18- res->nh_sel =0 ; -return ; - } - } +u8 w; + w =bitrev8 (this_cpu_inc_return (fib_mp_rr_counter)); -/* w should be random number [0..fi->fib_power-1], -* it is pretty bad approximation. -*/ - - w = jiffies % fi->fib_power; +for_nexthops (fi) { +if (w >=atomic_read (&nh->nh_mp_upper_bound)) +continue ; -change_nexthops (fi) { -if (!(nexthop_nh->nh_flags &RTNH_F_DEAD ) && - nexthop_nh->nh_power) { - w -= nexthop_nh->nh_power; -if (w <=0 ) { - nexthop_nh->nh_power--; - fi->fib_power--; - res->nh_sel = nhsel; -
spin_unlock_bh(&fib_multipath_lock);net/ipv4/fib_semantics.c +2
-3-return ; - } - } + res->nh_sel = nhsel; +return ; }endfor_nexthops (fi);
Quoted 12 lines
net/ipv4/fib_semantics.c int fib_sync_up(struct net_device *dev) · line 1336
Quoted 7 lines
net/ipv4/fib_semantics.c +1
-2-/* Race condition: route has just become dead. */ +/* Race condition: route has just become dead */ res->nh_sel = 0; - spin_unlock_bh(&fib_multipath_lock); }#endif Instead of doing bitrev8 why not just hash jiffies since that is essentially what you are doing with this counter anyway. You could probably even look at converting from a u8 to something like a u32 which would give you a more pseudo random distribution.
The old algorithm tried to be kinda random by using jiffies, but since jiffies has a relatively low resolution (at best 1kHz), at lot of packets in a row would get the same jiffies value. The code did try to adhere to the weight ratios and as a consequence it really worked like round robin (except at every tick when the jiffies variable would increment and offset the round robin slightly).
Considering that, I thought I'd avoid any false pretenses and just do round robin. But since the new algorithm relies on thresholds and not modulo, the most significant bits are the ones that need to flip at every packet. So by reversing the bit order, I get exactly that. At a slow packet rate this is somewhat costly as the bitrev8 lookup table must be fetched from L3 cache or even memory, but as the packet rate increase, the likelihood of it being in L2 or L1 cache increases and cost go down.
Regarding moving from u8 to larger integers, I chose 8-bit resolution since hashing L3 and L4 headers with XOR into a single byte actually produced fairly good results with 8 bits, while switching to 16 bits was terrible. Considering the purpose of the round robin counter here, the primary advantage of switching to higher resolution would be to better support a total weight above 255.
PATCH 2/3 ipv4: L3 and L4 hash-based multipath routing
In reply to Peter NørlundThis patch adds L3 and L4 hash-based multipath routing, selectable on a per-route basis with the reintroduced RTA_MP_ALGO attribute. The default is now RT_MP_ALG_L3_HASH.
--- include/net/ip_fib.h | 4 ++- include/net/route.h | 5 ++-- include/uapi/linux/rtnetlink.h | 14 ++++++++++- net/ipv4/fib_frontend.c | 4 +++ net/ipv4/fib_semantics.c | 34 ++++++++++++++++++++++--- net/ipv4/icmp.c | 4 +-- net/ipv4/route.c | 56 +++++++++++++++++++++++++++++++++++------- net/ipv4/xfrm4_policy.c | 2 +- 8 files changed, 103 insertions(+), 20 deletions(-)
include/net/ip_fib.h +3 -1
@@ -37,6 +37,7 @@ struct fib_config {u32 fc_flags;u32 fc_priority;__be32 fc_prefsrc; +int fc_mp_alg;struct nlattr *fc_mx;struct rtnexthop *fc_mp;int fc_mx_len;
@@ -116,6 +117,7 @@ struct fib_info {int fib_nhs;#ifdef CONFIG_IP_ROUTE_MULTIPATH int fib_mp_weight; +int fib_mp_alg;#endif struct rcu_head rcu;struct fib_nh fib_nh[0 ];
@@ -308,7 +310,7 @@ int ip_fib_check_default(__be32 gw, struct net_device *dev);int fib_sync_down_dev (struct net_device *dev,int force);int fib_sync_down_addr (struct net *net,__be32 local);int fib_sync_up (struct net_device *dev); -void fib_select_multipath (struct fib_result *res); +void fib_select_multipath (struct fib_result *res,const struct flowi4 *flow);/* Exported by fib_trie.c */ void fib_trie_init (void );
include/net/route.h +3 -2
@@ -110,7 +110,8 @@ struct in_device;int ip_rt_init (void );void rt_cache_flush (struct net *net);void rt_flush_dev (struct net_device *dev); -struct rtable *__ip_route_output_key (struct net *,struct flowi4 *flp); +struct rtable *__ip_route_output_key (struct net *,struct flowi4 *flp, +const struct flowi4 *mp_flow);struct rtable *ip_route_output_flow (struct net *,struct flowi4 *flp,struct sock *sk); struct dst_entry *ipv4_blackhole_route(struct net *net,
@@ -267,7 +268,7 @@ static inline struct rtable *ip_route_connect(struct flowi4 *fl4, sport, dport, sk);if (!dst || !src) { - rt =__ip_route_output_key (net, fl4); + rt =__ip_route_output_key (net, fl4,NULL );if (IS_ERR (rt))return rt;ip_rt_put (rt);
include/uapi/linux/rtnetlink.h +13 -1
@@ -271,6 +271,18 @@ enum rt_scope_t {#define RTM_F_EQUALIZE 0x400/* Multipath equalizer: NI */ #define RTM_F_PREFIX 0x800/* Prefix addresses */ +/* Multipath algorithms */ + +enum rt_mp_alg_t { +RT_MP_ALG_L3_HASH ,/* Was IP_MP_ALG_NONE */ +RT_MP_ALG_PER_PACKET ,/* Was IP_MP_ALG_RR */ +RT_MP_ALG_DRR ,/* not used */ +RT_MP_ALG_RANDOM ,/* not used */ +RT_MP_ALG_WRANDOM ,/* not used */ +RT_MP_ALG_L4_HASH , + __RT_MP_ALG_MAX +}; +/* Reserved table identifiers */ enum rt_class_t {
@@ -301,7 +313,7 @@ enum rtattr_type_t {RTA_FLOW ,RTA_CACHEINFO ,RTA_SESSION ,/* no longer used */ -RTA_MP_ALGO ,/* no longer used */ +RTA_MP_ALGO ,RTA_TABLE ,RTA_MARK , RTA_MFC_STATS,
net/ipv4/fib_frontend.c +4 -0
@@ -590,6 +590,7 @@ const struct nla_policy rtm_ipv4_policy[RTA_MAX + 1] = { [RTA_PREFSRC] = { .type = NLA_U32 }, [RTA_METRICS] = { .type = NLA_NESTED }, [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, + [RTA_MP_ALGO] = { .type = NLA_U32 }, [RTA_FLOW] = { .type = NLA_U32 }, };
@@ -650,6 +651,9 @@ static int rtm_to_fib_config(struct net *net, struct sk_buff *skb, cfg->fc_mp =nla_data (attr); cfg->fc_mp_len =nla_len (attr);break ; +case RTA_MP_ALGO : + cfg->fc_mp_alg =nla_get_u32 (attr); +break ;case RTA_FLOW : cfg->fc_flow =nla_get_u32 (attr);break ;
net/ipv4/fib_semantics.c +30 -4
@@ -257,6 +257,11 @@ static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi) {const struct fib_nh *onh = ofi->fib_nh; +#ifdef CONFIG_IP_ROUTE_MULTIPATH +if (fi->fib_mp_alg != ofi->fib_mp_alg) +return -1 ; +#endif +for_nexthops (fi) { if (nh->nh_oif != onh->nh_oif || nh->nh_gw != onh->nh_gw ||
@@ -896,6 +901,7 @@ struct fib_info *fib_create_info(struct fib_config *cfg)if (cfg->fc_mp) {#ifdef CONFIG_IP_ROUTE_MULTIPATH + fi->fib_mp_alg = cfg->fc_mp_alg; err =fib_get_nhs (fi, cfg->fc_mp, cfg->fc_mp_len, cfg);if (err !=0 ) goto failure;
@@ -1085,6 +1091,10 @@ int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,struct rtnexthop *rtnh;struct nlattr *mp; +if (fi->fib_mp_alg && +nla_put_u32 (skb,RTA_MP_ALGO , fi->fib_mp_alg)) + goto nla_put_failure; + mp =nla_nest_start (skb,RTA_MULTIPATH );if (!mp) goto nla_put_failure;
@@ -1312,15 +1322,31 @@ int fib_sync_up(struct net_device *dev) }/* -* The algorithm is suboptimal, but it provides really -* fair weighted route distribution. +* Compute multipath hash based on 3- or 5-tuple */ -void fib_select_multipath (struct fib_result *res) +static int fib_multipath_hash (const struct fib_result *res, +const struct flowi4 *flow) +{ +u32 hash = flow->saddr ^ flow->daddr; + +if (res->fi->fib_mp_alg ==RT_MP_ALG_L4_HASH && flow->flowi4_proto !=0 ) + hash ^= flow->flowi4_proto ^ flow->fl4_sport ^ flow->fl4_dport; + + hash ^= hash >>16 ; + hash ^= hash >>8 ; +return hash &0xFF ; +} + +void fib_select_multipath (struct fib_result *res,const struct flowi4 *flow) {struct fib_info *fi = res->fi;u8 w; - w =bitrev8 (this_cpu_inc_return (fib_mp_rr_counter)); +if (res->fi->fib_mp_alg ==RT_MP_ALG_PER_PACKET ) { + w =bitrev8 (this_cpu_inc_return (fib_mp_rr_counter)); + }else { + w =fib_multipath_hash (res, flow); + }for_nexthops (fi) {if (w >=atomic_read (&nh->nh_mp_upper_bound))
net/ipv4/icmp.c +2 -2
@@ -459,7 +459,7 @@ static struct rtable *icmp_route_lookup(struct net *net, fl4->fl4_icmp_type = type; fl4->fl4_icmp_code = code;security_skb_classify_flow (skb_in,flowi4_to_flowi (fl4)); - rt =__ip_route_output_key (net, fl4); + rt =__ip_route_output_key (net, fl4,NULL );if (IS_ERR (rt))return rt;
@@ -481,7 +481,7 @@ static struct rtable *icmp_route_lookup(struct net *net, goto relookup_failed;if (inet_addr_type (net, fl4_dec.saddr) ==RTN_LOCAL ) { - rt2 =__ip_route_output_key (net, &fl4_dec); + rt2 =__ip_route_output_key (net, &fl4_dec,NULL );if (IS_ERR (rt2)) err =PTR_ERR (rt2); }else {
net/ipv4/route.c +47 -9
110 lines not on this page · raw message
net/ipv4/xfrm4_policy.c +1 -1
@@ -31,7 +31,7 @@ static struct dst_entry *__xfrm4_dst_lookup(struct net *net, struct flowi4 *fl4,if (saddr) fl4->saddr = saddr->a4; - rt =__ip_route_output_key (net, fl4); + rt =__ip_route_output_key (net, fl4,NULL );if (!IS_ERR (rt))return &rt->dst;
Signature · 1 line
2.1.4
Re: [PATCH net-next 2/3] ipv4: L3 and L4 hash-based multipath routing
Quoted 169 lines · Peter Nørlund
This patch adds L3 and L4 hash-based multipath routing, selectable on a per-route basis with the reintroduced
RTA_MP_ALGOattribute. The default is nowRT_MP_ALG_L3_HASH.Signed-off-by Peter Nørlund <[email protected]>--- include/net/ip_fib.h | 4 ++- include/net/route.h | 5 ++-- include/uapi/linux/rtnetlink.h | 14 ++++++++++- net/ipv4/fib_frontend.c | 4 +++ net/ipv4/fib_semantics.c | 34 ++++++++++++++++++++++--- net/ipv4/icmp.c | 4 +-- net/ipv4/route.c | 56 +++++++++++++++++++++++++++++++++++------- net/ipv4/xfrm4_policy.c | 2 +- 8 files changed, 103 insertions(+), 20 deletions(-)include/net/ip_fib.h +3
-1@@ -37,6 +37,7 @@ struct fib_config {u32 fc_flags;u32 fc_priority;__be32 fc_prefsrc; +int fc_mp_alg;struct nlattr *fc_mx;struct rtnexthop *fc_mp;int fc_mx_len;@@ -116,6 +117,7 @@ struct fib_info {int fib_nhs;#ifdef CONFIG_IP_ROUTE_MULTIPATH int fib_mp_weight; +int fib_mp_alg;#endif struct rcu_head rcu;struct fib_nh fib_nh[0 ];@@ -308,7 +310,7 @@ int ip_fib_check_default(__be32 gw, struct net_device *dev);int fib_sync_down_dev (struct net_device *dev,int force);int fib_sync_down_addr (struct net *net,__be32 local);int fib_sync_up (struct net_device *dev); -void fib_select_multipath (struct fib_result *res); +void fib_select_multipath (struct fib_result *res,const struct flowi4 *flow);/* Exported by fib_trie.c */ void fib_trie_init (void );include/net/route.h +3
-2@@ -110,7 +110,8 @@ struct in_device;int ip_rt_init (void );void rt_cache_flush (struct net *net);void rt_flush_dev (struct net_device *dev); -struct rtable *__ip_route_output_key (struct net *,struct flowi4 *flp); +struct rtable *__ip_route_output_key (struct net *,struct flowi4 *flp, +const struct flowi4 *mp_flow);struct rtable *ip_route_output_flow (struct net *,struct flowi4 *flp,struct sock *sk); struct dst_entry *ipv4_blackhole_route(struct net *net,@@ -267,7 +268,7 @@ static inline struct rtable *ip_route_connect(struct flowi4 *fl4, sport, dport, sk);if (!dst || !src) { - rt =__ip_route_output_key (net, fl4); + rt =__ip_route_output_key (net, fl4,NULL );if (IS_ERR (rt))return rt;ip_rt_put (rt);include/uapi/linux/rtnetlink.h +13
-1@@ -271,6 +271,18 @@ enum rt_scope_t {#define RTM_F_EQUALIZE 0x400/* Multipath equalizer: NI */ #define RTM_F_PREFIX 0x800/* Prefix addresses */ +/* Multipath algorithms */ + +enum rt_mp_alg_t { +RT_MP_ALG_L3_HASH ,/* Was IP_MP_ALG_NONE */ +RT_MP_ALG_PER_PACKET ,/* Was IP_MP_ALG_RR */ +RT_MP_ALG_DRR ,/* not used */ +RT_MP_ALG_RANDOM ,/* not used */ +RT_MP_ALG_WRANDOM ,/* not used */ +RT_MP_ALG_L4_HASH , + __RT_MP_ALG_MAX +}; +/* Reserved table identifiers */ enum rt_class_t {@@ -301,7 +313,7 @@ enum rtattr_type_t {RTA_FLOW ,RTA_CACHEINFO ,RTA_SESSION ,/* no longer used */ -RTA_MP_ALGO ,/* no longer used */ +RTA_MP_ALGO ,RTA_TABLE ,RTA_MARK , RTA_MFC_STATS,net/ipv4/fib_frontend.c +4
-0@@ -590,6 +590,7 @@ const struct nla_policy rtm_ipv4_policy[RTA_MAX + 1] = { [RTA_PREFSRC] = { .type = NLA_U32 }, [RTA_METRICS] = { .type = NLA_NESTED }, [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, + [RTA_MP_ALGO] = { .type = NLA_U32 }, [RTA_FLOW] = { .type = NLA_U32 }, };@@ -650,6 +651,9 @@ static int rtm_to_fib_config(struct net *net, struct sk_buff *skb, cfg->fc_mp =nla_data (attr); cfg->fc_mp_len =nla_len (attr);break ; +case RTA_MP_ALGO : + cfg->fc_mp_alg =nla_get_u32 (attr); +break ;case RTA_FLOW : cfg->fc_flow =nla_get_u32 (attr);break ;net/ipv4/fib_semantics.c +14
-3@@ -257,6 +257,11 @@ static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi) {const struct fib_nh *onh = ofi->fib_nh; +#ifdef CONFIG_IP_ROUTE_MULTIPATH +if (fi->fib_mp_alg != ofi->fib_mp_alg) +return -1 ; +#endif +for_nexthops (fi) { if (nh->nh_oif != onh->nh_oif || nh->nh_gw != onh->nh_gw ||@@ -896,6 +901,7 @@ struct fib_info *fib_create_info(struct fib_config *cfg)if (cfg->fc_mp) {#ifdef CONFIG_IP_ROUTE_MULTIPATH + fi->fib_mp_alg = cfg->fc_mp_alg; err =fib_get_nhs (fi, cfg->fc_mp, cfg->fc_mp_len, cfg);if (err !=0 ) goto failure;@@ -1085,6 +1091,10 @@ int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,struct rtnexthop *rtnh;struct nlattr *mp; +if (fi->fib_mp_alg && +nla_put_u32 (skb,RTA_MP_ALGO , fi->fib_mp_alg)) + goto nla_put_failure; + mp =nla_nest_start (skb,RTA_MULTIPATH );if (!mp) goto nla_put_failure;@@ -1312,15 +1322,31 @@ int fib_sync_up(struct net_device *dev) }/* -* The algorithm is suboptimal, but it provides really -* fair weighted route distribution. +* Compute multipath hash based on 3- or 5-tuple */ -void fib_select_multipath (struct fib_result *res) +static int fib_multipath_hash (const struct fib_result *res, +const struct flowi4 *flow) +{
net/ipv4/fib_semantics.c int fib_sync_up(struct net_device *dev) · line 1339
Quoted 10 lines
net/ipv4/fib_semantics.c +10
-0+u32 hash = flow->saddr ^ flow->daddr; + +if (res->fi->fib_mp_alg ==RT_MP_ALG_L4_HASH && flow->flowi4_proto !=0 ) + hash ^= flow->flowi4_proto ^ flow->fl4_sport ^ flow->fl4_dport; + + hash ^= hash >>16 ; + hash ^= hash >>8 ; +return hash &0xFF ; +} +
This hash is still far from optimal. Really I think you should look at something such as jhash_3words or the like for mixing up the addresses.
Right now just XORing the values together like you are will end up
with a fairly high collision rate since for example in the case of two endpoints on the same subnet you would lose the subnet as a result of XORing the source and destination addresses. Also you would lose the port data in the case of a protocol using something such as UDP where the source and destination ports might be the same value.
Quoted 87 lines
net/ipv4/fib_semantics.c +6
-1+void fib_select_multipath (struct fib_result *res,const struct flowi4 *flow) {struct fib_info *fi = res->fi;u8 w; - w =bitrev8 (this_cpu_inc_return (fib_mp_rr_counter)); +if (res->fi->fib_mp_alg ==RT_MP_ALG_PER_PACKET ) { + w =bitrev8 (this_cpu_inc_return (fib_mp_rr_counter)); + }else { + w =fib_multipath_hash (res, flow); + }for_nexthops (fi) {if (w >=atomic_read (&nh->nh_mp_upper_bound))net/ipv4/icmp.c +2
-2@@ -459,7 +459,7 @@ static struct rtable *icmp_route_lookup(struct net *net, fl4->fl4_icmp_type = type; fl4->fl4_icmp_code = code;security_skb_classify_flow (skb_in,flowi4_to_flowi (fl4)); - rt =__ip_route_output_key (net, fl4); + rt =__ip_route_output_key (net, fl4,NULL );if (IS_ERR (rt))return rt;@@ -481,7 +481,7 @@ static struct rtable *icmp_route_lookup(struct net *net, goto relookup_failed;if (inet_addr_type (net, fl4_dec.saddr) ==RTN_LOCAL ) { - rt2 =__ip_route_output_key (net, &fl4_dec); + rt2 =__ip_route_output_key (net, &fl4_dec,NULL );if (IS_ERR (rt2)) err =PTR_ERR (rt2); }else {net/ipv4/route.c +11
-4@@ -1006,7 +1006,7 @@ void ipv4_update_pmtu(struct sk_buff *skb, struct net *net, u32 mtu,__build_flow_key (&fl4,NULL , iph, oif,RT_TOS (iph->tos), protocol, mark, flow_flags); - rt =__ip_route_output_key (net, &fl4); + rt =__ip_route_output_key (net, &fl4,NULL );if (!IS_ERR (rt)) {__ip_rt_update_pmtu (rt, &fl4, mtu);ip_rt_put (rt);@@ -1025,7 +1025,7 @@ static void __ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu)if (!fl4.flowi4_mark) fl4.flowi4_mark =IP4_REPLY_MARK (sock_net (sk), skb->mark); - rt =__ip_route_output_key (sock_net (sk), &fl4); + rt =__ip_route_output_key (sock_net (sk), &fl4,NULL );if (!IS_ERR (rt)) {__ip_rt_update_pmtu (rt, &fl4, mtu);ip_rt_put (rt);@@ -1094,7 +1094,7 @@ void ipv4_redirect(struct sk_buff *skb, struct net *net,__build_flow_key (&fl4,NULL , iph, oif,RT_TOS (iph->tos), protocol, mark, flow_flags); - rt =__ip_route_output_key (net, &fl4); + rt =__ip_route_output_key (net, &fl4,NULL );if (!IS_ERR (rt)) {__ip_do_redirect (rt, skb, &fl4, false);ip_rt_put (rt);@@ -1109,7 +1109,7 @@ void ipv4_sk_redirect(struct sk_buff *skb, struct sock *sk)struct rtable *rt;__build_flow_key (&fl4, sk, iph,0 ,0 ,0 ,0 ,0 ); - rt =__ip_route_output_key (sock_net (sk), &fl4); + rt =__ip_route_output_key (sock_net (sk), &fl4,NULL );if (!IS_ERR (rt)) {__ip_do_redirect (rt, skb, &fl4, false);ip_rt_put (rt);@@ -1631,6 +1631,39 @@ out:return err; } +#ifdef CONFIG_IP_ROUTE_MULTIPATH +/* Fill flow key data based on packet for use in multipath routing. */ +static void ip_multipath_flow (const struct sk_buff *skb,struct flowi4 *flow) +{ +const struct iphdr *iph; + + iph =ip_hdr (skb);
net/ipv4/route.c out: · line 1650
Quoted 10 lines
net/ipv4/route.c +10
-0+ + flow->saddr = iph->saddr; + flow->daddr = iph->daddr; + flow->flowi4_proto = iph->protocol; + flow->fl4_sport =0 ; + flow->fl4_dport =0 ; + +if (unlikely (ip_is_fragment (iph))) +return ; +
I'm not sure if checking for fragmentation is enough. For example if this system is routing and received a flow of UDP packets, some fragmented some not then it might end up mixing them over 2 separate next hops since some will include L4 header data and some won't.
As such you may want to have the option to exclude UDP from the protocols listed below.
Quoted 21 lines
net/ipv4/route.c +16
-0+if (iph->protocol ==IPPROTO_TCP || + iph->protocol ==IPPROTO_UDP || + iph->protocol ==IPPROTO_SCTP ) { +__be16 _ports; +const __be16 *ports; + + ports =skb_header_pointer (skb, iph->ihl *4 ,sizeof (_ports), + &_ports); +if (ports) { + flow->fl4_sport = ports[0 ]; + flow->fl4_dport = ports[1 ]; + } + } +} +#endif /* CONFIG_IP_ROUTE_MULTIPATH */ + static int ip_mkroute_input(struct sk_buff *skb, struct fib_result *res, const struct flowi4 *fl4,@@ -1638,8 +1671,12 @@ static int ip_mkroute_input(struct sk_buff *skb, __be32 daddr, __be32 saddr, u32 tos)
net/ipv4/route.c static int ip_mkroute_input(struct sk_buff *skb, · line 1679
Quoted 10 lines
net/ipv4/route.c +6
-2{#ifdef CONFIG_IP_ROUTE_MULTIPATH -if (res->fi && res->fi->fib_nhs >1 ) -fib_select_multipath (res); +if (res->fi && res->fi->fib_nhs >1 ) { +struct flowi4 mp_flow; + +ip_multipath_flow (skb, &mp_flow); +fib_select_multipath (res, &mp_flow); + }
What is the point in populating the mp_flow if you don't know if it is going to be used? You are populating it in ip_multipath_flow, and then you might completely ignore it in fib_select_multipath.
Maybe instead of passing the mp_flow you could instead look at passing a function pointer that would alter the flow for the multipath case instead.
Quoted 45 lines
net/ipv4/route.c +4
-3#endif /* create a routing cache entry */ @@ -2012,7 +2049,8 @@ add:* Major route resolver routine. */ -struct rtable *__ip_route_output_key (struct net *net,struct flowi4 *fl4) +struct rtable *__ip_route_output_key (struct net *net,struct flowi4 *fl4, +const struct flowi4 *mp_flow) {struct net_device *dev_out =NULL ;__u8 tos =RT_FL_TOS (fl4);@@ -2170,7 +2208,7 @@ struct rtable *__ip_route_output_key(struct net *net, struct flowi4 *fl4)#ifdef CONFIG_IP_ROUTE_MULTIPATH if (res.fi->fib_nhs >1 && fl4->flowi4_oif ==0 ) -fib_select_multipath (&res); +fib_select_multipath (&res, (mp_flow ? mp_flow : fl4)); else#endif if (!res.prefixlen &&@@ -2273,7 +2311,7 @@ struct dst_entry *ipv4_blackhole_route(struct net *net, struct dst_entry *dst_orstruct rtable *ip_route_output_flow (struct net *net,struct flowi4 *flp4,struct sock *sk) { -struct rtable *rt =__ip_route_output_key (net, flp4); +struct rtable *rt =__ip_route_output_key (net, flp4,NULL );if (IS_ERR (rt))return rt;net/ipv4/xfrm4_policy.c +1
-1@@ -31,7 +31,7 @@ static struct dst_entry *__xfrm4_dst_lookup(struct net *net, struct flowi4 *fl4,if (saddr) fl4->saddr = saddr->a4; - rt =__ip_route_output_key (net, fl4); + rt =__ip_route_output_key (net, fl4,NULL );if (!IS_ERR (rt))return &rt->dst;
Re: [PATCH net-next 2/3] ipv4: L3 and L4 hash-based multipath routing
Quoted 191 lines · On Thu, 18 Jun 2015 15:52:22 -0700 Alexander Duyck…
Quoted 181 lines · Peter Nørlund
This patch adds L3 and L4 hash-based multipath routing, selectable on a per-route basis with the reintroduced
RTA_MP_ALGOattribute. The default is nowRT_MP_ALG_L3_HASH.Signed-off-by Peter Nørlund <[email protected]>--- include/net/ip_fib.h | 4 ++- include/net/route.h | 5 ++-- include/uapi/linux/rtnetlink.h | 14 ++++++++++- net/ipv4/fib_frontend.c | 4 +++ net/ipv4/fib_semantics.c | 34 ++++++++++++++++++++++--- net/ipv4/icmp.c | 4 +-- net/ipv4/route.c | 56+++++++++++++++++++++++++++++++++++-------
net/ipv4/xfrm4_policy.c| 2 +- 8 files changed, 103
insertions(+), 20 deletions(-)include/net/ip_fib.h +2
-0@@ -37,6 +37,7 @@ struct fib_config {u32 fc_flags;u32 fc_priority;__be32 fc_prefsrc; +int fc_mp_alg;struct nlattr *fc_mx;struct rtnexthop *fc_mp;int fc_mx_len;@@ -116,6 +117,7 @@ struct fib_info {int fib_nhs;#ifdef CONFIG_IP_ROUTE_MULTIPATH int fib_mp_weight; +int fib_mp_alg;#endif struct rcu_head rcu;struct fib_nh fib_nh[0 ];@@ -308,7 +310,7 @@ int ip_fib_check_default(__be32 gw, struct
net_device*dev); intfib_sync_down_dev(struct net_device *dev, int force); intfib_sync_down_addr(struct net *net, __be32 local);
intfib_sync_up(struct net_device *dev);include/net/ip_fib.h +1
-1-void fib_select_multipath (struct fib_result *res); +void fib_select_multipath(struct fib_result *res, const structflowi4 *flow);
/* Exported by fib_trie.c */ void fib_trie_init(void);include/net/route.h +1
-1@@ -110,7 +110,8 @@ struct in_device;int ip_rt_init (void );void rt_cache_flush (struct net *net);void rt_flush_dev (struct net_device *dev); -struct rtable *__ip_route_output_key (struct net *,struct flowi4 *flp); +struct rtable *__ip_route_output_key(struct net *, structflowi4 *flp, +const struct flowi4 *mp_flow); struct rtable *ip_route_output_flow(struct net *, struct flowi4*flp, struct sock *sk);
structdst_entry*ipv4_blackhole_route(struct net *net,include/net/route.h +1
-1@@ -267,7 +268,7 @@ static inline struct rtable *ip_route_connect(struct flowi4 *fl4, sport, dport, sk);if (!dst || !src) { - rt =__ip_route_output_key (net, fl4); + rt =__ip_route_output_key (net, fl4,NULL );if (IS_ERR (rt))return rt;ip_rt_put (rt);(quoted diff) +0
-0
b/include/uapi/linux/rtnetlink.hindex 17fb02f..dff4a72 100644include/uapi/linux/rtnetlink.h +0
-0@@ -271,6 +271,18 @@ enum rt_scope_t {#define RTM_F_EQUALIZE 0x400/* Multipath equalizer: NI */ #define
RTM_F_PREFIX0x800/* Prefix addresses */include/uapi/linux/rtnetlink.h +13
-1+/* Multipath algorithms */ + +enum rt_mp_alg_t { +RT_MP_ALG_L3_HASH ,/* Was IP_MP_ALG_NONE */ +RT_MP_ALG_PER_PACKET ,/* Was IP_MP_ALG_RR */ +RT_MP_ALG_DRR ,/* not used */ +RT_MP_ALG_RANDOM ,/* not used */ +RT_MP_ALG_WRANDOM ,/* not used */ +RT_MP_ALG_L4_HASH , + __RT_MP_ALG_MAX +}; +/* Reserved table identifiers */ enum rt_class_t {@@ -301,7 +313,7 @@ enum rtattr_type_t {RTA_FLOW ,RTA_CACHEINFO ,RTA_SESSION ,/* no longer used */ -RTA_MP_ALGO ,/* no longer used */ +RTA_MP_ALGO ,RTA_TABLE ,RTA_MARK , RTA_MFC_STATS,net/ipv4/fib_frontend.c +5
-0@@ -590,6 +590,7 @@ const struct nla_policy rtm_ipv4_policy[RTA_MAX + 1] = { [RTA_PREFSRC] = { .type = NLA_U32 }, [RTA_METRICS ] = { .type =NLA_NESTED }, [RTA_MULTIPATH ] = { .len =sizeof (struct rtnexthop ) }, + [RTA_MP_ALGO ] = { .type =NLA_U32 }, [RTA_FLOW ] = { .type =NLA_U32 }, };@@ -650,6 +651,9 @@ static int rtm_to_fib_config(struct net *net, struct sk_buff *skb, cfg->fc_mp = nla_data(attr); cfg->fc_mp_len =nla_len (attr);break ; +case RTA_MP_ALGO : + cfg->fc_mp_alg =nla_get_u32 (attr); +break ;case RTA_FLOW : cfg->fc_flow =nla_get_u32 (attr);break ;net/ipv4/fib_semantics.c +6
-0@@ -257,6 +257,11 @@ static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi) { const struct fib_nh *onh = ofi->fib_nh; +#ifdef CONFIG_IP_ROUTE_MULTIPATH +if (fi->fib_mp_alg != ofi->fib_mp_alg) +return -1 ; +#endif +for_nexthops (fi) { if (nh->nh_oif != onh->nh_oif || nh->nh_gw != onh->nh_gw ||@@ -896,6 +901,7 @@ struct fib_info *fib_create_info(struct fib_config *cfg)if (cfg->fc_mp) {#ifdef CONFIG_IP_ROUTE_MULTIPATH + fi->fib_mp_alg = cfg->fc_mp_alg; err =fib_get_nhs (fi, cfg->fc_mp, cfg->fc_mp_len, cfg);if (err !=0 )goto failure;
net/ipv4/fib_semantics.c +12
-3@@ -1085,6 +1091,10 @@ int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event, struct rtnexthop *rtnh;struct nlattr *mp; +if (fi->fib_mp_alg && +nla_put_u32 (skb,RTA_MP_ALGO , fi->fib_mp_alg)) + goto nla_put_failure; + mp =nla_nest_start (skb,RTA_MULTIPATH );if (!mp) goto nla_put_failure;@@ -1312,15 +1322,31 @@ int fib_sync_up(struct net_device *dev) }/* -* The algorithm is suboptimal, but it provides really -* fair weighted route distribution. +* Compute multipath hash based on 3- or 5-tuple */ -void fib_select_multipath(struct fib_result *res) +static int fib_multipath_hash (const struct fib_result *res, +const struct flowi4 *flow) +{ +u32 hash = flow->saddr ^ flow->daddr; + +if (res->fi->fib_mp_alg ==RT_MP_ALG_L4_HASH && flow->flowi4_proto !=0 ) + hash ^= flow->flowi4_proto ^ flow->fl4_sport ^
flow->fl4_dport; +net/ipv4/fib_semantics.c int fib_sync_up(struct net_device *dev) · line 1339
Quoted 5 lines
net/ipv4/fib_semantics.c +5
-0+ hash ^= hash >>16 ; + hash ^= hash >>8 ; +return hash &0xFF ; +} +This hash is still far from optimal. Really I think you should look
Quoted 9 lines
at something such as
jhash_3wordsor the like for mixing up the addresses. Right now just XORing the values together like you are will end up with a fairly high collision rate since for example in the case of two endpoints on the same subnet you would lose the subnet as a result of XORing the source and destination addresses. Also you would lose the port data in the case of a protocol using something such as UDP where the source and destination ports might be the same value.
When I begun to work on the multipath code, I took a look at what the hardware routers did, and found that you could typically choose between XOR, CRC16, and CRC32. CRC32 produces excellent hashes but is slow (unless you take advantage of SSE 4.2), while XOR is fast but less optimal. When I tested the XOR hash on the routers of ordbogen.com (my work place), I was actually surprised to see that the XOR hash was sufficient as long as I stuck with 8 bits. But you are probably right. While HTTP-traffic will likely balance well enough with XOR, other kind of traffic might not. I didn't know the Jenkins hash function, so I haven't tested it, but by the looks of it, I too think it is a good candidate. It also adds the possibility of extending the key space to 16 bits or more.
Quoted 109 lines
Quoted 93 lines
+void
fib_select_multipath(struct fib_result *res, const struct flowi4 *flow){
structfib_info*fi =res->fi;
u8 w;net/ipv4/fib_semantics.c +2
-1- w =bitrev8 (this_cpu_inc_return (fib_mp_rr_counter)); +if (res->fi->fib_mp_alg ==RT_MP_ALG_PER_PACKET ) { + w =bitrev8(this_cpu_inc_return(fib_mp_rr_counter));
net/ipv4/fib_semantics.c +3
-0+ }else { + w =fib_multipath_hash (res, flow); + }for_nexthops (fi) {if (w >=atomic_read (&nh->nh_mp_upper_bound))net/ipv4/icmp.c +2
-2@@ -459,7 +459,7 @@ static struct rtable *icmp_route_lookup(struct net *net, fl4->fl4_icmp_type = type; fl4->fl4_icmp_code = code;security_skb_classify_flow (skb_in,flowi4_to_flowi (fl4)); - rt =__ip_route_output_key (net, fl4); + rt =__ip_route_output_key (net, fl4,NULL );if (IS_ERR (rt))return rt;@@ -481,7 +481,7 @@ static struct rtable *icmp_route_lookup(struct net *net, goto relookup_failed;if (inet_addr_type (net, fl4_dec.saddr) ==RTN_LOCAL ) { - rt2 =__ip_route_output_key (net, &fl4_dec); + rt2 =__ip_route_output_key (net, &fl4_dec,NULL );if (IS_ERR (rt2)) err =PTR_ERR (rt2); }else {net/ipv4/route.c +1
-1@@ -1006,7 +1006,7 @@ void ipv4_update_pmtu(struct sk_buff *skb, struct net *net, u32 mtu, __build_flow_key(&fl4, NULL, iph, oif, RT_TOS(iph->tos), protocol, mark,flow_flags ); - rt =__ip_route_output_key (net, &fl4); + rt =__ip_route_output_key (net, &fl4,NULL );if (!IS_ERR (rt)) {__ip_rt_update_pmtu(rt, &fl4, mtu); ip_rt_put(rt);net/ipv4/route.c +1
-1@@ -1025,7 +1025,7 @@ static void __ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu) if (!fl4.flowi4_mark) fl4.flowi4_mark = IP4_REPLY_MARK(sock_net(sk), skb->mark); - rt =__ip_route_output_key (sock_net (sk), &fl4); + rt =__ip_route_output_key (sock_net (sk), &fl4,NULL );if (!IS_ERR (rt)) {__ip_rt_update_pmtu (rt, &fl4, mtu);ip_rt_put(rt);net/ipv4/route.c +1
-1@@ -1094,7 +1094,7 @@ void ipv4_redirect(struct sk_buff *skb,struct net *net, __build_flow_key(&fl4, NULL, iph, oif, RT_TOS(iph->tos), protocol, mark,flow_flags ); - rt =__ip_route_output_key (net, &fl4); + rt =__ip_route_output_key (net, &fl4,NULL );if (!IS_ERR (rt)) {__ip_do_redirect(rt, skb, &fl4, false); ip_rt_put(rt);net/ipv4/route.c +3
-1@@ -1109,7 +1109,7 @@ void ipv4_sk_redirect(struct sk_buff *skb, struct sock *sk) struct rtable *rt;__build_flow_key (&fl4, sk, iph,0 ,0 ,0 ,0 ,0 ); - rt =__ip_route_output_key (sock_net (sk), &fl4); + rt =__ip_route_output_key (sock_net (sk), &fl4,NULL );if (!IS_ERR (rt)) {__ip_do_redirect (rt, skb, &fl4, false);ip_rt_put (rt);@@ -1631,6 +1631,39 @@ out:return err; } +#ifdef CONFIG_IP_ROUTE_MULTIPATH +/* Fill flow key data based on packet for use in multipath routing. */ +static void
ip_multipath_flow(const struct sk_buff *skb, struct flowi4 *flow)+{net/ipv4/route.c +3
-0+const struct iphdr *iph; + + iph =ip_hdr (skb);net/ipv4/route.c out: · line 1650
Quoted 10 lines
net/ipv4/route.c +10
-0+ + flow->saddr = iph->saddr; + flow->daddr = iph->daddr; + flow->flowi4_proto = iph->protocol; + flow->fl4_sport =0 ; + flow->fl4_dport =0 ; + +if (unlikely (ip_is_fragment (iph))) +return ; +I'm not sure if checking for fragmentation is enough. For example if this system is routing and received a flow of UDP packets, some fragmented some not then it might end up mixing them over 2 separate next hops since some will include L4 header data and some won't.
Quoted 3 lines
As such you may want to have the option to exclude UDP from the protocols listed below.
Just like the hash algorithms, I looked at the hardware routers for inspiration, and they typically do like this. It is a known problem, and it can just as well happen with TCP, so I don't think adding a special case for UDP is the way to go. But what about looking at the don't-fragment-bit? If the DF-bit is set on a packet, it is usually set on all packets in that flow, so one can assume that that flow will never fragment. If it is clear, you can't be sure and should probably avoid L4 hashing altogether. The interesting question is how many flows will be excluded from L4 hashing with this approach.
Quoted 37 lines
Quoted 22 lines
net/ipv4/route.c +7
-0+if (iph->protocol ==IPPROTO_TCP || + iph->protocol ==IPPROTO_UDP || + iph->protocol ==IPPROTO_SCTP ) { +__be16 _ports; +const __be16 *ports; + + ports = skb_header_pointer(skb, iph->ihl * 4,
sizeof(_ports),net/ipv4/route.c +9
-0+ &_ports); +if (ports) { + flow->fl4_sport = ports[0 ]; + flow->fl4_dport = ports[1 ]; + } + } +} +#endif /* CONFIG_IP_ROUTE_MULTIPATH */ + static int ip_mkroute_input(struct sk_buff *skb, struct fib_result *res, const struct flowi4 *fl4,@@ -1638,8 +1671,12 @@ static int ip_mkroute_input(struct sk_buff *skb, __be32 daddr, __be32 saddr, u32 tos)net/ipv4/route.c static int ip_mkroute_input(struct sk_buff · line 1679
Quoted 10 lines
net/ipv4/route.c +6
-2{#ifdef CONFIG_IP_ROUTE_MULTIPATH -if (res->fi && res->fi->fib_nhs >1 ) -fib_select_multipath (res); +if (res->fi && res->fi->fib_nhs >1 ) { +struct flowi4 mp_flow; + +ip_multipath_flow (skb, &mp_flow); +fib_select_multipath (res, &mp_flow); + }What is the point in populating the
mp_flowif you don't know if it is going to be used? You are populating it inip_multipath_flow, and then you might completely ignore it infib_select_multipath.
Quoted 4 lines
Maybe instead of passing the
mp_flowyou could instead look at passing a function pointer that would alter the flow for the multipath case instead.
Definably not a bad idea. It would remove unnecessary overhead when running per-packet multipath.
Quoted 47 lines
Quoted 47 lines
net/ipv4/route.c +2
-2#endif /* create a routing cache entry */ @@ -2012,7 +2049,8 @@ add:* Major route resolver routine. */ -struct rtable *__ip_route_output_key (struct net *net,struct flowi4 *fl4) +struct rtable *__ip_route_output_key(struct net *net,struct flowi4 *fl4, +const struct flowi4 *mp_flow) {struct net_device *dev_out =NULL ;__u8 tos =RT_FL_TOS (fl4);@@ -2170,7 +2208,7 @@ struct rtable *__ip_route_output_key(struct net *net, struct flowi4 *fl4) #ifdef CONFIG_IP_ROUTE_MULTIPATH if (res.fi->fib_nhs > 1 && fl4->flowi4_oif == 0) - fib_select_multipath(&res); + fib_select_multipath(&res, (mp_flow ? mp_flow :fl4)); else
#endif
if (!res.prefixlen &&net/ipv4/route.c +0
-0@@ -2273,7 +2311,7 @@ struct dst_entry *ipv4_blackhole_route(structnet *net, struct
dst_entry*dst_orstruct rtable *ip_route_output_flow(struct net *net, struct flowi4 *flp4, struct sock *sk) {net/ipv4/route.c +1
-1-struct rtable *rt =__ip_route_output_key (net, flp4); +struct rtable *rt =__ip_route_output_key (net, flp4,NULL );if (IS_ERR (rt))return rt;net/ipv4/xfrm4_policy.c +1
-1@@ -31,7 +31,7 @@ static struct dst_entry *__xfrm4_dst_lookup(struct net *net, struct flowi4 *fl4, if (saddr) fl4->saddr = saddr->a4; - rt =__ip_route_output_key (net, fl4); + rt =__ip_route_output_key (net, fl4,NULL );if (!IS_ERR (rt))return &rt->dst;
PATCH 3/3 ipv4: ICMP packet inspection for multipath
In reply to Peter NørlundICMP packets are inspected to let them route together with the flow they belong to, allowing anycast environments to work with ECMP.
--- net/ipv4/icmp.c | 27 ++++++++++++++++++- net/ipv4/route.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 92 insertions(+), 15 deletions(-)
net/ipv4/icmp.c +26 -1
@@ -447,6 +447,7 @@ static struct rtable *icmp_route_lookup(struct net *net, {struct rtable *rt, *rt2;struct flowi4 fl4_dec; +struct flowi4 mp_flow;int err;memset (fl4,0 ,sizeof (*fl4));
@@ -459,7 +460,31 @@ static struct rtable *icmp_route_lookup(struct net *net, fl4->fl4_icmp_type = type; fl4->fl4_icmp_code = code;security_skb_classify_flow (skb_in,flowi4_to_flowi (fl4)); - rt =__ip_route_output_key (net, fl4,NULL ); + +/* Source and destination is swapped. See ip_multipath_flow */ + mp_flow.saddr = iph->daddr; + mp_flow.daddr = iph->saddr; + mp_flow.flowi4_proto = iph->protocol; + mp_flow.fl4_sport =0 ; + mp_flow.fl4_dport =0 ; +if (!ip_is_fragment (iph)) { +if (iph->protocol ==IPPROTO_TCP || + iph->protocol ==IPPROTO_UDP || + iph->protocol ==IPPROTO_SCTP ) { +__be16 _ports[2 ]; +const __be16 *ports; + + ports =skb_header_pointer (skb_in, iph->ihl *4 , +sizeof (_ports), + &_ports); +if (ports) { + mp_flow.fl4_sport = ports[1 ]; + mp_flow.fl4_dport = ports[0 ]; + } + } + } + + rt =__ip_route_output_key (net, fl4, &mp_flow);if (IS_ERR (rt))return rt;
net/ipv4/route.c +66 -14
97 lines not on this page · raw message
Signature · 1 line
2.1.4