Skip to content

Commit bc7cbb1

Browse files
pmachatadavem330
authored andcommitted
selftests: forwarding: Add devlink_lib.sh
This helper library contains wrappers to devlink functionality agnostic to the underlying device. Signed-off-by: Yuval Mintz <yuvalm@mellanox.com> [petrm@mellanox.com: Split this out from another patch.] Signed-off-by: Petr Machata <petrm@mellanox.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 68d9cea commit bc7cbb1

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: GPL-2.0
3+
4+
##############################################################################
5+
# Source library
6+
7+
relative_path="${BASH_SOURCE%/*}"
8+
if [[ "$relative_path" == "${BASH_SOURCE}" ]]; then
9+
relative_path="."
10+
fi
11+
12+
source "$relative_path/lib.sh"
13+
14+
##############################################################################
15+
# Defines
16+
17+
DEVLINK_DEV=$(devlink port show | grep "${NETIFS[p1]}" | \
18+
grep -v "${NETIFS[p1]}[0-9]" | cut -d" " -f1 | \
19+
rev | cut -d"/" -f2- | rev)
20+
if [ -z "$DEVLINK_DEV" ]; then
21+
echo "SKIP: ${NETIFS[p1]} has no devlink device registered for it"
22+
exit 1
23+
fi
24+
if [[ "$(echo $DEVLINK_DEV | grep -c pci)" -eq 0 ]]; then
25+
echo "SKIP: devlink device's bus is not PCI"
26+
exit 1
27+
fi
28+
29+
DEVLINK_VIDDID=$(lspci -s $(echo $DEVLINK_DEV | cut -d"/" -f2) \
30+
-n | cut -d" " -f3)
31+
32+
##############################################################################
33+
# Sanity checks
34+
35+
devlink -j resource show "$DEVLINK_DEV" &> /dev/null
36+
if [ $? -ne 0 ]; then
37+
echo "SKIP: iproute2 too old, missing devlink resource support"
38+
exit 1
39+
fi
40+
41+
##############################################################################
42+
# Devlink helpers
43+
44+
devlink_resource_names_to_path()
45+
{
46+
local resource
47+
local path=""
48+
49+
for resource in "${@}"; do
50+
if [ "$path" == "" ]; then
51+
path="$resource"
52+
else
53+
path="${path}/$resource"
54+
fi
55+
done
56+
57+
echo "$path"
58+
}
59+
60+
devlink_resource_get()
61+
{
62+
local name=$1
63+
local resource_name=.[][\"$DEVLINK_DEV\"]
64+
65+
resource_name="$resource_name | .[] | select (.name == \"$name\")"
66+
67+
shift
68+
for resource in "${@}"; do
69+
resource_name="${resource_name} | .[\"resources\"][] | \
70+
select (.name == \"$resource\")"
71+
done
72+
73+
devlink -j resource show "$DEVLINK_DEV" | jq "$resource_name"
74+
}
75+
76+
devlink_resource_size_get()
77+
{
78+
local size=$(devlink_resource_get "$@" | jq '.["size_new"]')
79+
80+
if [ "$size" == "null" ]; then
81+
devlink_resource_get "$@" | jq '.["size"]'
82+
else
83+
echo "$size"
84+
fi
85+
}
86+
87+
devlink_resource_size_set()
88+
{
89+
local new_size=$1
90+
local path
91+
92+
shift
93+
path=$(devlink_resource_names_to_path "$@")
94+
devlink resource set "$DEVLINK_DEV" path "$path" size "$new_size"
95+
check_err $? "Failed setting path $path to size $size"
96+
}
97+
98+
devlink_reload()
99+
{
100+
local still_pending
101+
102+
devlink dev reload "$DEVLINK_DEV" &> /dev/null
103+
check_err $? "Failed reload"
104+
105+
still_pending=$(devlink resource show "$DEVLINK_DEV" | \
106+
grep -c "size_new")
107+
check_err $still_pending "Failed reload - There are still unset sizes"
108+
}

0 commit comments

Comments
 (0)