提交 389cb1ec 编写于 作者: J Jakub Kicinski

Merge branch 'add-notifications-when-route-hardware-flags-change'

Ido Schimmel says:

====================
Add notifications when route hardware flags change

Routes installed to the kernel can be programmed to capable devices, in
which case they are marked with one of two flags. RTM_F_OFFLOAD for
routes that offload traffic from the kernel and RTM_F_TRAP for routes
that trap packets to the kernel for processing (e.g., host routes).

These flags are of interest to routing daemons since they would like to
delay advertisement of routes until they are installed in hardware. This
allows them to avoid packet loss or misrouted packets. Currently,
routing daemons do not receive any notifications when these flags are
changed, requiring them to poll the kernel tables for changes which is
inefficient.

This series addresses the issue by having the kernel emit RTM_NEWROUTE
notifications whenever these flags change. The behavior is controlled by
two sysctls (net.ipv4.fib_notify_on_flag_change and
net.ipv6.fib_notify_on_flag_change) that default to 0 (no
notifications).

Note that even if route installation in hardware is improved to be more
synchronous, these notifications are still of interest. For example, a
multipath route can change from RTM_F_OFFLOAD to RTM_F_TRAP if its
neighbours become invalid. A routing daemon can choose to withdraw /
replace the route in that case. In addition, the deletion of a route
from the kernel can prompt the installation of an identical route
(already in kernel, with an higher metric) to hardware.

For testing purposes, netdevsim is aligned to simulate a "real" driver
that programs routes to hardware.

Series overview:

Patches #1-#2 align netdevsim to perform route programming in a
non-atomic context

Patches #3-#5 add sysctl to control IPv4 notifications

Patches #6-#8 add sysctl to control IPv6 notifications

Patch #9 extends existing fib tests to set sysctls before running tests

Patch #10 adds test for fib notifications over netdevsim
====================

