Skip to content

Commit d2d68ba

Browse files
committed
ipv4: Cache input routes in fib_info nexthops.
Caching input routes is slightly simpler than output routes, since we don't need to be concerned with nexthop exceptions. (locally destined, and routed packets, never trigger PMTU events or redirects that will be processed by us). However, we have to elide caching for the DIRECTSRC and non-zero itag cases. Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent f2bb4be commit d2d68ba

3 files changed

Lines changed: 46 additions & 12 deletions

File tree

‎include/net/ip_fib.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ struct fib_nh {
8282
__be32 nh_saddr;
8383
int nh_saddr_genid;
8484
struct rtable *nh_rth_output;
85+
struct rtable *nh_rth_input;
8586
struct fnhe_hash_bucket *nh_exceptions;
8687
};
8788

‎net/ipv4/fib_semantics.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ static void free_fib_info_rcu(struct rcu_head *head)
173173
free_nh_exceptions(nexthop_nh);
174174
if (nexthop_nh->nh_rth_output)
175175
dst_release(&nexthop_nh->nh_rth_output->dst);
176+
if (nexthop_nh->nh_rth_input)
177+
dst_release(&nexthop_nh->nh_rth_input->dst);
176178
} endfor_nexthops(fi);
177179

178180
release_net(fi->fib_net);

‎net/ipv4/route.c‎

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,9 @@ static void rt_cache_route(struct fib_nh *nh, struct rtable *rt)
12311231
{
12321232
struct rtable *orig, *prev, **p = &nh->nh_rth_output;
12331233

1234+
if (rt_is_input_route(rt))
1235+
p = &nh->nh_rth_input;
1236+
12341237
orig = *p;
12351238

12361239
prev = cmpxchg(p, orig, rt);
@@ -1241,6 +1244,11 @@ static void rt_cache_route(struct fib_nh *nh, struct rtable *rt)
12411244
}
12421245
}
12431246

1247+
static bool rt_cache_valid(struct rtable *rt)
1248+
{
1249+
return (rt && rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK);
1250+
}
1251+
12441252
static void rt_set_nexthop(struct rtable *rt, __be32 daddr,
12451253
const struct fib_result *res,
12461254
struct fib_nh_exception *fnhe,
@@ -1257,8 +1265,7 @@ static void rt_set_nexthop(struct rtable *rt, __be32 daddr,
12571265
#ifdef CONFIG_IP_ROUTE_CLASSID
12581266
rt->dst.tclassid = nh->nh_tclassid;
12591267
#endif
1260-
if (!(rt->dst.flags & DST_HOST) &&
1261-
rt_is_output_route(rt))
1268+
if (!(rt->dst.flags & DST_HOST))
12621269
rt_cache_route(nh, rt);
12631270
}
12641271

@@ -1384,11 +1391,11 @@ static int __mkroute_input(struct sk_buff *skb,
13841391
__be32 daddr, __be32 saddr, u32 tos,
13851392
struct rtable **result)
13861393
{
1387-
struct fib_nh_exception *fnhe;
13881394
struct rtable *rth;
13891395
int err;
13901396
struct in_device *out_dev;
13911397
unsigned int flags = 0;
1398+
bool do_cache;
13921399
u32 itag;
13931400

13941401
/* get a working reference to the output device */
@@ -1431,13 +1438,21 @@ static int __mkroute_input(struct sk_buff *skb,
14311438
}
14321439
}
14331440

1434-
fnhe = NULL;
1435-
if (res->fi)
1436-
fnhe = find_exception(&FIB_RES_NH(*res), daddr);
1441+
do_cache = false;
1442+
if (res->fi) {
1443+
if (!(flags & RTCF_DIRECTSRC) && !itag) {
1444+
rth = FIB_RES_NH(*res).nh_rth_input;
1445+
if (rt_cache_valid(rth)) {
1446+
dst_use(&rth->dst, jiffies);
1447+
goto out;
1448+
}
1449+
do_cache = true;
1450+
}
1451+
}
14371452

14381453
rth = rt_dst_alloc(out_dev->dev,
14391454
IN_DEV_CONF_GET(in_dev, NOPOLICY),
1440-
IN_DEV_CONF_GET(out_dev, NOXFRM), false);
1455+
IN_DEV_CONF_GET(out_dev, NOXFRM), do_cache);
14411456
if (!rth) {
14421457
err = -ENOBUFS;
14431458
goto cleanup;
@@ -1456,8 +1471,8 @@ static int __mkroute_input(struct sk_buff *skb,
14561471
rth->dst.input = ip_forward;
14571472
rth->dst.output = ip_output;
14581473

1459-
rt_set_nexthop(rth, daddr, res, fnhe, res->fi, res->type, itag);
1460-
1474+
rt_set_nexthop(rth, daddr, res, NULL, res->fi, res->type, itag);
1475+
out:
14611476
*result = rth;
14621477
err = 0;
14631478
cleanup:
@@ -1509,6 +1524,7 @@ static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
15091524
struct rtable *rth;
15101525
int err = -EINVAL;
15111526
struct net *net = dev_net(dev);
1527+
bool do_cache;
15121528

15131529
/* IP on this device is disabled. */
15141530

@@ -1522,6 +1538,7 @@ static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
15221538
if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr))
15231539
goto martian_source;
15241540

1541+
res.fi = NULL;
15251542
if (ipv4_is_lbcast(daddr) || (saddr == 0 && daddr == 0))
15261543
goto brd_input;
15271544

@@ -1597,8 +1614,20 @@ out: return err;
15971614
RT_CACHE_STAT_INC(in_brd);
15981615

15991616
local_input:
1617+
do_cache = false;
1618+
if (res.fi) {
1619+
if (!(flags & RTCF_DIRECTSRC) && !itag) {
1620+
rth = FIB_RES_NH(res).nh_rth_input;
1621+
if (rt_cache_valid(rth)) {
1622+
dst_use(&rth->dst, jiffies);
1623+
goto set_and_out;
1624+
}
1625+
do_cache = true;
1626+
}
1627+
}
1628+
16001629
rth = rt_dst_alloc(net->loopback_dev,
1601-
IN_DEV_CONF_GET(in_dev, NOPOLICY), false, false);
1630+
IN_DEV_CONF_GET(in_dev, NOPOLICY), false, do_cache);
16021631
if (!rth)
16031632
goto e_nobufs;
16041633

@@ -1622,6 +1651,9 @@ out: return err;
16221651
rth->dst.error= -err;
16231652
rth->rt_flags &= ~RTCF_LOCAL;
16241653
}
1654+
if (do_cache)
1655+
rt_cache_route(&FIB_RES_NH(res), rth);
1656+
set_and_out:
16251657
skb_dst_set(skb, &rth->dst);
16261658
err = 0;
16271659
goto out;
@@ -1756,8 +1788,7 @@ static struct rtable *__mkroute_output(const struct fib_result *res,
17561788
fnhe = find_exception(&FIB_RES_NH(*res), fl4->daddr);
17571789
if (!fnhe) {
17581790
rth = FIB_RES_NH(*res).nh_rth_output;
1759-
if (rth &&
1760-
rth->dst.obsolete == DST_OBSOLETE_FORCE_CHK) {
1791+
if (rt_cache_valid(rth)) {
17611792
dst_use(&rth->dst, jiffies);
17621793
return rth;
17631794
}

0 commit comments

Comments
 (0)