Skip to content

Commit 2b2450c

Browse files
dsaherndavem330
authored andcommitted
ipv6: Move gateway checks to a fib6_nh setting
The gateway setting is not per fib6_info entry but per-fib6_nh. Add a new fib_nh_has_gw flag to fib6_nh and convert references to RTF_GATEWAY to the new flag. For IPv6 address the flag is cheaper than checking that nh_gw is non-0 like IPv4 does. While this increases fib6_nh by 8-bytes, the effective allocation size of a fib6_info is unchanged. The 8 bytes is recovered later with a fib_nh_common change. Signed-off-by: David Ahern <dsahern@gmail.com> Reviewed-by: Ido Schimmel <idosch@mellanox.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent dac7d0f commit 2b2450c

7 files changed

Lines changed: 41 additions & 30 deletions

File tree

‎drivers/net/ethernet/mellanox/mlxsw/spectrum_router.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4913,7 +4913,7 @@ static void mlxsw_sp_rt6_destroy(struct mlxsw_sp_rt6 *mlxsw_sp_rt6)
49134913
static bool mlxsw_sp_fib6_rt_can_mp(const struct fib6_info *rt)
49144914
{
49154915
/* RTF_CACHE routes are ignored */
4916-
return (rt->fib6_flags & (RTF_GATEWAY | RTF_ADDRCONF)) == RTF_GATEWAY;
4916+
return !(rt->fib6_flags & RTF_ADDRCONF) && rt->fib6_nh.fib_nh_has_gw;
49174917
}
49184918