Link: https://lore.kernel.org/r/20210201194757.3463461-1-idosch@idosch.orgSigned-off-by: NJakub Kicinski <kuba@kernel.org>
......@@ -178,6 +178,26 @@ min_adv_mss - INTEGER
The advertised MSS depends on the first hop route MTU, but will
never be lower than this setting.
fib_notify_on_flag_change - INTEGER
Whether to emit RTM_NEWROUTE notifications whenever RTM_F_OFFLOAD/
RTM_F_TRAP flags are changed.
After installing a route to the kernel, user space receives an
acknowledgment, which means the route was installed in the kernel,
but not necessarily in hardware.
It is also possible for a route already installed in hardware to change
its action and therefore its flags. For example, a host route that is
trapping packets can be "promoted" to perform decapsulation following
the installation of an IPinIP/VXLAN tunnel.
The notifications will indicate to user-space the state of the route.
Default: 0 (Do not emit notifications.)
Possible values:
- 0 - Do not emit notifications.
- 1 - Emit notifications.
IP Fragmentation:
ipfrag_high_thresh - LONG INTEGER
......@@ -1775,6 +1795,26 @@ nexthop_compat_mode - BOOLEAN
and extraneous notifications.
Default: true (backward compat mode)
fib_notify_on_flag_change - INTEGER
Whether to emit RTM_NEWROUTE notifications whenever RTM_F_OFFLOAD/
RTM_F_TRAP flags are changed.
After installing a route to the kernel, user space receives an
acknowledgment, which means the route was installed in the kernel,
but not necessarily in hardware.
It is also possible for a route already installed in hardware to change
its action and therefore its flags. For example, a host route that is
trapping packets can be "promoted" to perform decapsulation following
the installation of an IPinIP/VXLAN tunnel.
The notifications will indicate to user-space the state of the route.
Default: 0 (Do not emit notifications.)
Possible values:
- 0 - Do not emit notifications.
- 1 - Emit notifications.
IPv6 Fragmentation:
ip6frag_high_thresh - INTEGER
......
......@@ -4988,6 +4988,7 @@ mlxsw_sp_fib4_entry_hw_flags_clear(struct mlxsw_sp *mlxsw_sp,
fib_alias_hw_flags_set(mlxsw_sp_net(mlxsw_sp), &fri);
}
#if IS_ENABLED(CONFIG_IPV6)
static void
mlxsw_sp_fib6_entry_hw_flags_set(struct mlxsw_sp *mlxsw_sp,
struct mlxsw_sp_fib_entry *fib_entry)
......@@ -5004,10 +5005,18 @@ mlxsw_sp_fib6_entry_hw_flags_set(struct mlxsw_sp *mlxsw_sp,
fib6_entry = container_of(fib_entry, struct mlxsw_sp_fib6_entry,
common);
list_for_each_entry(mlxsw_sp_rt6, &fib6_entry->rt6_list, list)
fib6_info_hw_flags_set(mlxsw_sp_rt6->rt, should_offload,
!should_offload);
fib6_info_hw_flags_set(mlxsw_sp_net(mlxsw_sp), mlxsw_sp_rt6->rt,
should_offload, !should_offload);
}
#else
static void
mlxsw_sp_fib6_entry_hw_flags_set(struct mlxsw_sp *mlxsw_sp,
struct mlxsw_sp_fib_entry *fib_entry)
{
}
#endif
#if IS_ENABLED(CONFIG_IPV6)
static void
mlxsw_sp_fib6_entry_hw_flags_clear(struct mlxsw_sp *mlxsw_sp,
struct mlxsw_sp_fib_entry *fib_entry)
......@@ -5018,8 +5027,16 @@ mlxsw_sp_fib6_entry_hw_flags_clear(struct mlxsw_sp *mlxsw_sp,
fib6_entry = container_of(fib_entry, struct mlxsw_sp_fib6_entry,
common);
list_for_each_entry(mlxsw_sp_rt6, &fib6_entry->rt6_list, list)
fib6_info_hw_flags_set(mlxsw_sp_rt6->rt, false, false);
fib6_info_hw_flags_set(mlxsw_sp_net(mlxsw_sp), mlxsw_sp_rt6->rt,
false, false);
}
#else
static void
mlxsw_sp_fib6_entry_hw_flags_clear(struct mlxsw_sp *mlxsw_sp,
struct mlxsw_sp_fib_entry *fib_entry)
{
}
#endif
static void
mlxsw_sp_fib_entry_hw_flags_set(struct mlxsw_sp *mlxsw_sp,
......
此差异已折叠。
......@@ -336,13 +336,6 @@ static inline void fib6_info_release(struct fib6_info *f6i)
call_rcu(&f6i->rcu, fib6_info_destroy_rcu);
}
static inline void fib6_info_hw_flags_set(struct fib6_info *f6i, bool offload,
bool trap)
{
f6i->offload = offload;
f6i->trap = trap;
}
enum fib6_walk_state {
#ifdef CONFIG_IPV6_SUBTREES
FWS_S,
......@@ -545,6 +538,8 @@ static inline bool fib6_metric_locked(struct fib6_info *f6i, int metric)
{
return !!(f6i->fib6_metrics->metrics[RTAX_LOCK - 1] & (1 << metric));
}
void fib6_info_hw_flags_set(struct net *net, struct fib6_info *f6i,
bool offload, bool trap);
#if IS_BUILTIN(CONFIG_IPV6) && defined(CONFIG_BPF_SYSCALL)
struct bpf_iter__ipv6_route {
......
......@@ -188,6 +188,8 @@ struct netns_ipv4 {
int sysctl_udp_wmem_min;
int sysctl_udp_rmem_min;
int sysctl_fib_notify_on_flag_change;
#ifdef CONFIG_NET_L3_MASTER_DEV
int sysctl_udp_l3mdev_accept;
#endif
......
......@@ -51,6 +51,7 @@ struct netns_sysctl_ipv6 {
int max_hbh_opts_len;
int seg6_flowlabel;
bool skip_notify_on_dev_down;
int fib_notify_on_flag_change;
};
struct netns_ipv6 {
......
......@@ -1871,6 +1871,8 @@ static __net_init int inet_init_net(struct net *net)
net->ipv4.sysctl_igmp_llm_reports = 1;
net->ipv4.sysctl_igmp_qrv = 2;
net->ipv4.sysctl_fib_notify_on_flag_change = 0;
return 0;
}
......
......@@ -39,9 +39,10 @@ int fib_nh_match(struct net *net, struct fib_config *cfg, struct fib_info *fi,
struct netlink_ext_ack *extack);
bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi);
int fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
struct fib_rt_info *fri, unsigned int flags);
const struct fib_rt_info *fri, unsigned int flags);
void rtmsg_fib(int event, __be32 key, struct fib_alias *fa, int dst_len,
u32 tb_id, const struct nl_info *info, unsigned int nlm_flags);
size_t fib_nlmsg_size(struct fib_info *fi);
static inline void fib_result_assign(struct fib_result *res,
struct fib_info *fi)
......
......@@ -452,7 +452,7 @@ int ip_fib_check_default(__be32 gw, struct net_device *dev)
return -1;
}
static inline size_t fib_nlmsg_size(struct fib_info *fi)
size_t fib_nlmsg_size(struct fib_info *fi)
{
size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
+ nla_total_size(4) /* RTA_TABLE */
......@@ -1733,7 +1733,7 @@ static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
#endif
int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
struct fib_rt_info *fri, unsigned int flags)
const struct fib_rt_info *fri, unsigned int flags)
{
unsigned int nhs = fib_info_num_path(fri->fi);
struct fib_info *fi = fri->fi;
......
......@@ -1038,6 +1038,8 @@ fib_find_matching_alias(struct net *net, const struct fib_rt_info *fri)
void fib_alias_hw_flags_set(struct net *net, const struct fib_rt_info *fri)
{
struct fib_alias *fa_match;
struct sk_buff *skb;
int err;
rcu_read_lock();
......@@ -1045,9 +1047,34 @@ void fib_alias_hw_flags_set(struct net *net, const struct fib_rt_info *fri)
if (!fa_match)
goto out;
if (fa_match->offload == fri->offload && fa_match->trap == fri->trap)
goto out;
fa_match->offload = fri->offload;
fa_match->trap = fri->trap;
if (!net->ipv4.sysctl_fib_notify_on_flag_change)
goto out;
skb = nlmsg_new(fib_nlmsg_size(fa_match->fa_info), GFP_ATOMIC);
if (!skb) {
err = -ENOBUFS;
goto errout;
}
err = fib_dump_info(skb, 0, 0, RTM_NEWROUTE, fri, 0);
if (err < 0) {
/* -EMSGSIZE implies BUG in fib_nlmsg_size() */
WARN_ON(err == -EMSGSIZE);
kfree_skb(skb);
goto errout;
}
rtnl_notify(skb, net, 0, RTNLGRP_IPV4_ROUTE, NULL, GFP_ATOMIC);
goto out;
errout:
rtnl_set_sk_err(net, RTNLGRP_IPV4_ROUTE, err);
out:
rcu_read_unlock();
}
......
......@@ -1354,6 +1354,15 @@ static struct ctl_table ipv4_net_table[] = {
.proc_handler = proc_dointvec_minmax,
.extra1 = SYSCTL_ONE
},
{
.procname = "fib_notify_on_flag_change",
.data = &init_net.ipv4.sysctl_fib_notify_on_flag_change,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec_minmax,
.extra1 = SYSCTL_ZERO,
.extra2 = SYSCTL_ONE,
},
{ }
};
......
......@@ -954,6 +954,7 @@ static int __net_init inet6_net_init(struct net *net)
net->ipv6.sysctl.max_hbh_opts_cnt = IP6_DEFAULT_MAX_HBH_OPTS_CNT;
net->ipv6.sysctl.max_dst_opts_len = IP6_DEFAULT_MAX_DST_OPTS_LEN;
net->ipv6.sysctl.max_hbh_opts_len = IP6_DEFAULT_MAX_HBH_OPTS_LEN;
net->ipv6.sysctl.fib_notify_on_flag_change = 0;
atomic_set(&net->ipv6.fib6_sernum, 1);
err = ipv6_init_mibs(net);
......
......@@ -6064,6 +6064,50 @@ void fib6_rt_update(struct net *net, struct fib6_info *rt,
rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
}
void fib6_info_hw_flags_set(struct net *net, struct fib6_info *f6i,
bool offload, bool trap)
{
struct sk_buff *skb;
int err;
if (f6i->offload == offload && f6i->trap == trap)
return;
f6i->offload = offload;
f6i->trap = trap;
if (!rcu_access_pointer(f6i->fib6_node))
/* The route was removed from the tree, do not send
* notfication.
*/
return;
if (!net->ipv6.sysctl.fib_notify_on_flag_change)
return;
skb = nlmsg_new(rt6_nlmsg_size(f6i), GFP_KERNEL);
if (!skb) {
err = -ENOBUFS;
goto errout;
}
err = rt6_fill_node(net, skb, f6i, NULL, NULL, NULL, 0, RTM_NEWROUTE, 0,
0, 0);
if (err < 0) {
/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
WARN_ON(err == -EMSGSIZE);
kfree_skb(skb);
goto errout;
}
rtnl_notify(skb, net, 0, RTNLGRP_IPV6_ROUTE, NULL, GFP_KERNEL);
return;
errout:
rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
}
EXPORT_SYMBOL(fib6_info_hw_flags_set);
static int ip6_route_dev_notify(struct notifier_block *this,
unsigned long event, void *ptr)
{
......
......@@ -160,6 +160,15 @@ static struct ctl_table ipv6_table_template[] = {
.mode = 0644,
.proc_handler = proc_dointvec
},
{
.procname = "fib_notify_on_flag_change",
.data = &init_net.ipv6.sysctl.fib_notify_on_flag_change,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec_minmax,
.extra1 = SYSCTL_ZERO,
.extra2 = SYSCTL_ONE,
},
{ }
};
......
......@@ -225,6 +225,16 @@ ipv6_local_replace()
ip -n $ns link del dev dummy1
}
fib_notify_on_flag_change_set()
{
local notify=$1; shift
ip netns exec testns1 sysctl -qw net.ipv4.fib_notify_on_flag_change=$notify
ip netns exec testns1 sysctl -qw net.ipv6.fib_notify_on_flag_change=$notify
log_info "Set fib_notify_on_flag_change to $notify"
}
setup_prepare()
{
ip netns add testns1
......@@ -251,6 +261,10 @@ trap cleanup EXIT
setup_prepare
fib_notify_on_flag_change_set 1
tests_run
fib_notify_on_flag_change_set 0
tests_run
exit $EXIT_STATUS
......@@ -302,6 +302,16 @@ ipv6_error_path()
ipv6_error_path_replay
}
fib_notify_on_flag_change_set()
{
local notify=$1; shift
ip netns exec testns1 sysctl -qw net.ipv4.fib_notify_on_flag_change=$notify
ip netns exec testns1 sysctl -qw net.ipv6.fib_notify_on_flag_change=$notify
log_info "Set fib_notify_on_flag_change to $notify"
}
setup_prepare()
{
local netdev
......@@ -336,6 +346,10 @@ trap cleanup EXIT
setup_prepare
fib_notify_on_flag_change_set 1
tests_run
fib_notify_on_flag_change_set 0
tests_run
exit $EXIT_STATUS
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
lib_dir=$(dirname $0)/../../../net/forwarding
ALL_TESTS="
ipv4_route_addition_test
ipv4_route_deletion_test
ipv4_route_replacement_test
ipv6_route_addition_test
ipv6_route_deletion_test
ipv6_route_replacement_test
"
NETDEVSIM_PATH=/sys/bus/netdevsim/
DEV_ADDR=1337
DEV=netdevsim${DEV_ADDR}
DEVLINK_DEV=netdevsim/${DEV}
SYSFS_NET_DIR=/sys/bus/netdevsim/devices/$DEV/net/
NUM_NETIFS=0
source $lib_dir/lib.sh
check_rt_trap()
{
local outfile=$1; shift
local line
# Make sure that the first notification was emitted without RTM_F_TRAP
# flag and the second with RTM_F_TRAP flag
head -n 1 $outfile | grep -q "rt_trap"
if [[ $? -eq 0 ]]; then
return 1
fi
head -n 2 $outfile | tail -n 1 | grep -q "rt_trap"
}
route_notify_check()
{
local outfile=$1; shift
local expected_num_lines=$1; shift
# check the monitor results
lines=`wc -l $outfile | cut "-d " -f1`
test $lines -eq $expected_num_lines
check_err $? "$expected_num_lines notifications were expected but $lines were received"
if [[ $expected_num_lines -eq 2 ]]; then
check_rt_trap $outfile
check_err $? "Wrong RTM_F_TRAP flags in notifications"
fi
}
route_addition_check()
{
local ip=$1; shift
local notify=$1; shift
local route=$1; shift
local expected_num_notifications=$1; shift
ip netns exec testns1 sysctl -qw net.$ip.fib_notify_on_flag_change=$notify
local outfile=$(mktemp)
$IP monitor route &> $outfile &
sleep 1
$IP route add $route dev dummy1
sleep 1
kill %% && wait %% &> /dev/null
route_notify_check $outfile $expected_num_notifications
rm -f $outfile
$IP route del $route dev dummy1
}
ipv4_route_addition_test()
{
RET=0
local ip="ipv4"
local route=192.0.2.0/24
# Make sure a single notification will be emitted for the programmed
# route.
local notify=0
local expected_num_notifications=1
# route_addition_check will assign value to RET.
route_addition_check $ip $notify $route $expected_num_notifications
# Make sure two notifications will be emitted for the programmed route.
notify=1
expected_num_notifications=2
route_addition_check $ip $notify $route $expected_num_notifications
log_test "IPv4 route addition"
}
route_deletion_check()
{
local ip=$1; shift
local notify=$1; shift
local route=$1; shift
local expected_num_notifications=$1; shift
ip netns exec testns1 sysctl -qw net.$ip.fib_notify_on_flag_change=$notify
$IP route add $route dev dummy1
sleep 1
local outfile=$(mktemp)
$IP monitor route &> $outfile &
sleep 1
$IP route del $route dev dummy1
sleep 1
kill %% && wait %% &> /dev/null
route_notify_check $outfile $expected_num_notifications
rm -f $outfile
}
ipv4_route_deletion_test()
{
RET=0
local ip="ipv4"
local route=192.0.2.0/24
local expected_num_notifications=1
# Make sure a single notification will be emitted for the deleted route,
# regardless of fib_notify_on_flag_change value.
local notify=0
# route_deletion_check will assign value to RET.
route_deletion_check $ip $notify $route $expected_num_notifications
notify=1
route_deletion_check $ip $notify $route $expected_num_notifications
log_test "IPv4 route deletion"
}
route_replacement_check()
{
local ip=$1; shift
local notify=$1; shift
local route=$1; shift
local expected_num_notifications=$1; shift
ip netns exec testns1 sysctl -qw net.$ip.fib_notify_on_flag_change=$notify
$IP route add $route dev dummy1
sleep 1
local outfile=$(mktemp)
$IP monitor route &> $outfile &
sleep 1
$IP route replace $route dev dummy2
sleep 1
kill %% && wait %% &> /dev/null
route_notify_check $outfile $expected_num_notifications
rm -f $outfile
$IP route del $route dev dummy2
}
ipv4_route_replacement_test()
{
RET=0
local ip="ipv4"
local route=192.0.2.0/24
$IP link add name dummy2 type dummy
$IP link set dev dummy2 up
# Make sure a single notification will be emitted for the new route.
local notify=0
local expected_num_notifications=1
# route_replacement_check will assign value to RET.
route_replacement_check $ip $notify $route $expected_num_notifications
# Make sure two notifications will be emitted for the new route.
notify=1
expected_num_notifications=2
route_replacement_check $ip $notify $route $expected_num_notifications
$IP link del name dummy2
log_test "IPv4 route replacement"
}
ipv6_route_addition_test()
{
RET=0
local ip="ipv6"
local route=2001:db8:1::/64
# Make sure a single notification will be emitted for the programmed
# route.
local notify=0
local expected_num_notifications=1
route_addition_check $ip $notify $route $expected_num_notifications
# Make sure two notifications will be emitted for the programmed route.
notify=1
expected_num_notifications=2
route_addition_check $ip $notify $route $expected_num_notifications
log_test "IPv6 route addition"
}
ipv6_route_deletion_test()
{
RET=0
local ip="ipv6"
local route=2001:db8:1::/64
local expected_num_notifications=1
# Make sure a single notification will be emitted for the deleted route,
# regardless of fib_notify_on_flag_change value.
local notify=0
route_deletion_check $ip $notify $route $expected_num_notifications
notify=1
route_deletion_check $ip $notify $route $expected_num_notifications
log_test "IPv6 route deletion"
}
ipv6_route_replacement_test()
{
RET=0
local ip="ipv6"
local route=2001:db8:1::/64
$IP link add name dummy2 type dummy
$IP link set dev dummy2 up
# Make sure a single notification will be emitted for the new route.
local notify=0
local expected_num_notifications=1
route_replacement_check $ip $notify $route $expected_num_notifications
# Make sure two notifications will be emitted for the new route.
notify=1
expected_num_notifications=2
route_replacement_check $ip $notify $route $expected_num_notifications
$IP link del name dummy2
log_test "IPv6 route replacement"
}
setup_prepare()
{
modprobe netdevsim &> /dev/null
echo "$DEV_ADDR 1" > ${NETDEVSIM_PATH}/new_device
while [ ! -d $SYSFS_NET_DIR ] ; do :; done
ip netns add testns1
if [ $? -ne 0 ]; then
echo "Failed to add netns \"testns1\""
exit 1
fi
devlink dev reload $DEVLINK_DEV netns testns1
if [ $? -ne 0 ]; then
echo "Failed to reload into netns \"testns1\""
exit 1
fi
IP="ip -n testns1"
$IP link add name dummy1 type dummy
$IP link set dev dummy1 up
}
cleanup()
{
pre_cleanup
$IP link del name dummy1
ip netns del testns1
echo "$DEV_ADDR" > ${NETDEVSIM_PATH}/del_device
modprobe -r netdevsim &> /dev/null
}
trap cleanup EXIT
setup_prepare
tests_run
exit $EXIT_STATUS
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册