Skip to content

Commit d08c4f3

Browse files
David Aherndavem330
authored andcommitted
net: Refactor rtable initialization
All callers to rt_dst_alloc have nearly the same initialization following a successful allocation. Consolidate it into rt_dst_alloc. Signed-off-by: David Ahern <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 64d1def commit d08c4f3

File tree

1 file changed

+33
-52
lines changed

1 file changed

+33
-52
lines changed

net/ipv4/route.c

Lines changed: 33 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,12 +1438,33 @@ static void rt_set_nexthop(struct rtable *rt, __be32 daddr,
14381438
}
14391439

14401440
static struct rtable *rt_dst_alloc(struct net_device *dev,
1441+
unsigned int flags, u16 type,
14411442
bool nopolicy, bool noxfrm, bool will_cache)
14421443
{
1443-
return dst_alloc(&ipv4_dst_ops, dev, 1, DST_OBSOLETE_FORCE_CHK,
1444-
(will_cache ? 0 : (DST_HOST | DST_NOCACHE)) |
1445-
(nopolicy ? DST_NOPOLICY : 0) |
1446-
(noxfrm ? DST_NOXFRM : 0));
1444+
struct rtable *rt;
1445+
1446+
rt = dst_alloc(&ipv4_dst_ops, dev, 1, DST_OBSOLETE_FORCE_CHK,
1447+
(will_cache ? 0 : (DST_HOST | DST_NOCACHE)) |
1448+
(nopolicy ? DST_NOPOLICY : 0) |
1449+
(noxfrm ? DST_NOXFRM : 0));
1450+
1451+
if (rt) {
1452+
rt->rt_genid = rt_genid_ipv4(dev_net(dev));
1453+
rt->rt_flags = flags;
1454+
rt->rt_type = type;
1455+
rt->rt_is_input = 0;
1456+
rt->rt_iif = 0;
1457+
rt->rt_pmtu = 0;
1458+
rt->rt_gateway = 0;
1459+
rt->rt_uses_gateway = 0;
1460+
INIT_LIST_HEAD(&rt->rt_uncached);
1461+
1462+
rt->dst.output = ip_output;
1463+
if (flags & RTCF_LOCAL)
1464+
rt->dst.input = ip_local_deliver;
1465+
}
1466+
1467+
return rt;
14471468
}
14481469

