Skip to content

Commit

Permalink
selftests: net: lib: Add several autodefer helpers
Browse files Browse the repository at this point in the history
Add ip_link_set_addr(), ip_link_set_up(), ip_addr_add() and ip_route_add()
to the suite of helpers that automatically schedule a corresponding
cleanup.

When setting a new MAC, one needs to remember the old address first. Move
mac_get() from forwarding/ to that end.

Signed-off-by: Petr Machata <petrm@nvidia.com>
Reviewed-by: Ido Schimmel <idosch@nvidia.com>
Reviewed-by: Nikolay Aleksandrov <razor@blackwall.org>
Link: https://patch.msgid.link/add6bcbe30828fd01363266df20c338cf13aaf25.1733412063.git.petrm@nvidia.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
  • Loading branch information
pmachata authored and kuba-moo committed Dec 9, 2024
1 parent 8653eb2 commit d76ccb2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
7 changes: 0 additions & 7 deletions tools/testing/selftests/net/forwarding/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -932,13 +932,6 @@ packets_rate()
echo $(((t1 - t0) / interval))
}

mac_get()
{
local if_name=$1

ip -j link show dev $if_name | jq -r '.[]["address"]'
}

ether_addr_to_u64()
{
local addr="$1"
Expand Down
39 changes: 39 additions & 0 deletions tools/testing/selftests/net/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,13 @@ xfail_on_veth()
fi
}

mac_get()
{
local if_name=$1

ip -j link show dev $if_name | jq -r '.[]["address"]'
}

kill_process()
{
local pid=$1; shift
Expand All @@ -459,3 +466,35 @@ ip_link_set_master()
ip link set dev "$member" master "$master"
defer ip link set dev "$member" nomaster
}

ip_link_set_addr()
{
local name=$1; shift
local addr=$1; shift

local old_addr=$(mac_get "$name")
ip link set dev "$name" address "$addr"
defer ip link set dev "$name" address "$old_addr"
}

ip_link_set_up()
{
local name=$1; shift

ip link set dev "$name" up
defer ip link set dev "$name" down
}

ip_addr_add()
{
local name=$1; shift

ip addr add dev "$name" "$@"
defer ip addr del dev "$name" "$@"
}

ip_route_add()
{
ip route add "$@"
defer ip route del "$@"
}

0 comments on commit d76ccb2

Please sign in to comment.