Skip to content

Commit f2bb4be

Browse files
committed
ipv4: Cache output routes in fib_info nexthops.
If we have an output route that lacks nexthop exceptions, we can cache it in the FIB info nexthop. Such routes will have DST_HOST cleared because such routes refer to a family of destinations, rather than just one. The sequence of the handling of exceptions during route lookup is adjusted to make the logic work properly. Before we allocate the route, we lookup the exception. Then we know if we will cache this route or not, and therefore whether DST_HOST should be set on the allocated route. Then we use DST_HOST to key off whether we should store the resulting route, during rt_set_nexthop(), in the FIB nexthop cache. With help from Eric Dumazet. Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent ceb3320 commit f2bb4be

3 files changed

Lines changed: 101 additions & 43 deletions

File tree

‎include/net/ip_fib.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ struct fib_config {
4646
};
4747

4848
struct fib_info;
49+
struct rtable;
4950

5051
struct fib_nh_exception {
5152
struct fib_nh_exception __rcu *fnhe_next;
@@ -80,6 +81,7 @@ struct fib_nh {
8081
__be32 nh_gw;
8182
__be32 nh_saddr;
8283
int nh_saddr_genid;
84+
struct rtable *nh_rth_output;
8385
struct fnhe_hash_bucket *nh_exceptions;
8486
};
8587

‎net/ipv4/fib_semantics.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ static void free_fib_info_rcu(struct rcu_head *head)
171171
dev_put(nexthop_nh->nh_dev);
172172
if (nexthop_nh->nh_exceptions)
173173
free_nh_exceptions(nexthop_nh);
174+
if (nexthop_nh->nh_rth_output)
175+
dst_release(&nexthop_nh->nh_rth_output->dst);
174176
} endfor_nexthops(fi);
175177

176178
release_net(fi->fib_net);

‎net/ipv4/route.c‎

Lines changed: 97 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,8 +1158,7 @@ static unsigned int ipv4_mtu(const struct dst_entry *dst)
11581158
return mtu;
11591159
}
11601160

