1#!/bin/bash 2# SPDX-License-Identifier: GPL-2.0 3 4############################################################################## 5# Defines 6 7DEVLINK_DEV=$(devlink port show "${NETIFS[p1]}" -j \ 8 | jq -r '.port | keys[]' | cut -d/ -f-2) 9if [ -z "$DEVLINK_DEV" ]; then 10 echo "SKIP: ${NETIFS[p1]} has no devlink device registered for it" 11 exit 1 12fi 13if [[ "$(echo $DEVLINK_DEV | grep -c pci)" -eq 0 ]]; then 14 echo "SKIP: devlink device's bus is not PCI" 15 exit 1 16fi 17 18DEVLINK_VIDDID=$(lspci -s $(echo $DEVLINK_DEV | cut -d"/" -f2) \ 19 -n | cut -d" " -f3) 20 21############################################################################## 22# Sanity checks 23 24devlink help 2>&1 | grep resource &> /dev/null 25if [ $? -ne 0 ]; then 26 echo "SKIP: iproute2 too old, missing devlink resource support" 27 exit 1 28fi 29 30############################################################################## 31# Devlink helpers 32 33devlink_resource_names_to_path() 34{ 35 local resource 36 local path="" 37 38 for resource in "${@}"; do 39 if [ "$path" == "" ]; then 40 path="$resource" 41 else 42 path="${path}/$resource" 43 fi 44 done 45 46 echo "$path" 47} 48 49devlink_resource_get() 50{ 51 local name=$1 52 local resource_name=.[][\"$DEVLINK_DEV\"] 53 54 resource_name="$resource_name | .[] | select (.name == \"$name\")" 55 56 shift 57 for resource in "${@}"; do 58 resource_name="${resource_name} | .[\"resources\"][] | \ 59 select (.name == \"$resource\")" 60 done 61 62 devlink -j resource show "$DEVLINK_DEV" | jq "$resource_name" 63} 64 65devlink_resource_size_get() 66{ 67 local size=$(devlink_resource_get "$@" | jq '.["size_new"]') 68 69 if [ "$size" == "null" ]; then 70 devlink_resource_get "$@" | jq '.["size"]' 71 else 72 echo "$size" 73 fi 74} 75 76devlink_resource_size_set() 77{ 78 local new_size=$1 79 local path 80 81 shift 82 path=$(devlink_resource_names_to_path "$@") 83 devlink resource set "$DEVLINK_DEV" path "$path" size "$new_size" 84 check_err $? "Failed setting path $path to size $size" 85} 86 87devlink_reload() 88{ 89 local still_pending 90 91 devlink dev reload "$DEVLINK_DEV" &> /dev/null 92 check_err $? "Failed reload" 93 94 still_pending=$(devlink resource show "$DEVLINK_DEV" | \ 95 grep -c "size_new") 96 check_err $still_pending "Failed reload - There are still unset sizes" 97} 98 99declare -A DEVLINK_ORIG 100 101devlink_port_pool_threshold() 102{ 103 local port=$1; shift 104 local pool=$1; shift 105 106 devlink sb port pool show $port pool $pool -j \ 107 | jq '.port_pool."'"$port"'"[].threshold' 108} 109 110devlink_port_pool_th_set() 111{ 112 local port=$1; shift 113 local pool=$1; shift 114 local th=$1; shift 115 local key="port_pool($port,$pool).threshold" 116 117 DEVLINK_ORIG[$key]=$(devlink_port_pool_threshold $port $pool) 118 devlink sb port pool set $port pool $pool th $th 119} 120 121devlink_port_pool_th_restore() 122{ 123 local port=$1; shift 124 local pool=$1; shift 125 local key="port_pool($port,$pool).threshold" 126 127 devlink sb port pool set $port pool $pool th ${DEVLINK_ORIG[$key]} 128} 129 130devlink_pool_size_thtype() 131{ 132 local pool=$1; shift 133 134 devlink sb pool show "$DEVLINK_DEV" pool $pool -j \ 135 | jq -r '.pool[][] | (.size, .thtype)' 136} 137 138devlink_pool_size_thtype_set() 139{ 140 local pool=$1; shift 141 local thtype=$1; shift 142 local size=$1; shift 143 local key="pool($pool).size_thtype" 144 145 DEVLINK_ORIG[$key]=$(devlink_pool_size_thtype $pool) 146 devlink sb pool set "$DEVLINK_DEV" pool $pool size $size thtype $thtype 147} 148 149devlink_pool_size_thtype_restore() 150{ 151 local pool=$1; shift 152 local key="pool($pool).size_thtype" 153 local -a orig=(${DEVLINK_ORIG[$key]}) 154 155 devlink sb pool set "$DEVLINK_DEV" pool $pool \ 156 size ${orig[0]} thtype ${orig[1]} 157} 158 159devlink_tc_bind_pool_th() 160{ 161 local port=$1; shift 162 local tc=$1; shift 163 local dir=$1; shift 164 165 devlink sb tc bind show $port tc $tc type $dir -j \ 166 | jq -r '.tc_bind[][] | (.pool, .threshold)' 167} 168 169devlink_tc_bind_pool_th_set() 170{ 171 local port=$1; shift 172 local tc=$1; shift 173 local dir=$1; shift 174 local pool=$1; shift 175 local th=$1; shift 176 local key="tc_bind($port,$dir,$tc).pool_th" 177 178 DEVLINK_ORIG[$key]=$(devlink_tc_bind_pool_th $port $tc $dir) 179 devlink sb tc bind set $port tc $tc type $dir pool $pool th $th 180} 181 182devlink_tc_bind_pool_th_restore() 183{ 184 local port=$1; shift 185 local tc=$1; shift 186 local dir=$1; shift 187 local key="tc_bind($port,$dir,$tc).pool_th" 188 local -a orig=(${DEVLINK_ORIG[$key]}) 189 190 devlink sb tc bind set $port tc $tc type $dir \ 191 pool ${orig[0]} th ${orig[1]} 192} 193