Skip to content

Commit a4daad6

Browse files
committed
net: Pre-COW metrics for TCP.
TCP is going to record metrics for the connection, so pre-COW the route metrics at route cache entry creation time. This avoids several atomic operations that have to occur if we COW the metrics after the entry reaches global visibility. Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 8571a19 commit a4daad6

4 files changed

Lines changed: 36 additions & 5 deletions

File tree

‎include/net/flow.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ struct flowi {
4848

4949
__u8 proto;
5050
__u8 flags;
51-
#define FLOWI_FLAG_ANYSRC 0x01
51+
#define FLOWI_FLAG_ANYSRC 0x01
52+
#define FLOWI_FLAG_PRECOW_METRICS 0x02
5253
union {
5354
struct {
5455
__be16 sport;

‎include/net/inet_sock.h‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,13 @@ static inline struct request_sock *inet_reqsk_alloc(struct request_sock_ops *ops
219219

220220
static inline __u8 inet_sk_flowi_flags(const struct sock *sk)
221221
{
222-
return inet_sk(sk)->transparent ? FLOWI_FLAG_ANYSRC : 0;
222+
__u8 flags = 0;
223+
224+
if (inet_sk(sk)->transparent)
225+
flags |= FLOWI_FLAG_ANYSRC;
226+
if (sk->sk_protocol == IPPROTO_TCP)
227+
flags |= FLOWI_FLAG_PRECOW_METRICS;
228+
return flags;
223229
}
224230

225231
#endif /* _INET_SOCK_H */

‎include/net/route.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ static inline int ip_route_connect(struct rtable **rp, __be32 dst,
182182

183183
if (inet_sk(sk)->transparent)
184184
fl.flags |= FLOWI_FLAG_ANYSRC;
185+
if (protocol == IPPROTO_TCP)
186+
fl.flags |= FLOWI_FLAG_PRECOW_METRICS;
185187

186188
if (!dst || !src) {
187189
err = __ip_route_output_key(net, rp, &fl);
@@ -209,6 +211,8 @@ static inline int ip_route_newports(struct rtable **rp, u8 protocol,
209211
fl.proto = protocol;
210212
if (inet_sk(sk)->transparent)
211213
fl.flags |= FLOWI_FLAG_ANYSRC;
214+
if (protocol == IPPROTO_TCP)
215+
fl.flags |= FLOWI_FLAG_PRECOW_METRICS;
212216
ip_rt_put(*rp);
213217
*rp = NULL;
214218
security_sk_classify_flow(sk, &fl);

‎net/ipv4/route.c‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,6 +1857,28 @@ static unsigned int ipv4_default_mtu(const struct dst_entry *dst)
18571857
return mtu;
18581858
}
18591859

1860+
static void rt_init_metrics(struct rtable *rt, struct fib_info *fi)
1861+
{
1862+
if (!(rt->fl.flags & FLOWI_FLAG_PRECOW_METRICS)) {
1863+
no_cow:
1864+
rt->fi = fi;
1865+
atomic_inc(&fi->fib_clntref);
1866+
dst_init_metrics(&rt->dst, fi->fib_metrics, true);
1867+
} else {
1868+
struct inet_peer *peer;
1869+
1870+
if (!rt->peer)
1871+
rt_bind_peer(rt, 1);
1872+
peer = rt->peer;
1873+
if (!peer)
1874+
goto no_cow;
1875+
if (inet_metrics_new(peer))
1876+
memcpy(peer->metrics, fi->fib_metrics,
1877+
sizeof(u32) * RTAX_MAX);
1878+
dst_init_metrics(&rt->dst, peer->metrics, false);
1879+
}
1880+
}
1881+
18601882
static void rt_set_nexthop(struct rtable *rt, struct fib_result *res, u32 itag)
18611883
{
18621884
struct dst_entry *dst = &rt->dst;
@@ -1866,9 +1888,7 @@ static void rt_set_nexthop(struct rtable *rt, struct fib_result *res, u32 itag)
18661888
if (FIB_RES_GW(*res) &&
18671889
FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
18681890
rt->rt_gateway = FIB_RES_GW(*res);
1869-
rt->fi = fi;
1870-
atomic_inc(&fi->fib_clntref);
1871-
dst_init_metrics(dst, fi->fib_metrics, true);
1891+
rt_init_metrics(rt, fi);
18721892
#ifdef CONFIG_IP_ROUTE_CLASSID
18731893
dst->tclassid = FIB_RES_NH(*res).nh_tclassid;
18741894
#endif

0 commit comments

Comments
 (0)