1161-
static void rt_init_metrics(struct rtable *rt, const struct flowi4 *fl4,
1162-
struct fib_info *fi)
1161+
static void rt_init_metrics(struct rtable *rt, struct fib_info *fi)
11631162
{
11641163
if (fi->fib_metrics != (u32 *) dst_default_metrics) {
11651164
rt->fi = fi;
@@ -1168,63 +1167,99 @@ static void rt_init_metrics(struct rtable *rt, const struct flowi4 *fl4,
11681167
dst_init_metrics(&rt->dst, fi->fib_metrics, true);
11691168
}
11701169

1171-
static void rt_bind_exception(struct rtable *rt, struct fib_nh *nh, __be32 daddr)
1170+
static struct fib_nh_exception *find_exception(struct fib_nh *nh, __be32 daddr)
11721171
{
11731172
struct fnhe_hash_bucket *hash = nh->nh_exceptions;
11741173
struct fib_nh_exception *fnhe;
11751174
u32 hval;
11761175

1176+
if (!hash)
1177+
return NULL;
1178+
11771179
hval = fnhe_hashfun(daddr);
11781180

1179-
restart:
11801181
for (fnhe = rcu_dereference(hash[hval].chain); fnhe;
11811182
fnhe = rcu_dereference(fnhe->fnhe_next)) {
1182-
__be32 fnhe_daddr, gw;
1183-
unsigned long expires;
1184-
unsigned int seq;
1185-
u32 pmtu;
1186-
1187-
seq = read_seqbegin(&fnhe_seqlock);
1188-
fnhe_daddr = fnhe->fnhe_daddr;
1189-
gw = fnhe->fnhe_gw;
1190-
pmtu = fnhe->fnhe_pmtu;
1191-
expires = fnhe->fnhe_expires;
1192-
if (read_seqretry(&fnhe_seqlock, seq))
1193-
goto restart;
1194-
if (daddr != fnhe_daddr)
1195-
continue;
1196-
if (pmtu) {
1197-
unsigned long diff = expires - jiffies;
1183+
if (fnhe->fnhe_daddr == daddr)
1184+
return fnhe;
1185+
}
1186+
return NULL;
1187+
}
11981188

1199-
if (time_before(jiffies, expires)) {
1200-
rt->rt_pmtu = pmtu;
1201-
dst_set_expires(&rt->dst, diff);
1202-
}
1203-
}
1204-
if (gw) {
1205-
rt->rt_flags |= RTCF_REDIRECTED;
1206-
rt->rt_gateway = gw;
1189+
static void rt_bind_exception(struct rtable *rt, struct fib_nh_exception *fnhe,
1190+
__be32 daddr)
1191+
{
1192+
__be32 fnhe_daddr, gw;
1193+
unsigned long expires;
1194+
unsigned int seq;
1195+
u32 pmtu;
1196+
1197+
restart:
1198+
seq = read_seqbegin(&fnhe_seqlock);
1199+
fnhe_daddr = fnhe->fnhe_daddr;
1200+
gw = fnhe->fnhe_gw;
1201+
pmtu = fnhe->fnhe_pmtu;
1202+
expires = fnhe->fnhe_expires;
1203+
if (read_seqretry(&fnhe_seqlock, seq))
1204+
goto restart;
1205+
1206+
if (daddr != fnhe_daddr)
1207+
return;
1208+
1209+
if (pmtu) {
1210+
unsigned long diff = expires - jiffies;
1211+
1212+
if (time_before(jiffies, expires)) {
1213+
rt->rt_pmtu = pmtu;
1214+
dst_set_expires(&rt->dst, diff);
12071215
}
1208-
fnhe->fnhe_stamp = jiffies;
1209-
break;
1216+
}
1217+
if (gw) {
1218+
rt->rt_flags |= RTCF_REDIRECTED;
1219+
rt->rt_gateway = gw;
1220+
}
1221+
fnhe->fnhe_stamp = jiffies;
1222+
}
1223+
1224+
static inline void rt_release_rcu(struct rcu_head *head)
1225+
{
1226+
struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head);
1227+
dst_release(dst);
1228+
}
1229+
1230+
static void rt_cache_route(struct fib_nh *nh, struct rtable *rt)
1231+
{
1232+
struct rtable *orig, *prev, **p = &nh->nh_rth_output;
1233+
1234+
orig = *p;
1235+
1236+
prev = cmpxchg(p, orig, rt);
1237+
if (prev == orig) {
1238+
dst_clone(&rt->dst);
1239+
if (orig)
1240+
call_rcu_bh(&orig->dst.rcu_head, rt_release_rcu);
12101241
}
12111242
}
12121243

1213-
static void rt_set_nexthop(struct rtable *rt, const struct flowi4 *fl4,
1244+
static void rt_set_nexthop(struct rtable *rt, __be32 daddr,
12141245
const struct fib_result *res,
1246+
struct fib_nh_exception *fnhe,
12151247
struct fib_info *fi, u16 type, u32 itag)
12161248
{
12171249
if (fi) {
12181250
struct fib_nh *nh = &FIB_RES_NH(*res);
12191251

12201252
if (nh->nh_gw && nh->nh_scope == RT_SCOPE_LINK)
12211253
rt->rt_gateway = nh->nh_gw;
1222-
if (unlikely(nh->nh_exceptions))
1223-
rt_bind_exception(rt, nh, fl4->daddr);
1224-
rt_init_metrics(rt, fl4, fi);
1254+
if (unlikely(fnhe))
1255+
rt_bind_exception(rt, fnhe, daddr);
1256+
rt_init_metrics(rt, fi);
12251257
#ifdef CONFIG_IP_ROUTE_CLASSID
1226-
rt->dst.tclassid = FIB_RES_NH(*res).nh_tclassid;
1258+
rt->dst.tclassid = nh->nh_tclassid;
12271259
#endif
1260+
if (!(rt->dst.flags & DST_HOST) &&
1261+
rt_is_output_route(rt))
1262+
rt_cache_route(nh, rt);
12281263
}
12291264

12301265
#ifdef CONFIG_IP_ROUTE_CLASSID
@@ -1236,10 +1271,10 @@ static void rt_set_nexthop(struct rtable *rt, const struct flowi4 *fl4,
12361271
}
12371272

12381273
static struct rtable *rt_dst_alloc(struct net_device *dev,
1239-
bool nopolicy, bool noxfrm)
1274+
bool nopolicy, bool noxfrm, bool will_cache)
12401275
{
12411276
return dst_alloc(&ipv4_dst_ops, dev, 1, DST_OBSOLETE_FORCE_CHK,
1242-
DST_HOST | DST_NOCACHE |
1277+
(will_cache ? 0 : DST_HOST) | DST_NOCACHE |
12431278
(nopolicy ? DST_NOPOLICY : 0) |
12441279
(noxfrm ? DST_NOXFRM : 0));
12451280
}
@@ -1276,7 +1311,7 @@ static int ip_route_input_mc(struct sk_buff *skb, __be32 daddr, __be32 saddr,
12761311
goto e_err;
12771312
}
12781313
rth = rt_dst_alloc(dev_net(dev)->loopback_dev,
1279-
IN_DEV_CONF_GET(in_dev, NOPOLICY), false);
1314+
IN_DEV_CONF_GET(in_dev, NOPOLICY), false, false);
12801315
if (!rth)
12811316
goto e_nobufs;
12821317

@@ -1349,6 +1384,7 @@ static int __mkroute_input(struct sk_buff *skb,
13491384
__be32 daddr, __be32 saddr, u32 tos,
13501385
struct rtable **result)
13511386
{
1387+
struct fib_nh_exception *fnhe;
13521388
struct rtable *rth;
13531389
int err;
13541390
struct in_device *out_dev;
@@ -1395,9 +1431,13 @@ static int __mkroute_input(struct sk_buff *skb,
13951431
}
13961432
}
13971433

1434+
fnhe = NULL;
1435+
if (res->fi)
1436+
fnhe = find_exception(&FIB_RES_NH(*res), daddr);
1437+
13981438
rth = rt_dst_alloc(out_dev->dev,
13991439
IN_DEV_CONF_GET(in_dev, NOPOLICY),
1400-
IN_DEV_CONF_GET(out_dev, NOXFRM));
1440+
IN_DEV_CONF_GET(out_dev, NOXFRM), false);
14011441
if (!rth) {
14021442
err = -ENOBUFS;
14031443
goto cleanup;
@@ -1416,7 +1456,7 @@ static int __mkroute_input(struct sk_buff *skb,
14161456
rth->dst.input = ip_forward;
14171457
rth->dst.output = ip_output;
14181458

1419-
rt_set_nexthop(rth, NULL, res, res->fi, res->type, itag);
1459+
rt_set_nexthop(rth, daddr, res, fnhe, res->fi, res->type, itag);
14201460

14211461
*result = rth;
14221462
err = 0;
@@ -1558,7 +1598,7 @@ out: return err;
15581598

15591599
local_input:
15601600
rth = rt_dst_alloc(net->loopback_dev,
1561-
IN_DEV_CONF_GET(in_dev, NOPOLICY), false);
1601+
IN_DEV_CONF_GET(in_dev, NOPOLICY), false, false);
15621602
if (!rth)
15631603
goto e_nobufs;
15641604

@@ -1672,6 +1712,7 @@ static struct rtable *__mkroute_output(const struct fib_result *res,
16721712
unsigned int flags)
16731713
{
16741714
struct fib_info *fi = res->fi;
1715+
struct fib_nh_exception *fnhe;
16751716
struct in_device *in_dev;
16761717
u16 type = res->type;
16771718
struct rtable *rth;
@@ -1710,9 +1751,22 @@ static struct rtable *__mkroute_output(const struct fib_result *res,
17101751
fi = NULL;
17111752
}
17121753

1754+
fnhe = NULL;
1755+
if (fi) {
1756+
fnhe = find_exception(&FIB_RES_NH(*res), fl4->daddr);
1757+
if (!fnhe) {
1758+
rth = FIB_RES_NH(*res).nh_rth_output;
1759+
if (rth &&
1760+
rth->dst.obsolete == DST_OBSOLETE_FORCE_CHK) {
1761+
dst_use(&rth->dst, jiffies);
1762+
return rth;
1763+
}
1764+
}
1765+
}
17131766
rth = rt_dst_alloc(dev_out,
17141767
IN_DEV_CONF_GET(in_dev, NOPOLICY),
1715-
IN_DEV_CONF_GET(in_dev, NOXFRM));
1768+
IN_DEV_CONF_GET(in_dev, NOXFRM),
1769+
fi && !fnhe);
17161770
if (!rth)
17171771
return ERR_PTR(-ENOBUFS);
17181772

@@ -1749,7 +1803,7 @@ static struct rtable *__mkroute_output(const struct fib_result *res,
17491803
#endif
17501804
}
17511805

1752-
rt_set_nexthop(rth, fl4, res, fi, type, 0);
1806+
rt_set_nexthop(rth, fl4->daddr, res, fnhe, fi, type, 0);
17531807

17541808
if (fl4->flowi4_flags & FLOWI_FLAG_RT_NOCACHE)
17551809
rth->dst.flags |= DST_NOCACHE;

0 commit comments

Comments
 (0)