Skip to content

Commit bf4e0a3

Browse files
Nikolay Aleksandrovdavem330
authored andcommitted
net: ipv4: add support for ECMP hash policy choice
This patch adds support for ECMP hash policy choice via a new sysctl called fib_multipath_hash_policy and also adds support for L4 hashes. The current values for fib_multipath_hash_policy are: 0 - layer 3 (default) 1 - layer 4 If there's an skb hash already set and it matches the chosen policy then it will be used instead of being calculated (currently only for L4). In L3 mode we always calculate the hash due to the ICMP error special case, the flow dissector's field consistentification should handle the address order thus we can remove the address reversals. If the skb is provided we always use it for the hash calculation, otherwise we fallback to fl4, that is if skb is NULL fl4 has to be set. Signed-off-by: Nikolay Aleksandrov <nikolay@cumulusnetworks.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 88997e4 commit bf4e0a3

8 files changed

Lines changed: 100 additions & 60 deletions

File tree

‎Documentation/networking/ip-sysctl.txt‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ fib_multipath_use_neigh - BOOLEAN
7373
0 - disabled
7474
1 - enabled
7575

76+
fib_multipath_hash_policy - INTEGER
77+
Controls which hash policy to use for multipath routes. Only valid
78+
for kernels built with CONFIG_IP_ROUTE_MULTIPATH enabled.
79+
Default: 0 (Layer 3)
80+
Possible values:
81+
0 - Layer 3
82+
1 - Layer 4
83+
7684
route/max_size - INTEGER
7785
Maximum number of routes allowed in the kernel. Increase
7886
this when using large numbers of interfaces and/or routes.

‎include/net/ip_fib.h‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -395,17 +395,13 @@ int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force);
395395
int fib_sync_down_addr(struct net_device *dev, __be32 local);
396396
int fib_sync_up(struct net_device *dev, unsigned int nh_flags);
397397

398-
extern u32 fib_multipath_secret __read_mostly;
399-
400-
static inline int fib_multipath_hash(__be32 saddr, __be32 daddr)
401-
{
402-
return jhash_2words((__force u32)saddr, (__force u32)daddr,
403-
fib_multipath_secret) >> 1;
404-
}
405-
398+
#ifdef CONFIG_IP_ROUTE_MULTIPATH
399+
int fib_multipath_hash(const struct fib_info *fi, const struct flowi4 *fl4,
400+
const struct sk_buff *skb);
401+
#endif
406402
void fib_select_multipath(struct fib_result *res, int hash);
407403
void fib_select_path(struct net *net, struct fib_result *res,
408-
struct flowi4 *fl4, int mp_hash);
404+
struct flowi4 *fl4, const struct sk_buff *skb);
409405

410406
/* Exported by fib_trie.c */
411407
void fib_trie_init(void);

‎include/net/netns/ipv4.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ struct netns_ipv4 {
151151
#endif
152152
#ifdef CONFIG_IP_ROUTE_MULTIPATH
153153
int sysctl_fib_multipath_use_neigh;
154+
int sysctl_fib_multipath_hash_policy;
154155
#endif
155156

156157
unsigned int fib_seq; /* protected by rtnl_mutex */

‎include/net/route.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,13 @@ struct in_device;
113113
int ip_rt_init(void);
114114
void rt_cache_flush(struct net *net);
115115
void rt_flush_dev(struct net_device *dev);
116-
struct rtable *__ip_route_output_key_hash(struct net *, struct flowi4 *flp,
117-
int mp_hash);
116+
struct rtable *__ip_route_output_key_hash(struct net *net, struct flowi4 *flp,
117+
const struct sk_buff *skb);
118118

119119
static inline struct rtable *__ip_route_output_key(struct net *net,
120120
struct flowi4 *flp)
121121
{
122-
return __ip_route_output_key_hash(net, flp, -1);
122+
return __ip_route_output_key_hash(net, flp, NULL);
123123
}
124124

125125
struct rtable *ip_route_output_flow(struct net *, struct flowi4 *flp,

‎net/ipv4/fib_semantics.c‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ static unsigned int fib_info_cnt;
5757
static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
5858

5959
#ifdef CONFIG_IP_ROUTE_MULTIPATH
60-
u32 fib_multipath_secret __read_mostly;
6160

6261
#define for_nexthops(fi) { \
6362
int nhsel; const struct fib_nh *nh; \
@@ -576,9 +575,6 @@ static void fib_rebalance(struct fib_info *fi)
576575

577576
atomic_set(&nexthop_nh->nh_upper_bound, upper_bound);
578577
} endfor_nexthops(fi);
579-
580-
net_get_random_once(&fib_multipath_secret,
581-
sizeof(fib_multipath_secret));
582578
}
583579

