Commit 22ba9d0d authored by David S. Miller's avatar David S. Miller

Merge branch 'custom-multipath-hash'

Ido Schimmel says:

====================
Add support for custom multipath hash

This patchset adds support for custom multipath hash policy for both
IPv4 and IPv6 traffic. The new policy allows user space to control the
outer and inner packet fields used for the hash computation.

Motivation
==========

Linux currently supports different multipath hash policies for IPv4 and
IPv6 traffic:

* Layer 3
* Layer 4
* Layer 3 or inner layer 3, if present

These policies hash on a fixed set of fields, which is inflexible and
against operators' requirements to control the hash input: "The ability
to control the inputs to the hash function should be a consideration in
any load-balancing RFP" [1].

An example of this inflexibility can be seen by the fact that none of
the current policies allows operators to use the standard 5-tuple and
the flow label for multipath hash computation. Such a policy is useful
in the following real-world example of a data center with the following
types of traffic:

* Anycast IPv6 TCP traffic towards layer 4 load balancers. Flow label is
constant (zero) to avoid breaking established connections

* Non-encapsulated IPv6 traffic. Flow label is used to re-route flows
around problematic (congested / failed) paths [2]

* IPv6 encapsulated traffic (IPv4-in-IPv6 or IPv6-in-IPv6). Outer flow
label is generated from encapsulated packet

* UDP encapsulated traffic. Outer source port is generated from
encapsulated packet

In the above example, using the inner flow information for hash
computation in addition to the outer flow information is useful during
failures of the BPF agent that selectively generates the flow label
based on the traffic type. In such cases, the self-healing properties of
the flow label are lost, but encapsulated flows are still load balanced.

Control over the inner fields is even more critical when encapsulation
is performed by hardware routers. For example, the Spectrum ASIC can
only encode 8 bits of entropy in the outer flow label / outer UDP source
port when performing IP / UDP encapsulation. In the case of IPv4 GRE
encapsulation there is no outer field to encode the inner hash in.

User interface
==============

In accordance with existing multipath hash configuration, the new custom
policy is added as a new option (3) to the
net.ipv{4,6}.fib_multipath_hash_policy sysctls. When the new policy is
used, the packet fields used for hash computation are determined by the
net.ipv{4,6}.fib_multipath_hash_fields sysctls. These sysctls accept a
bitmask according to the following table (from ip-sysctl.rst):

	====== ============================
	0x0001 Source IP address
	0x0002 Destination IP address
	0x0004 IP protocol
	0x0008 Flow Label
	0x0010 Source port
	0x0020 Destination port
	0x0040 Inner source IP address
	0x0080 Inner destination IP address
	0x0100 Inner IP protocol
	0x0200 Inner Flow Label
	0x0400 Inner source port
	0x0800 Inner destination port
	====== ============================

For example, to allow IPv6 traffic to be hashed based on standard
5-tuple and flow label:

 # sysctl -wq net.ipv6.fib_multipath_hash_fields=0x0037
 # sysctl -wq net.ipv6.fib_multipath_hash_policy=3

Implementation
==============

As with existing policies, the new policy relies on the flow dissector
to extract the packet fields for the hash computation. However, unlike
existing policies that either use the outer or inner flow, the new
policy might require both flows to be dissected.

To avoid unnecessary invocations of the flow dissector, the data path
skips dissection of the outer or inner flows if none of the outer or
inner fields are required.

In addition, inner flow dissection is not performed when no
encapsulation was encountered (i.e., 'FLOW_DIS_ENCAPSULATION' not set by
flow dissector) during dissection of the outer flow.

Testing
=======

Three new selftests are added with three different topologies that allow
testing of following traffic combinations:

* Non-encapsulated IPv4 / IPv6 traffic
* IPv4 / IPv6 overlay over IPv4 underlay
* IPv4 / IPv6 overlay over IPv6 underlay

All three tests follow the same pattern. Each time a different packet
field is used for hash computation. When the field changes in the packet
stream, traffic is expected to be balanced across the two paths. When
the field does not change, traffic is expected to be unbalanced across
the two paths.

Patchset overview
=================

Patches #1-#3 add custom multipath hash support for IPv4 traffic
Patches #4-#7 do the same for IPv6
Patches #8-#10 add selftests

Future work
===========

mlxsw support can be found here [3].

Changes since RFC v2 [4]:

* Patch #2: Document that 0x0008 is used for Flow Label
* Patch #2: Do not allow the bitmask to be zero
* Patch #6: Do not allow the bitmask to be zero

Changes since RFC v1 [5]:

* Use a bitmask instead of a bitmap