14491470
/* called in rcu_read_lock() section */
@@ -1452,6 +1473,7 @@ static int ip_route_input_mc(struct sk_buff *skb, __be32 daddr, __be32 saddr,
14521473
{
14531474
struct rtable *rth;
14541475
struct in_device *in_dev = __in_dev_get_rcu(dev);
1476+
unsigned int flags = RTCF_MULTICAST;
14551477
u32 itag = 0;
14561478
int err;
14571479

@@ -1477,7 +1499,10 @@ static int ip_route_input_mc(struct sk_buff *skb, __be32 daddr, __be32 saddr,
14771499
if (err < 0)
14781500
goto e_err;
14791501
}
1480-
rth = rt_dst_alloc(dev_net(dev)->loopback_dev,
1502+
if (our)
1503+
flags |= RTCF_LOCAL;
1504+
1505+
rth = rt_dst_alloc(dev_net(dev)->loopback_dev, flags, RTN_MULTICAST,
14811506
IN_DEV_CONF_GET(in_dev, NOPOLICY), false, false);
14821507
if (!rth)
14831508
goto e_nobufs;
@@ -1486,20 +1511,7 @@ static int ip_route_input_mc(struct sk_buff *skb, __be32 daddr, __be32 saddr,
14861511
rth->dst.tclassid = itag;
14871512
#endif
14881513
rth->dst.output = ip_rt_bug;
1489-
1490-
rth->rt_genid = rt_genid_ipv4(dev_net(dev));
1491-
rth->rt_flags = RTCF_MULTICAST;
1492-
rth->rt_type = RTN_MULTICAST;
14931514
rth->rt_is_input= 1;
1494-
rth->rt_iif = 0;
1495-
rth->rt_pmtu = 0;
1496-
rth->rt_gateway = 0;
1497-
rth->rt_uses_gateway = 0;
1498-
INIT_LIST_HEAD(&rth->rt_uncached);
1499-
if (our) {
1500-
rth->dst.input= ip_local_deliver;
1501-
rth->rt_flags |= RTCF_LOCAL;
1502-
}
15031515

15041516
#ifdef CONFIG_IP_MROUTE
15051517
if (!ipv4_is_local_multicast(daddr) && IN_DEV_MFORWARD(in_dev))
@@ -1608,27 +1620,18 @@ static int __mkroute_input(struct sk_buff *skb,
16081620
}
16091621
}
16101622

1611-
rth = rt_dst_alloc(out_dev->dev,
1623+
rth = rt_dst_alloc(out_dev->dev, 0, res->type,
16121624
IN_DEV_CONF_GET(in_dev, NOPOLICY),
16131625
IN_DEV_CONF_GET(out_dev, NOXFRM), do_cache);
16141626
if (!rth) {
16151627
err = -ENOBUFS;
16161628
goto cleanup;
16171629
}
16181630

1619-
rth->rt_genid = rt_genid_ipv4(dev_net(rth->dst.dev));
1620-
rth->rt_flags = 0;
1621-
rth->rt_type = res->type;
16221631
rth->rt_is_input = 1;
1623-
rth->rt_iif = 0;
1624-
rth->rt_pmtu = 0;
1625-
rth->rt_gateway = 0;
1626-
rth->rt_uses_gateway = 0;
1627-
INIT_LIST_HEAD(&rth->rt_uncached);
16281632
RT_CACHE_STAT_INC(in_slow_tot);
16291633

16301634
rth->dst.input = ip_forward;
1631-
rth->dst.output = ip_output;
16321635

16331636
rt_set_nexthop(rth, daddr, res, fnhe, res->fi, res->type, itag);
16341637
if (lwtunnel_output_redirect(rth->dst.lwtstate)) {
@@ -1795,26 +1798,16 @@ out: return err;
17951798
}
17961799
}
17971800

1798-
rth = rt_dst_alloc(net->loopback_dev,
1801+
rth = rt_dst_alloc(net->loopback_dev, flags | RTCF_LOCAL, res.type,
17991802
IN_DEV_CONF_GET(in_dev, NOPOLICY), false, do_cache);
18001803
if (!rth)
18011804
goto e_nobufs;
18021805

1803-
rth->dst.input= ip_local_deliver;
18041806
rth->dst.output= ip_rt_bug;
18051807
#ifdef CONFIG_IP_ROUTE_CLASSID
18061808
rth->dst.tclassid = itag;
18071809
#endif
1808-
1809-
rth->rt_genid = rt_genid_ipv4(net);
1810-
rth->rt_flags = flags|RTCF_LOCAL;
1811-
rth->rt_type = res.type;
18121810
rth->rt_is_input = 1;
1813-
rth->rt_iif = 0;
1814-
rth->rt_pmtu = 0;
1815-
rth->rt_gateway = 0;
1816-
rth->rt_uses_gateway = 0;
1817-
INIT_LIST_HEAD(&rth->rt_uncached);
18181811

18191812
RT_CACHE_STAT_INC(in_slow_tot);
18201813
if (res.type == RTN_UNREACHABLE) {
@@ -1987,28 +1980,16 @@ static struct rtable *__mkroute_output(const struct fib_result *res,
19871980
}
19881981

19891982
add:
1990-
rth = rt_dst_alloc(dev_out,
1983+
rth = rt_dst_alloc(dev_out, flags, type,
19911984
IN_DEV_CONF_GET(in_dev, NOPOLICY),
19921985
IN_DEV_CONF_GET(in_dev, NOXFRM),
19931986
do_cache);
19941987
if (!rth)
19951988
return ERR_PTR(-ENOBUFS);
19961989

1997-
rth->dst.output = ip_output;
1998-
1999-
rth->rt_genid = rt_genid_ipv4(dev_net(dev_out));
2000-
rth->rt_flags = flags;
2001-
rth->rt_type = type;
2002-
rth->rt_is_input = 0;
20031990
rth->rt_iif = orig_oif ? : 0;
2004-
rth->rt_pmtu = 0;
2005-
rth->rt_gateway = 0;
2006-
rth->rt_uses_gateway = 0;
2007-
INIT_LIST_HEAD(&rth->rt_uncached);
20081991
RT_CACHE_STAT_INC(out_slow_tot);
20091992

2010-
if (flags & RTCF_LOCAL)
2011-
rth->dst.input = ip_local_deliver;
20121993
if (flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
20131994
if (flags & RTCF_LOCAL &&
20141995
!(dev_out->flags & IFF_LOOPBACK)) {

0 commit comments

Comments
 (0)