Skip to content

Commit 1c78efa

Browse files
rshearmandavem330
authored andcommitted
mpls: flow-based multipath selection
Change the selection of a multipath route to use a flow-based hash. This more suitable for traffic sensitive to reordering within a flow (e.g. TCP, L2VPN) and whilst still allowing a good distribution of traffic given enough flows. Selection of the path for a multipath route is done using a hash of: 1. Label stack up to MAX_MP_SELECT_LABELS labels or up to and including entropy label, whichever is first. 2. 3-tuple of (L3 src, L3 dst, proto) from IPv4/IPv6 header in MPLS payload, if present. Naturally, a 5-tuple hash using L4 information in addition would be possible and be better in some scenarios, but there is a tradeoff between looking deeper into the packet to achieve good distribution, and packet forwarding performance, and I have erred on the side of the latter as the default. Signed-off-by: Robert Shearman <[email protected]> Signed-off-by: Roopa Prabhu <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent f8efb73 commit 1c78efa

File tree

1 file changed

+83
-4
lines changed

1 file changed

+83
-4
lines changed

net/mpls/af_mpls.c

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
#include <net/nexthop.h>
2323
#include "internal.h"
2424

25+
/* Maximum number of labels to look ahead at when selecting a path of
26+
* a multipath route
27+
*/
28+
#define MAX_MP_SELECT_LABELS 4
29+
2530
static int zero = 0;
2631
static int label_limit = (1 << 20) - 1;
2732

@@ -77,10 +82,78 @@ bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
7782
}
7883
EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
7984

80-
static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt)
85+
static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
86+
struct sk_buff *skb, bool bos)
8187
{
82-
/* assume single nexthop for now */
83-
return &rt->rt_nh[0];
88+
struct mpls_entry_decoded dec;
89+
struct mpls_shim_hdr *hdr;
90+
bool eli_seen = false;
91+
int label_index;
92+
int nh_index = 0;
93+
u32 hash = 0;
94+
95+
/* No need to look further into packet if there's only
96+
* one path
97+
*/
98+
if (rt->rt_nhn == 1)
99+
goto out;
100+
101+
for (label_index = 0; label_index < MAX_MP_SELECT_LABELS && !bos;
102+
label_index++) {
103+
if (!pskb_may_pull(skb, sizeof(*hdr) * label_index))
104+
break;
105+
106+
/* Read and decode the current label */
107+
hdr = mpls_hdr(skb) + label_index;
108+
dec = mpls_entry_decode(hdr);
109+
110+
/* RFC6790 - reserved labels MUST NOT be used as keys
111+
* for the load-balancing function
112+
*/
113+
if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
114+
hash = jhash_1word(dec.label, hash);
115+
116+
/* The entropy label follows the entropy label
117+
* indicator, so this means that the entropy
118+
* label was just added to the hash - no need to
119+
* go any deeper either in the label stack or in the
120+
* payload
121+
*/
122+
if (eli_seen)
123+
break;
124+
} else if (dec.label == MPLS_LABEL_ENTROPY) {
125+
eli_seen = true;
126+
}
127+
128+
bos = dec.bos;
129+
if (bos && pskb_may_pull(skb, sizeof(*hdr) * label_index +
130+
sizeof(struct iphdr))) {
131+
const struct iphdr *v4hdr;
132+
133+
v4hdr = (const struct iphdr *)(mpls_hdr(skb) +
134+
label_index);
135+
if (v4hdr->version == 4) {
136+
hash = jhash_3words(ntohl(v4hdr->saddr),
137+
ntohl(v4hdr->daddr),
138+
v4hdr->protocol, hash);
139+
} else if (v4hdr->version == 6 &&
140+
pskb_may_pull(skb, sizeof(*hdr) * label_index +
141+
sizeof(struct ipv6hdr))) {
142+
const struct ipv6hdr *v6hdr;
143+
144+
v6hdr = (const struct ipv6hdr *)(mpls_hdr(skb) +
145+
label_index);
146+
147+
hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
148+
hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
149+
hash = jhash_1word(v6hdr->nexthdr, hash);
150+
}
151+
}
152+
}
153+
154+
nh_index = hash % rt->rt_nhn;
155+
out:
156+
return &rt->rt_nh[nh_index];
84157
}
85158

86159
static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
@@ -175,7 +248,7 @@ static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
175248
if (!rt)
176249
goto drop;
177250

178-
nh = mpls_select_multipath(rt);
251+
nh = mpls_select_multipath(rt, skb, dec.bos);
179252
if (!nh)
180253
goto drop;
181254

@@ -541,6 +614,12 @@ static int mpls_nh_build_multi(struct mpls_route_config *cfg,
541614
if (!rtnh_ok(rtnh, remaining))
542615
goto errout;
543616

617+
/* neither weighted multipath nor any flags
618+
* are supported
619+
*/
620+
if (rtnh->rtnh_hops || rtnh->rtnh_flags)
621+
goto errout;
622+
544623
attrlen = rtnh_attrlen(rtnh);
545624
if (attrlen > 0) {
546625
struct nlattr *attrs = rtnh_attrs(rtnh);

0 commit comments

Comments
 (0)