[1] https://blog.apnic.net/2018/01/11/ipv6-flow-label-misuse-hashing/
[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=3acf3ec3f4b0fd4263989f2e4227bbd1c42b5fe1
[3] https://github.com/idosch/linux/tree/submit/custom_hash_mlxsw_v2
[4] https://lore.kernel.org/netdev/20210509151615.200608-1-idosch@idosch.org/
[5] https://lore.kernel.org/netdev/20210502162257.3472453-1-idosch@idosch.org/
====================
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents 06b38e23 b7715acb
......@@ -99,6 +99,35 @@ fib_multipath_hash_policy - INTEGER
- 0 - Layer 3
- 1 - Layer 4
- 2 - Layer 3 or inner Layer 3 if present
- 3 - Custom multipath hash. Fields used for multipath hash calculation
are determined by fib_multipath_hash_fields sysctl
fib_multipath_hash_fields - UNSIGNED INTEGER
When fib_multipath_hash_policy is set to 3 (custom multipath hash), the
fields used for multipath hash calculation are determined by this
sysctl.
This value is a bitmask which enables various fields for multipath hash
calculation.
Possible fields are:
====== ============================
0x0001 Source IP address
0x0002 Destination IP address
0x0004 IP protocol
0x0008 Unused (Flow Label)
0x0010 Source port
0x0020 Destination port
0x0040 Inner source IP address
0x0080 Inner destination IP address
0x0100 Inner IP protocol
0x0200 Inner Flow Label
0x0400 Inner source port
0x0800 Inner destination port
====== ============================
Default: 0x0007 (source IP, destination IP and IP protocol)
fib_sync_mem - UNSIGNED INTEGER
Amount of dirty memory from fib entries that can be backlogged before
......@@ -1743,6 +1772,35 @@ fib_multipath_hash_policy - INTEGER
- 0 - Layer 3 (source and destination addresses plus flow label)
- 1 - Layer 4 (standard 5-tuple)
- 2 - Layer 3 or inner Layer 3 if present
- 3 - Custom multipath hash. Fields used for multipath hash calculation
are determined by fib_multipath_hash_fields sysctl
fib_multipath_hash_fields - UNSIGNED INTEGER
When fib_multipath_hash_policy is set to 3 (custom multipath hash), the
fields used for multipath hash calculation are determined by this
sysctl.
This value is a bitmask which enables various fields for multipath hash
calculation.
Possible fields are:
====== ============================
0x0001 Source IP address
0x0002 Destination IP address
0x0004 IP protocol
0x0008 Flow Label
0x0010 Source port
0x0020 Destination port
0x0040 Inner source IP address
0x0080 Inner destination IP address
0x0100 Inner IP protocol
0x0200 Inner Flow Label
0x0400 Inner source port
0x0800 Inner destination port
====== ============================
Default: 0x0007 (source IP, destination IP and IP protocol)
anycast_src_echo_reply - BOOLEAN
Controls the use of anycast addresses as source addresses for ICMPv6
......
......@@ -466,6 +466,49 @@ int fib_sync_up(struct net_device *dev, unsigned char nh_flags);
void fib_sync_mtu(struct net_device *dev, u32 orig_mtu);
void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig);
/* Fields used for sysctl_fib_multipath_hash_fields.
* Common to IPv4 and IPv6.
*
* Add new fields at the end. This is user API.
*/
#define FIB_MULTIPATH_HASH_FIELD_SRC_IP BIT(0)
#define FIB_MULTIPATH_HASH_FIELD_DST_IP BIT(1)
#define FIB_MULTIPATH_HASH_FIELD_IP_PROTO BIT(2)
#define FIB_MULTIPATH_HASH_FIELD_FLOWLABEL BIT(3)
#define FIB_MULTIPATH_HASH_FIELD_SRC_PORT BIT(4)
#define FIB_MULTIPATH_HASH_FIELD_DST_PORT BIT(5)
#define FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP BIT(6)
#define FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP BIT(7)
#define FIB_MULTIPATH_HASH_FIELD_INNER_IP_PROTO BIT(8)
#define FIB_MULTIPATH_HASH_FIELD_INNER_FLOWLABEL BIT(9)
#define FIB_MULTIPATH_HASH_FIELD_INNER_SRC_PORT BIT(10)
#define FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT BIT(11)
#define FIB_MULTIPATH_HASH_FIELD_OUTER_MASK \
(FIB_MULTIPATH_HASH_FIELD_SRC_IP | \
FIB_MULTIPATH_HASH_FIELD_DST_IP | \
FIB_MULTIPATH_HASH_FIELD_IP_PROTO | \
FIB_MULTIPATH_HASH_FIELD_FLOWLABEL | \
FIB_MULTIPATH_HASH_FIELD_SRC_PORT | \
FIB_MULTIPATH_HASH_FIELD_DST_PORT)
#define FIB_MULTIPATH_HASH_FIELD_INNER_MASK \
(FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP | \
FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP | \
FIB_MULTIPATH_HASH_FIELD_INNER_IP_PROTO | \
FIB_MULTIPATH_HASH_FIELD_INNER_FLOWLABEL | \
FIB_MULTIPATH_HASH_FIELD_INNER_SRC_PORT | \
FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT)
#define FIB_MULTIPATH_HASH_FIELD_ALL_MASK \
(FIB_MULTIPATH_HASH_FIELD_OUTER_MASK | \
FIB_MULTIPATH_HASH_FIELD_INNER_MASK)
#define FIB_MULTIPATH_HASH_FIELD_DEFAULT_MASK \
(FIB_MULTIPATH_HASH_FIELD_SRC_IP | \
FIB_MULTIPATH_HASH_FIELD_DST_IP | \
FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
#ifdef CONFIG_IP_ROUTE_MULTIPATH
int fib_multipath_hash(const struct net *net, const struct flowi4 *fl4,
const struct sk_buff *skb, struct flow_keys *flkeys);
......
......@@ -926,11 +926,19 @@ static inline int ip6_multipath_hash_policy(const struct net *net)
{
return net->ipv6.sysctl.multipath_hash_policy;
}
static inline u32 ip6_multipath_hash_fields(const struct net *net)
{
return net->ipv6.sysctl.multipath_hash_fields;
}
#else
static inline int ip6_multipath_hash_policy(const struct net *net)
{
return 0;
}
static inline u32 ip6_multipath_hash_fields(const struct net *net)
{
return 0;
}
#endif
/*
......
......@@ -210,6 +210,7 @@ struct netns_ipv4 {
#endif
#endif
#ifdef CONFIG_IP_ROUTE_MULTIPATH
u32 sysctl_fib_multipath_hash_fields;
u8 sysctl_fib_multipath_use_neigh;
u8 sysctl_fib_multipath_hash_policy;
#endif
......
......@@ -28,8 +28,9 @@ struct netns_sysctl_ipv6 {
int ip6_rt_gc_elasticity;
int ip6_rt_mtu_expires;
int ip6_rt_min_advmss;
u8 bindv6only;
u32 multipath_hash_fields;
u8 multipath_hash_policy;
u8 bindv6only;
u8 flowlabel_consistency;
u8 auto_flowlabels;
int icmpv6_time;
......
......@@ -1514,6 +1514,12 @@ static int __net_init ip_fib_net_init(struct net *net)
if (err)
return err;
#ifdef CONFIG_IP_ROUTE_MULTIPATH
/* Default to 3-tuple */
net->ipv4.sysctl_fib_multipath_hash_fields =
FIB_MULTIPATH_HASH_FIELD_DEFAULT_MASK;
#endif
/* Avoid false sharing : Use at least a full cache line */
size = max_t(size_t, size, L1_CACHE_BYTES);
......
......@@ -1906,13 +1906,128 @@ static void ip_multipath_l3_keys(const struct sk_buff *skb,
hash_keys->addrs.v4addrs.dst = key_iph->daddr;
}
static u32 fib_multipath_custom_hash_outer(const struct net *net,
const struct sk_buff *skb,
bool *p_has_inner)
{
u32 hash_fields = net->ipv4.sysctl_fib_multipath_hash_fields;
struct flow_keys keys, hash_keys;
if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK))
return 0;
memset(&hash_keys, 0, sizeof(hash_keys));
skb_flow_dissect_flow_keys(skb, &keys, FLOW_DISSECTOR_F_STOP_AT_ENCAP);
hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP)
hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP)
hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
hash_keys.basic.ip_proto = keys.basic.ip_proto;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT)
hash_keys.ports.src = keys.ports.src;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT)
hash_keys.ports.dst = keys.ports.dst;
*p_has_inner = !!(keys.control.flags & FLOW_DIS_ENCAPSULATION);
return flow_hash_from_keys(&hash_keys);
}
static u32 fib_multipath_custom_hash_inner(const struct net *net,
const struct sk_buff *skb,
bool has_inner)
{
u32 hash_fields = net->ipv4.sysctl_fib_multipath_hash_fields;
struct flow_keys keys, hash_keys;
/* We assume the packet carries an encapsulation, but if none was
* encountered during dissection of the outer flow, then there is no
* point in calling the flow dissector again.
*/
if (!has_inner)
return 0;
if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_MASK))
return 0;
memset(&hash_keys, 0, sizeof(hash_keys));
skb_flow_dissect_flow_keys(skb, &keys, 0);
if (!(keys.control.flags & FLOW_DIS_ENCAPSULATION))
return 0;
if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP)
hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP)
hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst;
} else if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP)
hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP)
hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_FLOWLABEL)
hash_keys.tags.flow_label = keys.tags.flow_label;
}
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_IP_PROTO)
hash_keys.basic.ip_proto = keys.basic.ip_proto;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_PORT)
hash_keys.ports.src = keys.ports.src;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT)
hash_keys.ports.dst = keys.ports.dst;
return flow_hash_from_keys(&hash_keys);
}
static u32 fib_multipath_custom_hash_skb(const struct net *net,
const struct sk_buff *skb)
{
u32 mhash, mhash_inner;
bool has_inner = true;
mhash = fib_multipath_custom_hash_outer(net, skb, &has_inner);
mhash_inner = fib_multipath_custom_hash_inner(net, skb, has_inner);
return jhash_2words(mhash, mhash_inner, 0);
}
static u32 fib_multipath_custom_hash_fl4(const struct net *net,
const struct flowi4 *fl4)
{
u32 hash_fields = net->ipv4.sysctl_fib_multipath_hash_fields;
struct flow_keys hash_keys;
if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK))
return 0;
memset(&hash_keys, 0, sizeof(hash_keys));
hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP)
hash_keys.addrs.v4addrs.src = fl4->saddr;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP)
hash_keys.addrs.v4addrs.dst = fl4->daddr;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
hash_keys.basic.ip_proto = fl4->flowi4_proto;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT)
hash_keys.ports.src = fl4->fl4_sport;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT)
hash_keys.ports.dst = fl4->fl4_dport;
return flow_hash_from_keys(&hash_keys);
}
/* if skb is set it will be used and fl4 can be NULL */
int fib_multipath_hash(const struct net *net, const struct flowi4 *fl4,
const struct sk_buff *skb, struct flow_keys *flkeys)
{
u32 multipath_hash = fl4 ? fl4->flowi4_multipath_hash : 0;
struct flow_keys hash_keys;
u32 mhash;
u32 mhash = 0;
switch (net->ipv4.sysctl_fib_multipath_hash_policy) {
case 0:
......@@ -1924,6 +2039,7 @@ int fib_multipath_hash(const struct net *net, const struct flowi4 *fl4,
hash_keys.addrs.v4addrs.src = fl4->saddr;
hash_keys.addrs.v4addrs.dst = fl4->daddr;
}
mhash = flow_hash_from_keys(&hash_keys);
break;
case 1:
/* skb is currently provided only when forwarding */
......@@ -1957,6 +2073,7 @@ int fib_multipath_hash(const struct net *net, const struct flowi4 *fl4,
hash_keys.ports.dst = fl4->fl4_dport;
hash_keys.basic.ip_proto = fl4->flowi4_proto;
}
mhash = flow_hash_from_keys(&hash_keys);
break;
case 2:
memset(&hash_keys, 0, sizeof(hash_keys));
......@@ -1987,9 +2104,15 @@ int fib_multipath_hash(const struct net *net, const struct flowi4 *fl4,
hash_keys.addrs.v4addrs.src = fl4->saddr;
hash_keys.addrs.v4addrs.dst = fl4->daddr;
}
mhash = flow_hash_from_keys(&hash_keys);
break;
case 3:
if (skb)
mhash = fib_multipath_custom_hash_skb(net, skb);
else
mhash = fib_multipath_custom_hash_fl4(net, fl4);
break;
}
mhash = flow_hash_from_keys(&hash_keys);
if (multipath_hash)
mhash = jhash_2words(mhash, multipath_hash, 0);
......
......@@ -19,6 +19,7 @@
#include <net/snmp.h>
#include <net/icmp.h>
#include <net/ip.h>
#include <net/ip_fib.h>
#include <net/route.h>
#include <net/tcp.h>
#include <net/udp.h>
......@@ -29,6 +30,7 @@
#include <net/netevent.h>
static int two = 2;
static int three __maybe_unused = 3;
static int four = 4;
static int thousand = 1000;
static int tcp_retr1_max = 255;
......@@ -48,6 +50,8 @@ static int ip_ping_group_range_min[] = { 0, 0 };
static int ip_ping_group_range_max[] = { GID_T_MAX, GID_T_MAX };
static u32 u32_max_div_HZ = UINT_MAX / HZ;
static int one_day_secs = 24 * 3600;
static u32 fib_multipath_hash_fields_all_mask __maybe_unused =
FIB_MULTIPATH_HASH_FIELD_ALL_MASK;
/* obsolete */
static int sysctl_tcp_low_latency __read_mostly;
......@@ -1050,7 +1054,16 @@ static struct ctl_table ipv4_net_table[] = {
.mode = 0644,
.proc_handler = proc_fib_multipath_hash_policy,
.extra1 = SYSCTL_ZERO,
.extra2 = &two,
.extra2 = &three,
},
{
.procname = "fib_multipath_hash_fields",
.data = &init_net.ipv4.sysctl_fib_multipath_hash_fields,
.maxlen = sizeof(u32),
.mode = 0644,
.proc_handler = proc_douintvec_minmax,
.extra1 = SYSCTL_ONE,
.extra2 = &fib_multipath_hash_fields_all_mask,
},
#endif
{
......
......@@ -32,6 +32,7 @@
#include <net/lwtunnel.h>
#include <net/fib_notifier.h>
#include <net/ip_fib.h>
#include <net/ip6_fib.h>
#include <net/ip6_route.h>
......@@ -2355,6 +2356,10 @@ static int __net_init fib6_net_init(struct net *net)
if (err)
return err;
/* Default to 3-tuple */
net->ipv6.sysctl.multipath_hash_fields =
FIB_MULTIPATH_HASH_FIELD_DEFAULT_MASK;
spin_lock_init(&net->ipv6.fib6_gc_lock);
rwlock_init(&net->ipv6.fib6_walker_lock);
INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
......@@ -2362,7 +2367,7 @@ static int __net_init fib6_net_init(struct net *net)
net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
if (!net->ipv6.rt6_stats)
goto out_timer;
goto out_notifier;
/* Avoid false sharing : Use at least a full cache line */
size = max_t(size_t, size, L1_CACHE_BYTES);
......@@ -2407,7 +2412,7 @@ static int __net_init fib6_net_init(struct net *net)
kfree(net->ipv6.fib_table_hash);
out_rt6_stats:
kfree(net->ipv6.rt6_stats);
out_timer:
out_notifier:
fib6_notifier_exit(net);
return -ENOMEM;
}
......
......@@ -2326,12 +2326,131 @@ static void ip6_multipath_l3_keys(const struct sk_buff *skb,
}
}
static u32 rt6_multipath_custom_hash_outer(const struct net *net,
const struct sk_buff *skb,
bool *p_has_inner)
{
u32 hash_fields = ip6_multipath_hash_fields(net);
struct flow_keys keys, hash_keys;
if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK))
return 0;
memset(&hash_keys, 0, sizeof(hash_keys));
skb_flow_dissect_flow_keys(skb, &keys, FLOW_DISSECTOR_F_STOP_AT_ENCAP);
hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP)
hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP)
hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
hash_keys.basic.ip_proto = keys.basic.ip_proto;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_FLOWLABEL)
hash_keys.tags.flow_label = keys.tags.flow_label;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT)
hash_keys.ports.src = keys.ports.src;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT)
hash_keys.ports.dst = keys.ports.dst;
*p_has_inner = !!(keys.control.flags & FLOW_DIS_ENCAPSULATION);
return flow_hash_from_keys(&hash_keys);
}
static u32 rt6_multipath_custom_hash_inner(const struct net *net,
const struct sk_buff *skb,
bool has_inner)
{
u32 hash_fields = ip6_multipath_hash_fields(net);
struct flow_keys keys, hash_keys;
/* We assume the packet carries an encapsulation, but if none was
* encountered during dissection of the outer flow, then there is no
* point in calling the flow dissector again.
*/
if (!has_inner)
return 0;
if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_MASK))
return 0;
memset(&hash_keys, 0, sizeof(hash_keys));
skb_flow_dissect_flow_keys(skb, &keys, 0);
if (!(keys.control.flags & FLOW_DIS_ENCAPSULATION))
return 0;
if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP)
hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP)
hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst;
} else if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP)
hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP)
hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_FLOWLABEL)
hash_keys.tags.flow_label = keys.tags.flow_label;
}
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_IP_PROTO)
hash_keys.basic.ip_proto = keys.basic.ip_proto;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_PORT)
hash_keys.ports.src = keys.ports.src;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT)
hash_keys.ports.dst = keys.ports.dst;
return flow_hash_from_keys(&hash_keys);
}
static u32 rt6_multipath_custom_hash_skb(const struct net *net,
const struct sk_buff *skb)
{
u32 mhash, mhash_inner;
bool has_inner = true;
mhash = rt6_multipath_custom_hash_outer(net, skb, &has_inner);
mhash_inner = rt6_multipath_custom_hash_inner(net, skb, has_inner);
return jhash_2words(mhash, mhash_inner, 0);
}
static u32 rt6_multipath_custom_hash_fl6(const struct net *net,
const struct flowi6 *fl6)
{
u32 hash_fields = ip6_multipath_hash_fields(net);
struct flow_keys hash_keys;
if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK))
return 0;
memset(&hash_keys, 0, sizeof(hash_keys));
hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP)
hash_keys.addrs.v6addrs.src = fl6->saddr;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP)
hash_keys.addrs.v6addrs.dst = fl6->daddr;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
hash_keys.basic.ip_proto = fl6->flowi6_proto;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_FLOWLABEL)
hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT)
hash_keys.ports.src = fl6->fl6_sport;
if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT)
hash_keys.ports.dst = fl6->fl6_dport;
return flow_hash_from_keys(&hash_keys);
}
/* if skb is set it will be used and fl6 can be NULL */
u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
const struct sk_buff *skb, struct flow_keys *flkeys)
{
struct flow_keys hash_keys;
u32 mhash;
u32 mhash = 0;
switch (ip6_multipath_hash_policy(net)) {
case 0:
......@@ -2345,6 +2464,7 @@ u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
hash_keys.basic.ip_proto = fl6->flowi6_proto;
}
mhash = flow_hash_from_keys(&hash_keys);
break;
case 1:
if (skb) {
......@@ -2376,6 +2496,7 @@ u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
hash_keys.ports.dst = fl6->fl6_dport;
hash_keys.basic.ip_proto = fl6->flowi6_proto;
}
mhash = flow_hash_from_keys(&hash_keys);
break;
case 2:
memset(&hash_keys, 0, sizeof(hash_keys));
......@@ -2412,9 +2533,15 @@ u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
hash_keys.basic.ip_proto = fl6->flowi6_proto;
}
mhash = flow_hash_from_keys(&hash_keys);
break;
case 3:
if (skb)
mhash = rt6_multipath_custom_hash_skb(net, skb);
else
mhash = rt6_multipath_custom_hash_fl6(net, fl6);
break;
}
mhash = flow_hash_from_keys(&hash_keys);
return mhash >> 1;
}
......
......@@ -17,13 +17,17 @@
#include <net/addrconf.h>
#include <net/inet_frag.h>
#include <net/netevent.h>
#include <net/ip_fib.h>
#ifdef CONFIG_NETLABEL
#include <net/calipso.h>
#endif
static int two = 2;
static int three = 3;
static int flowlabel_reflect_max = 0x7;
static int auto_flowlabels_max = IP6_AUTO_FLOW_LABEL_MAX;
static u32 rt6_multipath_hash_fields_all_mask =
FIB_MULTIPATH_HASH_FIELD_ALL_MASK;
static int proc_rt6_multipath_hash_policy(struct ctl_table *table, int write,
void *buffer, size_t *lenp, loff_t *ppos)
......@@ -149,7 +153,16 @@ static struct ctl_table ipv6_table_template[] = {
.mode = 0644,
.proc_handler = proc_rt6_multipath_hash_policy,
.extra1 = SYSCTL_ZERO,
.extra2 = &two,
.extra2 = &three,
},
{
.procname = "fib_multipath_hash_fields",
.data = &init_net.ipv6.sysctl.multipath_hash_fields,
.maxlen = sizeof(u32),
.mode = 0644,
.proc_handler = proc_douintvec_minmax,
.extra1 = SYSCTL_ONE,
.extra2 = &rt6_multipath_hash_fields_all_mask,
},
{
.procname = "seg6_flowlabel",
......
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
#
# Test traffic distribution between two paths when using custom hash policy.
#
# +--------------------------------+
# | H1 |
# | $h1 + |
# | 198.51.100.{2-253}/24 | |
# | 2001:db8:1::{2-fd}/64 | |
# +-------------------------|------+
# |
# +-------------------------|-------------------------+
# | SW1 | |
# | $rp1 + |
# | 198.51.100.1/24 |
# | 2001:db8:1::1/64 |
# | |
# | |
# | $rp11 + + $rp12 |
# | 192.0.2.1/28 | | 192.0.2.17/28 |
# | 2001:db8:2::1/64 | | 2001:db8:3::1/64 |
# +------------------|-------------|------------------+
# | |
# +------------------|-------------|------------------+
# | SW2 | | |
# | | | |
# | $rp21 + + $rp22 |
# | 192.0.2.2/28 192.0.2.18/28 |
# | 2001:db8:2::2/64 2001:db8:3::2/64 |
# | |
# | |
# | $rp2 + |
# | 203.0.113.1/24 | |
# | 2001:db8:4::1/64 | |
# +-------------------------|-------------------------+
# |
# +-------------------------|------+
# | H2 | |
# | $h2 + |
# | 203.0.113.{2-253}/24 |
# | 2001:db8:4::{2-fd}/64 |
# +--------------------------------+
ALL_TESTS="
ping_ipv4
ping_ipv6
custom_hash
"
NUM_NETIFS=8
source lib.sh
h1_create()
{
simple_if_init $h1 198.51.100.2/24 2001:db8:1::2/64
ip route add vrf v$h1 default via 198.51.100.1 dev $h1
ip -6 route add vrf v$h1 default via 2001:db8:1::1 dev $h1
}
h1_destroy()
{
ip -6 route del vrf v$h1 default
ip route del vrf v$h1 default
simple_if_fini $h1 198.51.100.2/24 2001:db8:1::2/64
}
sw1_create()
{
simple_if_init $rp1 198.51.100.1/24 2001:db8:1::1/64
__simple_if_init $rp11 v$rp1 192.0.2.1/28 2001:db8:2::1/64
__simple_if_init $rp12 v$rp1 192.0.2.17/28 2001:db8:3::1/64
ip route add vrf v$rp1 203.0.113.0/24 \
nexthop via 192.0.2.2 dev $rp11 \
nexthop via 192.0.2.18 dev $rp12
ip -6 route add vrf v$rp1 2001:db8:4::/64 \
nexthop via 2001:db8:2::2 dev $rp11 \
nexthop via 2001:db8:3::2 dev $rp12
}
sw1_destroy()
{
ip -6 route del vrf v$rp1 2001:db8:4::/64
ip route del vrf v$rp1 203.0.113.0/24
__simple_if_fini $rp12 192.0.2.17/28 2001:db8:3::1/64
__simple_if_fini $rp11 192.0.2.1/28 2001:db8:2::1/64
simple_if_fini $rp1 198.51.100.1/24 2001:db8:1::1/64
}
sw2_create()
{
simple_if_init $rp2 203.0.113.1/24 2001:db8:4::1/64
__simple_if_init $rp21 v$rp2 192.0.2.2/28 2001:db8:2::2/64
__simple_if_init $rp22 v$rp2 192.0.2.18/28 2001:db8:3::2/64
ip route add vrf v$rp2 198.51.100.0/24 \
nexthop via 192.0.2.1 dev $rp21 \
nexthop via 192.0.2.17 dev $rp22
ip -6 route add vrf v$rp2 2001:db8:1::/64 \
nexthop via 2001:db8:2::1 dev $rp21 \
nexthop via 2001:db8:3::1 dev $rp22
}
sw2_destroy()
{
ip -6 route del vrf v$rp2 2001:db8:1::/64
ip route del vrf v$rp2 198.51.100.0/24
__simple_if_fini $rp22 192.0.2.18/28 2001:db8:3::2/64
__simple_if_fini $rp21 192.0.2.2/28 2001:db8:2::2/64
simple_if_fini $rp2 203.0.113.1/24 2001:db8:4::1/64
}
h2_create()
{
simple_if_init $h2 203.0.113.2/24 2001:db8:4::2/64
ip route add vrf v$h2 default via 203.0.113.1 dev $h2
ip -6 route add vrf v$h2 default via 2001:db8:4::1 dev $h2
}
h2_destroy()
{
ip -6 route del vrf v$h2 default
ip route del vrf v$h2 default
simple_if_fini $h2 203.0.113.2/24 2001:db8:4::2/64
}
setup_prepare()
{
h1=${NETIFS[p1]}
rp1=${NETIFS[p2]}
rp11=${NETIFS[p3]}
rp21=${NETIFS[p4]}
rp12=${NETIFS[p5]}
rp22=${NETIFS[p6]}
rp2=${NETIFS[p7]}
h2=${NETIFS[p8]}
vrf_prepare
h1_create
sw1_create
sw2_create
h2_create
forwarding_enable
}
cleanup()
{
pre_cleanup
forwarding_restore
h2_destroy
sw2_destroy
sw1_destroy
h1_destroy
vrf_cleanup
}
ping_ipv4()
{
ping_test $h1 203.0.113.2
}
ping_ipv6()
{
ping6_test $h1 2001:db8:4::2
}
send_src_ipv4()
{
$MZ $h1 -q -p 64 -A "198.51.100.2-198.51.100.253" -B 203.0.113.2 \
-d 1msec -c 50 -t udp "sp=20000,dp=30000"
}
send_dst_ipv4()
{
$MZ $h1 -q -p 64 -A 198.51.100.2 -B "203.0.113.2-203.0.113.253" \
-d 1msec -c 50 -t udp "sp=20000,dp=30000"
}
send_src_udp4()
{
$MZ $h1 -q -p 64 -A 198.51.100.2 -B 203.0.113.2 \
-d 1msec -t udp "sp=0-32768,dp=30000"
}
send_dst_udp4()
{
$MZ $h1 -q -p 64 -A 198.51.100.2 -B 203.0.113.2 \
-d 1msec -t udp "sp=20000,dp=0-32768"
}
send_src_ipv6()
{
$MZ -6 $h1 -q -p 64 -A "2001:db8:1::2-2001:db8:1::fd" -B 2001:db8:4::2 \
-d 1msec -c 50 -t udp "sp=20000,dp=30000"
}
send_dst_ipv6()
{
$MZ -6 $h1 -q -p 64 -A 2001:db8:1::2 -B "2001:db8:4::2-2001:db8:4::fd" \
-d 1msec -c 50 -t udp "sp=20000,dp=30000"
}
send_flowlabel()
{
# Generate 16384 echo requests, each with a random flow label.
for _ in $(seq 1 16384); do
ip vrf exec v$h1 \
$PING6 2001:db8:4::2 -F 0 -c 1 -q >/dev/null 2>&1
done
}
send_src_udp6()
{
$MZ -6 $h1 -q -p 64 -A 2001:db8:1::2 -B 2001:db8:4::2 \
-d 1msec -t udp "sp=0-32768,dp=30000"
}
send_dst_udp6()
{
$MZ -6 $h1 -q -p 64 -A 2001:db8:1::2 -B 2001:db8:4::2 \
-d 1msec -t udp "sp=20000,dp=0-32768"
}
custom_hash_test()
{
local field="$1"; shift
local balanced="$1"; shift
local send_flows="$@"
RET=0
local t0_rp11=$(link_stats_tx_packets_get $rp11)
local t0_rp12=$(link_stats_tx_packets_get $rp12)
$send_flows
local t1_rp11=$(link_stats_tx_packets_get $rp11)
local t1_rp12=$(link_stats_tx_packets_get $rp12)
local d_rp11=$((t1_rp11 - t0_rp11))
local d_rp12=$((t1_rp12 - t0_rp12))
local diff=$((d_rp12 - d_rp11))
local sum=$((d_rp11 + d_rp12))
local pct=$(echo "$diff / $sum * 100" | bc -l)
local is_balanced=$(echo "-20 <= $pct && $pct <= 20" | bc)
[[ ( $is_balanced -eq 1 && $balanced == "balanced" ) ||
( $is_balanced -eq 0 && $balanced == "unbalanced" ) ]]
check_err $? "Expected traffic to be $balanced, but it is not"
log_test "Multipath hash field: $field ($balanced)"
log_info "Packets sent on path1 / path2: $d_rp11 / $d_rp12"
}
custom_hash_v4()
{
log_info "Running IPv4 custom multipath hash tests"
sysctl_set net.ipv4.fib_multipath_hash_policy 3
# Prevent the neighbour table from overflowing, as different neighbour
# entries will be created on $ol4 when using different destination IPs.
sysctl_set net.ipv4.neigh.default.gc_thresh1 1024
sysctl_set net.ipv4.neigh.default.gc_thresh2 1024
sysctl_set net.ipv4.neigh.default.gc_thresh3 1024
sysctl_set net.ipv4.fib_multipath_hash_fields 0x0001
custom_hash_test "Source IP" "balanced" send_src_ipv4
custom_hash_test "Source IP" "unbalanced" send_dst_ipv4
sysctl_set net.ipv4.fib_multipath_hash_fields 0x0002
custom_hash_test "Destination IP" "balanced" send_dst_ipv4
custom_hash_test "Destination IP" "unbalanced" send_src_ipv4
sysctl_set net.ipv4.fib_multipath_hash_fields 0x0010
custom_hash_test "Source port" "balanced" send_src_udp4
custom_hash_test "Source port" "unbalanced" send_dst_udp4
sysctl_set net.ipv4.fib_multipath_hash_fields 0x0020
custom_hash_test "Destination port" "balanced" send_dst_udp4
custom_hash_test "Destination port" "unbalanced" send_src_udp4
sysctl_restore net.ipv4.neigh.default.gc_thresh3
sysctl_restore net.ipv4.neigh.default.gc_thresh2
sysctl_restore net.ipv4.neigh.default.gc_thresh1
sysctl_restore net.ipv4.fib_multipath_hash_policy
}
custom_hash_v6()
{
log_info "Running IPv6 custom multipath hash tests"
sysctl_set net.ipv6.fib_multipath_hash_policy 3
# Prevent the neighbour table from overflowing, as different neighbour
# entries will be created on $ol4 when using different destination IPs.
sysctl_set net.ipv6.neigh.default.gc_thresh1 1024
sysctl_set net.ipv6.neigh.default.gc_thresh2 1024
sysctl_set net.ipv6.neigh.default.gc_thresh3 1024
sysctl_set net.ipv6.fib_multipath_hash_fields 0x0001
custom_hash_test "Source IP" "balanced" send_src_ipv6
custom_hash_test "Source IP" "unbalanced" send_dst_ipv6
sysctl_set net.ipv6.fib_multipath_hash_fields 0x0002
custom_hash_test "Destination IP" "balanced" send_dst_ipv6
custom_hash_test "Destination IP" "unbalanced" send_src_ipv6
sysctl_set net.ipv6.fib_multipath_hash_fields 0x0008
custom_hash_test "Flowlabel" "balanced" send_flowlabel
custom_hash_test "Flowlabel" "unbalanced" send_src_ipv6
sysctl_set net.ipv6.fib_multipath_hash_fields 0x0010
custom_hash_test "Source port" "balanced" send_src_udp6
custom_hash_test "Source port" "unbalanced" send_dst_udp6
sysctl_set net.ipv6.fib_multipath_hash_fields 0x0020
custom_hash_test "Destination port" "balanced" send_dst_udp6
custom_hash_test "Destination port" "unbalanced" send_src_udp6
sysctl_restore net.ipv6.neigh.default.gc_thresh3
sysctl_restore net.ipv6.neigh.default.gc_thresh2
sysctl_restore net.ipv6.neigh.default.gc_thresh1
sysctl_restore net.ipv6.fib_multipath_hash_policy
}
custom_hash()
{
# Test that when the hash policy is set to custom, traffic is
# distributed only according to the fields set in the
# fib_multipath_hash_fields sysctl.
#
# Each time set a different field and make sure traffic is only
# distributed when the field is changed in the packet stream.
custom_hash_v4
custom_hash_v6
}
trap cleanup EXIT
setup_prepare
setup_wait
tests_run
exit $EXIT_STATUS
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment