Skip to content

Commit ea82511

Browse files
tohojodavem330
authored andcommitted
sch_cake: Add NAT awareness to packet classifier
When CAKE is deployed on a gateway that also performs NAT (which is a common deployment mode), the host fairness mechanism cannot distinguish internal hosts from each other, and so fails to work correctly. To fix this, we add an optional NAT awareness mode, which will query the kernel conntrack mechanism to obtain the pre-NAT addresses for each packet and use that in the flow and host hashing. When the shaper is enabled and the host is already performing NAT, the cost of this lookup is negligible. However, in unlimited mode with no NAT being performed, there is a significant CPU cost at higher bandwidths. For this reason, the feature is turned off by default. Cc: [email protected] Signed-off-by: Toke Høiland-Jørgensen <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent b60a604 commit ea82511

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

net/sched/sch_cake.c

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@
7171
#include <net/tcp.h>
7272
#include <net/flow_dissector.h>
7373

74+
#if IS_ENABLED(CONFIG_NF_CONNTRACK)
75+
#include <net/netfilter/nf_conntrack_core.h>
76+
#endif
77+
7478
#define CAKE_SET_WAYS (8)
7579
#define CAKE_MAX_TINS (8)
7680
#define CAKE_QUEUES (1024)
@@ -516,6 +520,29 @@ static bool cobalt_should_drop(struct cobalt_vars *vars,
516520
return drop;
517521
}
518522

523+
static void cake_update_flowkeys(struct flow_keys *keys,
524+
const struct sk_buff *skb)
525+
{
526+
#if IS_ENABLED(CONFIG_NF_CONNTRACK)
527+
struct nf_conntrack_tuple tuple = {};
528+
bool rev = !skb->_nfct;
529+
530+
if (tc_skb_protocol(skb) != htons(ETH_P_IP))
531+
return;
532+
533+
if (!nf_ct_get_tuple_skb(&tuple, skb))
534+
return;
535+
536+
keys->addrs.v4addrs.src = rev ? tuple.dst.u3.ip : tuple.src.u3.ip;
537+
keys->addrs.v4addrs.dst = rev ? tuple.src.u3.ip : tuple.dst.u3.ip;
538+
539+
if (keys->ports.ports) {
540+
keys->ports.src = rev ? tuple.dst.u.all : tuple.src.u.all;
541+
keys->ports.dst = rev ? tuple.src.u.all : tuple.dst.u.all;
542+
}
543+
#endif
544+
}
545+
519546
/* Cake has several subtle multiple bit settings. In these cases you
520547
* would be matching triple isolate mode as well.
521548
*/
@@ -543,6 +570,9 @@ static u32 cake_hash(struct cake_tin_data *q, const struct sk_buff *skb,
543570
skb_flow_dissect_flow_keys(skb, &keys,
544571
FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
545572

573+
if (flow_mode & CAKE_FLOW_NAT_FLAG)
574+
cake_update_flowkeys(&keys, skb);
575+
546576
/* flow_hash_from_keys() sorts the addresses by value, so we have
547577
* to preserve their order in a separate data structure to treat
548578
* src and dst host addresses as independently selectable.
@@ -1939,12 +1969,25 @@ static int cake_change(struct Qdisc *sch, struct nlattr *opt,
19391969
if (err < 0)
19401970
return err;
19411971

1972+
if (tb[TCA_CAKE_NAT]) {
1973+
#if IS_ENABLED(CONFIG_NF_CONNTRACK)
1974+
q->flow_mode &= ~CAKE_FLOW_NAT_FLAG;
1975+
q->flow_mode |= CAKE_FLOW_NAT_FLAG *
1976+
!!nla_get_u32(tb[TCA_CAKE_NAT]);
1977+
#else
1978+
NL_SET_ERR_MSG_ATTR(extack, tb[TCA_CAKE_NAT],
1979+
"No conntrack support in kernel");
1980+
return -EOPNOTSUPP;
1981+
#endif
1982+
}
1983+
19421984
if (tb[TCA_CAKE_BASE_RATE64])
19431985
q->rate_bps = nla_get_u64(tb[TCA_CAKE_BASE_RATE64]);
19441986

19451987
if (tb[TCA_CAKE_FLOW_MODE])
1946-
q->flow_mode = (nla_get_u32(tb[TCA_CAKE_FLOW_MODE]) &
1947-
CAKE_FLOW_MASK);
1988+
q->flow_mode = ((q->flow_mode & CAKE_FLOW_NAT_FLAG) |
1989+
(nla_get_u32(tb[TCA_CAKE_FLOW_MODE]) &
1990+
CAKE_FLOW_MASK));
19481991

19491992
if (tb[TCA_CAKE_RTT]) {
19501993
q->interval = nla_get_u32(tb[TCA_CAKE_RTT]);
@@ -2111,6 +2154,10 @@ static int cake_dump(struct Qdisc *sch, struct sk_buff *skb)
21112154
if (nla_put_u32(skb, TCA_CAKE_ACK_FILTER, q->ack_filter))
21122155
goto nla_put_failure;
21132156

2157+
if (nla_put_u32(skb, TCA_CAKE_NAT,
2158+
!!(q->flow_mode & CAKE_FLOW_NAT_FLAG)))
2159+
goto nla_put_failure;
2160+
21142161
return nla_nest_end(skb, opts);
21152162

21162163
nla_put_failure:

0 commit comments

Comments
 (0)