584580
static inline void fib_add_weight(struct fib_info *fi,
@@ -1641,7 +1637,7 @@ void fib_select_multipath(struct fib_result *res, int hash)
16411637
#endif
16421638

16431639
void fib_select_path(struct net *net, struct fib_result *res,
1644-
struct flowi4 *fl4, int mp_hash)
1640+
struct flowi4 *fl4, const struct sk_buff *skb)
16451641
{
16461642
bool oif_check;
16471643

@@ -1650,10 +1646,9 @@ void fib_select_path(struct net *net, struct fib_result *res,
16501646

16511647
#ifdef CONFIG_IP_ROUTE_MULTIPATH
16521648
if (res->fi->fib_nhs > 1 && oif_check) {
1653-
if (mp_hash < 0)
1654-
mp_hash = get_hash_from_flowi4(fl4) >> 1;
1649+
int h = fib_multipath_hash(res->fi, fl4, skb);
16551650

1656-
fib_select_multipath(res, mp_hash);
1651+
fib_select_multipath(res, h);
16571652
}
16581653
else
16591654
#endif

‎net/ipv4/icmp.c‎

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -464,22 +464,6 @@ static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)
464464
local_bh_enable();
465465
}
466466

467-
#ifdef CONFIG_IP_ROUTE_MULTIPATH
468-
469-
/* Source and destination is swapped. See ip_multipath_icmp_hash */
470-
static int icmp_multipath_hash_skb(const struct sk_buff *skb)
471-
{
472-
const struct iphdr *iph = ip_hdr(skb);
473-
474-
return fib_multipath_hash(iph->daddr, iph->saddr);
475-
}
476-
477-
#else
478-
479-
#define icmp_multipath_hash_skb(skb) (-1)
480-
481-
#endif
482-
483467
static struct rtable *icmp_route_lookup(struct net *net,
484468
struct flowi4 *fl4,
485469
struct sk_buff *skb_in,
@@ -505,8 +489,7 @@ static struct rtable *icmp_route_lookup(struct net *net,
505489
fl4->flowi4_oif = l3mdev_master_ifindex(skb_dst(skb_in)->dev);
506490

507491
security_skb_classify_flow(skb_in, flowi4_to_flowi(fl4));
508-
rt = __ip_route_output_key_hash(net, fl4,
509-
icmp_multipath_hash_skb(skb_in));
492+
rt = __ip_route_output_key_hash(net, fl4, skb_in);
510493
if (IS_ERR(rt))
511494
return rt;
512495

‎net/ipv4/route.c‎

Lines changed: 70 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,45 +1734,97 @@ static int __mkroute_input(struct sk_buff *skb,
17341734
}
17351735

17361736
#ifdef CONFIG_IP_ROUTE_MULTIPATH
1737-
17381737
/* To make ICMP packets follow the right flow, the multipath hash is
1739-
* calculated from the inner IP addresses in reverse order.
1738+
* calculated from the inner IP addresses.
17401739
*/
1741-
static int ip_multipath_icmp_hash(struct sk_buff *skb)
1740+
static void ip_multipath_l3_keys(const struct sk_buff *skb,
1741+
struct flow_keys *hash_keys)
17421742
{
17431743
const struct iphdr *outer_iph = ip_hdr(skb);
1744-
struct icmphdr _icmph;
1744+
const struct iphdr *inner_iph;
17451745
const struct icmphdr *icmph;
17461746
struct iphdr _inner_iph;
1747-
const struct iphdr *inner_iph;
1747+
struct icmphdr _icmph;
1748+
1749+
hash_keys->addrs.v4addrs.src = outer_iph->saddr;
1750+
hash_keys->addrs.v4addrs.dst = outer_iph->daddr;
1751+
if (likely(outer_iph->protocol != IPPROTO_ICMP))
1752+
return;
17481753

17491754
if (unlikely((outer_iph->frag_off & htons(IP_OFFSET)) != 0))
1750-
goto standard_hash;
1755+
return;
17511756

17521757
icmph = skb_header_pointer(skb, outer_iph->ihl * 4, sizeof(_icmph),
17531758
&_icmph);
17541759
if (!icmph)
1755-
goto standard_hash;
1760+
return;
17561761

17571762
if (icmph->type != ICMP_DEST_UNREACH &&
17581763
icmph->type != ICMP_REDIRECT &&
17591764
icmph->type != ICMP_TIME_EXCEEDED &&
1760-
icmph->type != ICMP_PARAMETERPROB) {
1761-
goto standard_hash;
1762-
}
1765+
icmph->type != ICMP_PARAMETERPROB)
1766+
return;
17631767

17641768
inner_iph = skb_header_pointer(skb,
17651769
outer_iph->ihl * 4 + sizeof(_icmph),
17661770
sizeof(_inner_iph), &_inner_iph);
17671771
if (!inner_iph)
1768-
goto standard_hash;
1772+
return;
1773+
hash_keys->addrs.v4addrs.src = inner_iph->saddr;
1774+
hash_keys->addrs.v4addrs.dst = inner_iph->daddr;
1775+
}
17691776

1770-
return fib_multipath_hash(inner_iph->daddr, inner_iph->saddr);
1777+
/* if skb is set it will be used and fl4 can be NULL */
1778+
int fib_multipath_hash(const struct fib_info *fi, const struct flowi4 *fl4,
1779+
const struct sk_buff *skb)
1780+
{
1781+
struct net *net = fi->fib_net;
1782+
struct flow_keys hash_keys;
1783+
u32 mhash;
17711784

1772-
standard_hash:
1773-
return fib_multipath_hash(outer_iph->saddr, outer_iph->daddr);
1774-
}
1785+
switch (net->ipv4.sysctl_fib_multipath_hash_policy) {
1786+
case 0:
1787+
memset(&hash_keys, 0, sizeof(hash_keys));
1788+
hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
1789+
if (skb) {
1790+
ip_multipath_l3_keys(skb, &hash_keys);
1791+
} else {
1792+
hash_keys.addrs.v4addrs.src = fl4->saddr;
1793+
hash_keys.addrs.v4addrs.dst = fl4->daddr;
1794+
}
1795+
break;
1796+
case 1:
1797+
/* skb is currently provided only when forwarding */
1798+
if (skb) {
1799+
unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
1800+
struct flow_keys keys;
1801+
1802+
/* short-circuit if we already have L4 hash present */
1803+
if (skb->l4_hash)
1804+
return skb_get_hash_raw(skb) >> 1;
1805+
memset(&hash_keys, 0, sizeof(hash_keys));
1806+
skb_flow_dissect_flow_keys(skb, &keys, flag);
1807+
hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src;
1808+
hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst;
1809+
hash_keys.ports.src = keys.ports.src;
1810+
hash_keys.ports.dst = keys.ports.dst;
1811+
hash_keys.basic.ip_proto = keys.basic.ip_proto;
1812+
} else {
1813+
memset(&hash_keys, 0, sizeof(hash_keys));
1814+
hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
1815+
hash_keys.addrs.v4addrs.src = fl4->saddr;
1816+
hash_keys.addrs.v4addrs.dst = fl4->daddr;
1817+
hash_keys.ports.src = fl4->fl4_sport;
1818+
hash_keys.ports.dst = fl4->fl4_dport;
1819+
hash_keys.basic.ip_proto = fl4->flowi4_proto;
1820+
}
1821+
break;
1822+
}
1823+
mhash = flow_hash_from_keys(&hash_keys);
17751824

1825+
return mhash >> 1;
1826+
}
1827+
EXPORT_SYMBOL_GPL(fib_multipath_hash);
17761828
#endif /* CONFIG_IP_ROUTE_MULTIPATH */
17771829

