xref: /linux/tools/testing/selftests/net/forwarding/devlink_lib.sh (revision bd628c1bed7902ec1f24ba0fe70758949146abbe)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4##############################################################################
5# Source library
6
7relative_path="${BASH_SOURCE%/*}"
8if [[ "$relative_path" == "${BASH_SOURCE}" ]]; then
9	relative_path="."
10fi
11
12source "$relative_path/lib.sh"
13
14##############################################################################
15# Defines
16
17DEVLINK_DEV=$(devlink port show | grep "${NETIFS[p1]}" | \
18	      grep -v "${NETIFS[p1]}[0-9]" | cut -d" " -f1 | \
19	      rev | cut -d"/" -f2- | rev)
20if [ -z "$DEVLINK_DEV" ]; then
21	echo "SKIP: ${NETIFS[p1]} has no devlink device registered for it"
22	exit 1
23fi
24if [[ "$(echo $DEVLINK_DEV | grep -c pci)" -eq 0 ]]; then
25	echo "SKIP: devlink device's bus is not PCI"
26	exit 1
27fi
28
29DEVLINK_VIDDID=$(lspci -s $(echo $DEVLINK_DEV | cut -d"/" -f2) \
30		 -n | cut -d" " -f3)
31
32##############################################################################
33# Sanity checks
34
35devlink -j resource show "$DEVLINK_DEV" &> /dev/null
36if [ $? -ne 0 ]; then
37	echo "SKIP: iproute2 too old, missing devlink resource support"
38	exit 1
39fi
40
41##############################################################################
42# Devlink helpers
43
44devlink_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
60devlink_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
76devlink_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
87devlink_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
98devlink_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}
109