49194919
static struct fib6_info *
@@ -5053,7 +5053,7 @@ static void mlxsw_sp_nexthop6_fini(struct mlxsw_sp *mlxsw_sp,
50535053
static bool mlxsw_sp_rt6_is_gateway(const struct mlxsw_sp *mlxsw_sp,
50545054
const struct fib6_info *rt)
50555055
{
5056-
return rt->fib6_flags & RTF_GATEWAY ||
5056+
return rt->fib6_nh.fib_nh_has_gw ||
50575057
mlxsw_sp_nexthop6_ipip_type(mlxsw_sp, rt, NULL);
50585058
}
50595059

‎include/net/ip6_fib.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ struct rt6_exception {
126126

127127
struct fib6_nh {
128128
struct in6_addr nh_gw;
129+
bool fib_nh_has_gw;
129130
struct net_device *nh_dev;
130131
struct lwtunnel_state *nh_lwtstate;
131132

‎include/net/ip6_route.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ static inline bool rt6_need_strict(const struct in6_addr *daddr)
6868

6969
static inline bool rt6_qualify_for_ecmp(const struct fib6_info *f6i)
7070
{
71-
return (f6i->fib6_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
72-
RTF_GATEWAY;
71+
return !(f6i->fib6_flags & (RTF_ADDRCONF|RTF_DYNAMIC)) &&
72+
f6i->fib6_nh.fib_nh_has_gw;
7373
}
7474

7575
void ip6_route_input(struct sk_buff *skb);

‎net/core/filter.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4751,7 +4751,7 @@ static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
47514751
if (f6i->fib6_nh.nh_lwtstate)
47524752
return BPF_FIB_LKUP_RET_UNSUPP_LWT;
47534753

4754-
if (f6i->fib6_flags & RTF_GATEWAY)
4754+
if (f6i->fib6_nh.fib_nh_has_gw)
47554755
*dst = f6i->fib6_nh.nh_gw;
47564756

47574757
dev = f6i->fib6_nh.nh_dev;

‎net/ipv6/addrconf.c‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ static int addrconf_ifdown(struct net_device *dev, int how);
173173
static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
174174
int plen,
175175
const struct net_device *dev,
176-
u32 flags, u32 noflags);
176+
u32 flags, u32 noflags,
177+
bool no_gw);
177178

178179
static void addrconf_dad_start(struct inet6_ifaddr *ifp);
179180
static void addrconf_dad_work(struct work_struct *w);
@@ -1230,10 +1231,8 @@ cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_r
12301231
{
12311232
struct fib6_info *f6i;
12321233

1233-
f6i = addrconf_get_prefix_route(&ifp->addr,
1234-
ifp->prefix_len,
1235-
ifp->idev->dev,
1236-
0, RTF_GATEWAY | RTF_DEFAULT);
1234+
f6i = addrconf_get_prefix_route(&ifp->addr, ifp->prefix_len,
1235+
ifp->idev->dev, 0, RTF_DEFAULT, true);
12371236
if (f6i) {
12381237
if (del_rt)
12391238
ip6_del_rt(dev_net(ifp->idev->dev), f6i);
@@ -2402,7 +2401,8 @@ addrconf_prefix_route(struct in6_addr *pfx, int plen, u32 metric,
24022401
static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
24032402
int plen,
24042403
const struct net_device *dev,
2405-
u32 flags, u32 noflags)
2404+
u32 flags, u32 noflags,
2405+
bool no_gw)
24062406
{
24072407
struct fib6_node *fn;
24082408
struct fib6_info *rt = NULL;
@@ -2421,6 +2421,8 @@ static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
24212421
for_each_fib6_node_rt_rcu(fn) {
24222422
if (rt->fib6_nh.nh_dev->ifindex != dev->ifindex)
24232423
continue;
2424+
if (no_gw && rt->fib6_nh.fib_nh_has_gw)
2425+
continue;
24242426
if ((rt->fib6_flags & flags) != flags)
24252427
continue;
24262428
if ((rt->fib6_flags & noflags) != 0)
@@ -2717,7 +2719,7 @@ void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
27172719
pinfo->prefix_len,
27182720
dev,
27192721
RTF_ADDRCONF | RTF_PREFIX_RT,
2720-
RTF_GATEWAY | RTF_DEFAULT);
2722+
RTF_DEFAULT, true);
27212723

27222724
if (rt) {
27232725
/* Autoconf prefix route */
@@ -4588,10 +4590,8 @@ static int modify_prefix_route(struct inet6_ifaddr *ifp,
45884590
struct fib6_info *f6i;
45894591
u32 prio;
45904592

4591-
f6i = addrconf_get_prefix_route(&ifp->addr,
4592-
ifp->prefix_len,
4593-
ifp->idev->dev,
4594-
0, RTF_GATEWAY | RTF_DEFAULT);
4593+
f6i = addrconf_get_prefix_route(&ifp->addr, ifp->prefix_len,
4594+
ifp->idev->dev, 0, RTF_DEFAULT, true);
45954595
if (!f6i)
45964596
return -ENOENT;
45974597

@@ -5972,7 +5972,8 @@ static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
59725972
struct fib6_info *rt;
59735973

59745974
rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5975-
ifp->idev->dev, 0, 0);
5975+
ifp->idev->dev, 0, 0,
5976+
false);
59765977
if (rt)
59775978
ip6_del_rt(net, rt);
59785979
}

‎net/ipv6/ip6_fib.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,6 +2294,7 @@ static int ipv6_route_seq_show(struct seq_file *seq, void *v)
22942294
{
22952295
struct fib6_info *rt = v;
22962296
struct ipv6_route_iter *iter = seq->private;
2297+
unsigned int flags = rt->fib6_flags;
22972298
const struct net_device *dev;
22982299

22992300
seq_printf(seq, "%pi6 %02x ", &rt->fib6_dst.addr, rt->fib6_dst.plen);
@@ -2303,15 +2304,17 @@ static int ipv6_route_seq_show(struct seq_file *seq, void *v)
23032304
#else
23042305
seq_puts(seq, "00000000000000000000000000000000 00 ");
23052306
#endif
2306-
if (rt->fib6_flags & RTF_GATEWAY)
2307+
if (rt->fib6_nh.fib_nh_has_gw) {
2308+
flags |= RTF_GATEWAY;
23072309
seq_printf(seq, "%pi6", &rt->fib6_nh.nh_gw);
2308-
else
2310+
} else {
23092311
seq_puts(seq, "00000000000000000000000000000000");
2312+
}
23102313

23112314
dev = rt->fib6_nh.nh_dev;
23122315
seq_printf(seq, " %08x %08x %08x %08x %8s\n",
23132316
rt->fib6_metric, atomic_read(&rt->fib6_ref), 0,
2314-
rt->fib6_flags, dev ? dev->name : "");
2317+
flags, dev ? dev->name : "");
23152318
iter->w.leaf = NULL;
23162319
return 0;
23172320
}

‎net/ipv6/route.c‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ static void rt6_probe(struct fib6_info *rt)
533533
* Router Reachability Probe MUST be rate-limited
534534
* to no more than one per minute.
535535
*/
536-
if (!rt || !(rt->fib6_flags & RTF_GATEWAY))
536+
if (!rt || !rt->fib6_nh.fib_nh_has_gw)
537537
return;
538538

539539
nh_gw = &rt->fib6_nh.nh_gw;
@@ -595,7 +595,7 @@ static inline enum rt6_nud_state rt6_check_neigh(struct fib6_info *rt)
595595
struct neighbour *neigh;
596596

597597
if (rt->fib6_flags & RTF_NONEXTHOP ||
598-
!(rt->fib6_flags & RTF_GATEWAY))
598+
!rt->fib6_nh.fib_nh_has_gw)
599599
return RT6_NUD_SUCCEED;
600600