17781830
static int ip_mkroute_input(struct sk_buff *skb,
@@ -1782,12 +1834,8 @@ static int ip_mkroute_input(struct sk_buff *skb,
17821834
{
17831835
#ifdef CONFIG_IP_ROUTE_MULTIPATH
17841836
if (res->fi && res->fi->fib_nhs > 1) {
1785-
int h;
1837+
int h = fib_multipath_hash(res->fi, NULL, skb);
17861838

1787-
if (unlikely(ip_hdr(skb)->protocol == IPPROTO_ICMP))
1788-
h = ip_multipath_icmp_hash(skb);
1789-
else
1790-
h = fib_multipath_hash(saddr, daddr);
17911839
fib_select_multipath(res, h);
17921840
}
17931841
#endif
@@ -2203,7 +2251,7 @@ static struct rtable *__mkroute_output(const struct fib_result *res,
22032251
*/
22042252

22052253
struct rtable *__ip_route_output_key_hash(struct net *net, struct flowi4 *fl4,
2206-
int mp_hash)
2254+
const struct sk_buff *skb)
22072255
{
22082256
struct net_device *dev_out = NULL;
22092257
__u8 tos = RT_FL_TOS(fl4);
@@ -2365,7 +2413,7 @@ struct rtable *__ip_route_output_key_hash(struct net *net, struct flowi4 *fl4,
23652413
goto make_route;
23662414
}
23672415

2368-
fib_select_path(net, &res, fl4, mp_hash);
2416+
fib_select_path(net, &res, fl4, skb);
23692417

23702418
dev_out = FIB_RES_DEV(res);
23712419
fl4->flowi4_oif = dev_out->ifindex;

‎net/ipv4/sysctl_net_ipv4.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,15 @@ static struct ctl_table ipv4_net_table[] = {
997997
.extra1 = &zero,
998998
.extra2 = &one,
999999
},
1000+
{
1001+
.procname = "fib_multipath_hash_policy",
1002+
.data = &init_net.ipv4.sysctl_fib_multipath_hash_policy,
1003+
.maxlen = sizeof(int),
1004+
.mode = 0644,
1005+
.proc_handler = proc_dointvec_minmax,
1006+
.extra1 = &zero,
1007+
.extra2 = &one,
1008+
},
10001009
#endif
10011010
{
10021011
.procname = "ip_unprivileged_port_start",

0 commit comments

Comments
 (0)