selftests: forwarding: devlink_lib: Add devlink-trap helpers

Add helpers to interact with devlink-trap, such as setting the action of
a trap and retrieving statistics.

Signed-off-by: Ido Schimmel <idosch@mellanox.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
Ido Schimmel 2019-08-17 16:28:23 +03:00 committed by David S. Miller
parent bc030d9c91
commit a054c8d90b
1 changed files with 163 additions and 0 deletions

View File

@ -29,6 +29,12 @@ if [ $? -ne 0 ]; then
exit 1
fi
devlink help 2>&1 | grep trap &> /dev/null
if [ $? -ne 0 ]; then
echo "SKIP: iproute2 too old, missing devlink trap support"
exit 1
fi
##############################################################################
# Devlink helpers
@ -192,3 +198,160 @@ devlink_tc_bind_pool_th_restore()
devlink sb tc bind set $port tc $tc type $dir \
pool ${orig[0]} th ${orig[1]}
}
devlink_traps_num_get()
{
devlink -j trap | jq '.[]["'$DEVLINK_DEV'"] | length'
}
devlink_traps_get()
{
devlink -j trap | jq -r '.[]["'$DEVLINK_DEV'"][].name'
}
devlink_trap_type_get()
{
local trap_name=$1; shift
devlink -j trap show $DEVLINK_DEV trap $trap_name \
| jq -r '.[][][].type'
}
devlink_trap_action_set()
{
local trap_name=$1; shift
local action=$1; shift
# Pipe output to /dev/null to avoid expected warnings.
devlink trap set $DEVLINK_DEV trap $trap_name \
action $action &> /dev/null
}
devlink_trap_action_get()
{
local trap_name=$1; shift
devlink -j trap show $DEVLINK_DEV trap $trap_name \
| jq -r '.[][][].action'
}
devlink_trap_group_get()
{
devlink -j trap show $DEVLINK_DEV trap $trap_name \
| jq -r '.[][][].group'
}
devlink_trap_metadata_test()
{
local trap_name=$1; shift
local metadata=$1; shift
devlink -jv trap show $DEVLINK_DEV trap $trap_name \
| jq -e '.[][][].metadata | contains(["'$metadata'"])' \
&> /dev/null
}
devlink_trap_rx_packets_get()
{
local trap_name=$1; shift
devlink -js trap show $DEVLINK_DEV trap $trap_name \
| jq '.[][][]["stats"]["rx"]["packets"]'
}
devlink_trap_rx_bytes_get()
{
local trap_name=$1; shift
devlink -js trap show $DEVLINK_DEV trap $trap_name \
| jq '.[][][]["stats"]["rx"]["bytes"]'
}
devlink_trap_stats_idle_test()
{
local trap_name=$1; shift
local t0_packets t0_bytes
local t1_packets t1_bytes
t0_packets=$(devlink_trap_rx_packets_get $trap_name)
t0_bytes=$(devlink_trap_rx_bytes_get $trap_name)
sleep 1
t1_packets=$(devlink_trap_rx_packets_get $trap_name)
t1_bytes=$(devlink_trap_rx_bytes_get $trap_name)
if [[ $t0_packets -eq $t1_packets && $t0_bytes -eq $t1_bytes ]]; then
return 0
else
return 1
fi
}
devlink_traps_enable_all()
{
local trap_name
for trap_name in $(devlink_traps_get); do
devlink_trap_action_set $trap_name "trap"
done
}
devlink_traps_disable_all()
{
for trap_name in $(devlink_traps_get); do
devlink_trap_action_set $trap_name "drop"
done
}
devlink_trap_groups_get()
{
devlink -j trap group | jq -r '.[]["'$DEVLINK_DEV'"][].name'
}
devlink_trap_group_action_set()
{
local group_name=$1; shift
local action=$1; shift
# Pipe output to /dev/null to avoid expected warnings.
devlink trap group set $DEVLINK_DEV group $group_name action $action \
&> /dev/null
}
devlink_trap_group_rx_packets_get()
{
local group_name=$1; shift
devlink -js trap group show $DEVLINK_DEV group $group_name \
| jq '.[][][]["stats"]["rx"]["packets"]'
}
devlink_trap_group_rx_bytes_get()
{
local group_name=$1; shift
devlink -js trap group show $DEVLINK_DEV group $group_name \
| jq '.[][][]["stats"]["rx"]["bytes"]'
}
devlink_trap_group_stats_idle_test()
{
local group_name=$1; shift
local t0_packets t0_bytes
local t1_packets t1_bytes
t0_packets=$(devlink_trap_group_rx_packets_get $group_name)
t0_bytes=$(devlink_trap_group_rx_bytes_get $group_name)
sleep 1
t1_packets=$(devlink_trap_group_rx_packets_get $group_name)
t1_bytes=$(devlink_trap_group_rx_bytes_get $group_name)
if [[ $t0_packets -eq $t1_packets && $t0_bytes -eq $t1_bytes ]]; then
return 0
else
return 1
fi
}