601601
rcu_read_lock_bh();
@@ -784,7 +784,7 @@ static struct fib6_info *rt6_select(struct net *net, struct fib6_node *fn,
784784

785785
static bool rt6_is_gw_or_nonexthop(const struct fib6_info *rt)
786786
{
787-
return (rt->fib6_flags & (RTF_NONEXTHOP | RTF_GATEWAY));
787+
return (rt->fib6_flags & RTF_NONEXTHOP) || rt->fib6_nh.fib_nh_has_gw;
788788
}
789789

790790
#ifdef CONFIG_IPV6_ROUTE_INFO
@@ -989,8 +989,11 @@ static void ip6_rt_copy_init(struct rt6_info *rt, struct fib6_info *ort)
989989

990990
rt->rt6i_dst = ort->fib6_dst;
991991
rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
992-
rt->rt6i_gateway = ort->fib6_nh.nh_gw;
993992
rt->rt6i_flags = ort->fib6_flags;
993+
if (ort->fib6_nh.fib_nh_has_gw) {
994+
rt->rt6i_gateway = ort->fib6_nh.nh_gw;
995+
rt->rt6i_flags |= RTF_GATEWAY;
996+
}
994997
rt6_set_from(rt, ort);
995998
#ifdef CONFIG_IPV6_SUBTREES
996999
rt->rt6i_src = ort->fib6_src;
@@ -1872,7 +1875,7 @@ struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
18721875
rcu_read_unlock();
18731876
return rt;
18741877
} else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
1875-
!(f6i->fib6_flags & RTF_GATEWAY))) {
1878+
!f6i->fib6_nh.fib_nh_has_gw)) {
18761879
/* Create a RTF_CACHE clone which will not be
18771880
* owned by the fib6 tree. It is for the special case where
18781881
* the daddr in the skb during the neighbor look-up is different
@@ -2442,7 +2445,7 @@ static struct rt6_info *__ip6_route_redirect(struct net *net,
24422445
continue;
24432446
if (rt->fib6_flags & RTF_REJECT)
24442447
break;
2445-
if (!(rt->fib6_flags & RTF_GATEWAY))
2448+
if (!rt->fib6_nh.fib_nh_has_gw)
24462449
continue;
24472450
if (fl6->flowi6_oif != rt->fib6_nh.nh_dev->ifindex)
24482451
continue;
@@ -2986,6 +2989,7 @@ int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
29862989
goto out;
29872990

29882991
fib6_nh->nh_gw = cfg->fc_gateway;
2992+
fib6_nh->fib_nh_has_gw = 1;
29892993
}
29902994

29912995
err = -ENODEV;
@@ -3121,7 +3125,7 @@ static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
31213125
rt->fib6_table = table;
31223126
rt->fib6_metric = cfg->fc_metric;
31233127
rt->fib6_type = cfg->fc_type;
3124-
rt->fib6_flags = cfg->fc_flags;
3128+
rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
31253129

31263130
ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
31273131
rt->fib6_dst.plen = cfg->fc_dst_len;
@@ -3490,7 +3494,8 @@ static struct fib6_info *rt6_get_route_info(struct net *net,
34903494
for_each_fib6_node_rt_rcu(fn) {
34913495
if (rt->fib6_nh.nh_dev->ifindex != ifindex)
34923496
continue;
3493-
if ((rt->fib6_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
3497+
if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
3498+
!rt->fib6_nh.fib_nh_has_gw)
34943499
continue;
34953500
if (!ipv6_addr_equal(&rt->fib6_nh.nh_gw, gwaddr))
34963501
continue;
@@ -3811,14 +3816,15 @@ void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
38113816
fib6_clean_all(net, fib6_remove_prefsrc, &adni);
38123817
}
38133818

3814-
#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY)
3819+
#define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT)
38153820

38163821
/* Remove routers and update dst entries when gateway turn into host. */
38173822
static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
38183823
{
38193824
struct in6_addr *gateway = (struct in6_addr *)arg;
38203825

38213826
if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
3827+
rt->fib6_nh.fib_nh_has_gw &&
38223828
ipv6_addr_equal(gateway, &rt->fib6_nh.nh_gw)) {
38233829
return -1;
38243830
}
@@ -4607,7 +4613,7 @@ static int rt6_nexthop_info(struct sk_buff *skb, struct fib6_info *rt,
46074613
rcu_read_unlock();
46084614
}
46094615

4610-
if (rt->fib6_flags & RTF_GATEWAY) {
4616+
if (rt->fib6_nh.fib_nh_has_gw) {
46114617
if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->fib6_nh.nh_gw) < 0)
46124618
goto nla_put_failure;
46134619
}

0 commit comments

Comments
 (0)