|
#!/bin/bash |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
readonly HABANA_DRIVER_NAME="habanalabs" |
|
readonly HABANA_PCI_ID="0x1da3" |
|
|
|
usage() |
|
{ |
|
echo -e "\nusage: $(basename $1) [options]\n" |
|
|
|
echo -e "options:\n" |
|
echo -e " --up toggle up all interfaces" |
|
echo -e " --down toggle down all interfaces" |
|
echo -e " --status print status of all interfaces" |
|
echo -e " --set-pfc set PFC (enabled=0,1,2,3)" |
|
echo -e " --unset-pfc unset PFC (enabled=none)" |
|
echo -e " --check-pfc dump PFC configuration" |
|
echo -e " --no-progbar do not show progress bar" |
|
echo -e " --l3 enable support for L3 configuration" |
|
echo -e " -v, --verbose print more logs" |
|
echo -e " -h, --help print this help" |
|
} |
|
|
|
habana_net_list="" |
|
device_num=0 |
|
verbose=false |
|
print_module_id=false |
|
up=0 |
|
down=0 |
|
status=0 |
|
set_pfc=0 |
|
unset_pfc=0 |
|
check_pfc=0 |
|
l3=0 |
|
is_prog_bar=true |
|
op="" |
|
my_sudo="" |
|
if [ $EUID -ne 0 ]; then |
|
my_sudo="sudo" |
|
fi |
|
|
|
show_prog_bar () |
|
{ |
|
local progress |
|
local done |
|
local left |
|
|
|
let progress=(${1}*100/${2}*100)/100 |
|
let done=(${progress}*4)/10 |
|
let left=40-$done |
|
|
|
done=$(printf "%${done}s") |
|
left=$(printf "%${left}s") |
|
|
|
printf "\r$3: [${done// /\#}${left// /-}] ${progress}%%" |
|
} |
|
|
|
toggle_links() |
|
{ |
|
local i |
|
local sum |
|
|
|
sum=$(wc -w <<< "$habana_net_list") |
|
if [ $is_prog_bar = true ]; then |
|
show_prog_bar 0 ${sum} $1 |
|
fi |
|
|
|
i=0 |
|
|
|
for net_if in $habana_net_list; do |
|
$my_sudo ip link set $net_if $1 |
|
if [ $? -ne 0 ]; then |
|
echo "Failed to toggle I/F '$net_if'" |
|
exit 1 |
|
fi |
|
let i++ |
|
if [ $is_prog_bar = true ]; then |
|
show_prog_bar ${i} ${sum} $1 |
|
fi |
|
done |
|
|
|
echo "" |
|
} |
|
|
|
build_net_ifs_global_list() |
|
{ |
|
local net_if |
|
local if_info |
|
local driver_name |
|
|
|
for net_if in /sys/class/net/*/ ; do |
|
net_if=$(basename $net_if) |
|
|
|
|
|
if [ $net_if == "lo" ] || [ `echo $net_if | cut -c1-4` == "veth" ]; then |
|
continue |
|
fi |
|
|
|
|
|
if [ -d /sys/class/net/$net_if/device/ ] && [ -f /sys/class/net/$net_if/device/vendor ]; then |
|
if [ $(cat /sys/class/net/$net_if/device/vendor) != $HABANA_PCI_ID ]; then |
|
continue |
|
fi |
|
else |
|
|
|
net_if=`echo "$net_if" | cut -d'@' -f1` |
|
|
|
|
|
if_info=`$my_sudo ethtool -i $net_if` |
|
if [ $? -ne 0 ]; then |
|
echo "Failed to acquire information for the network interface '$net_if'" |
|
continue |
|
fi |
|
|
|
driver_name=`echo "$if_info" | grep 'driver' | awk '{print $2}'` |
|
if [[ $driver_name != $HABANA_DRIVER_NAME* ]]; then |
|
continue |
|
fi |
|
fi |
|
|
|
habana_net_list="$habana_net_list $net_if" |
|
done |
|
|
|
if [ -z "$habana_net_list" ]; then |
|
echo "Warning: no $HABANA_DRIVER_NAME network interfaces were detected" |
|
exit 1 |
|
fi |
|
} |
|
|
|
check_flags () |
|
{ |
|
local sum |
|
|
|
if [ $status -gt 1 ] || [ $up -gt 1 ] || [ $down -gt 1 ] || [ $set_pfc -gt 1 ] || [ $unset_pfc -gt 1 ] || [ $check_pfc -gt 1 ]; then |
|
echo "each flag should be used once" |
|
usage $0 |
|
exit 1 |
|
fi |
|
|
|
let "sum=$up + $down + $set_pfc + $unset_pfc + $check_pfc" |
|
if [ $status -ne 0 ] && [ $sum -ne 0 ]; then |
|
echo "status flag can't be combined with other flags" |
|
usage $0 |
|
exit 1 |
|
fi |
|
|
|
let "sum=$up + $down" |
|
if [ $sum -gt 1 ]; then |
|
echo "up and down flags can't be combined together" |
|
usage $0 |
|
exit 1 |
|
fi |
|
|
|
let "sum=$set_pfc + $unset_pfc + $check_pfc" |
|
if [ $down -ne 0 ] && [ $sum -ne 0 ]; then |
|
echo "down flag can't be combined with other flags" |
|
usage $0 |
|
exit 1 |
|
fi |
|
|
|
if [ $up -ne 0 ] && [ $sum -ne 0 ]; then |
|
echo "up flag can't be combined with other flags" |
|
usage $0 |
|
exit 1 |
|
fi |
|
|
|
let "sum=$set_pfc + $unset_pfc + $check_pfc" |
|
if [ $sum -gt 1 ]; then |
|
echo "PFC flags can't be combined together" |
|
usage $0 |
|
exit 1 |
|
fi |
|
} |
|
|
|
while [ -n "$1" ]; |
|
do |
|
case $1 in |
|
-h | --help ) |
|
usage $0 |
|
exit 0 |
|
;; |
|
--up ) |
|
let up++ |
|
;; |
|
--down ) |
|
let down++ |
|
;; |
|
--status ) |
|
let status++ |
|
;; |
|
--set-pfc ) |
|
let set_pfc++ |
|
;; |
|
--unset-pfc ) |
|
let unset_pfc++ |
|
;; |
|
--check-pfc ) |
|
let check_pfc++ |
|
;; |
|
--no-progbar ) |
|
is_prog_bar=false |
|
;; |
|
--l3 ) |
|
let l3++ |
|
;; |
|
-v | --verbose ) |
|
verbose=true |
|
;; |
|
*) |
|
echo "bad argument '$1'" |
|
usage $0 |
|
exit 1 |
|
;; |
|
esac |
|
shift |
|
done |
|
|
|
check_flags |
|
|
|
if [ $status -eq 1 ]; then |
|
if [ ! -d /sys/class/habanalabs/ ]; then |
|
echo "no available habanalabs directory" |
|
exit 1 |
|
fi |
|
|
|
device_num=$(ls /sys/class/habanalabs/ | grep -E "hl[0-9]+" | wc -l) |
|
if [ $device_num -eq 0 ]; then |
|
echo "no available habanalabs devices were found" |
|
exit 1 |
|
fi |
|
|
|
if [ -v HLTHUNK_RELEASE_BUILD ] && [ -f $HLTHUNK_RELEASE_BUILD/bin/control_device ]; then |
|
print_module_id=true |
|
fi |
|
|
|
for (( i=0; i<$device_num; i++ )); do |
|
dev_name="hl$i" |
|
if [ ! -d /sys/class/habanalabs/$dev_name/ ]; then |
|
echo "$dev_name doesn't exist" |
|
continue |
|
fi |
|
|
|
if [ "$(cat /sys/class/habanalabs/$dev_name/status)" != "Operational" ]; then |
|
echo "$dev_name is not operational" |
|
continue |
|
fi |
|
|
|
pci_addr=$(cat /sys/class/habanalabs/$dev_name/pci_addr) |
|
if [ $print_module_id = true ]; then |
|
module_id=$($HLTHUNK_RELEASE_BUILD/bin/control_device -s test_print_hw_ip_info \ |
|
-p $pci_addr -d 2> /dev/null | grep "Module ID" | cut -d ":" -f 2 | cut -c 2) |
|
fi |
|
dev_ifs="" |
|
dev_ifs_up="" |
|
dev_ifs_down="" |
|
dev_num_up=0 |
|
dev_num_down=0 |
|
|
|
if [ -d /sys/bus/pci/devices/$pci_addr/net/ ]; then |
|
for dev_if in /sys/bus/pci/devices/$pci_addr/net/*/; do |
|
dev_ifs="$dev_ifs $dev_if" |
|
done |
|
else |
|
for net_if in /sys/class/net/*/; do |
|
net_if=$(basename $net_if) |
|
|
|
|
|
if [ $net_if == "lo" ] || [ `echo $net_if | cut -c1-4` == "veth" ]; then |
|
continue |
|
fi |
|
|
|
|
|
net_if=`echo "$net_if" | cut -d'@' -f1` |
|
|
|
if_info=`$my_sudo ethtool -i $net_if` |
|
if [ $? -ne 0 ]; then |
|
echo "Failed to acquire information for the network interface '$net_if'" |
|
exit 1 |
|
fi |
|
|
|
|
|
bus_info=`echo "$if_info" | grep 'bus-info' | awk '{print $2}'` |
|
if [ $bus_info != $pci_addr ]; then |
|
continue |
|
fi |
|
|
|
dev_ifs="$dev_ifs $net_if" |
|
done |
|
fi |
|
|
|
for dev_if in $dev_ifs; do |
|
dev_if=$(basename $dev_if) |
|
|
|
if [ ! -f /sys/class/net/$dev_if/dev_port ] || |
|
[ ! -f /sys/class/net/$dev_if/operstate ]; then |
|
echo "can't get dev_port/opersate of $dev_if" |
|
exit 1 |
|
fi |
|
|
|
dev_port=$(cat /sys/class/net/$dev_if/dev_port) |
|
|
|
if [ $(cat /sys/class/net/$dev_if/operstate) == "up" ]; then |
|
let dev_num_up++ |
|
if [ -z "$dev_ifs_up" ]; then |
|
dev_ifs_up="$dev_port" |
|
else |
|
dev_ifs_up="$dev_ifs_up $dev_port" |
|
fi |
|
else |
|
let dev_num_down++ |
|
if [ -z "$dev_ifs_down" ]; then |
|
dev_ifs_down="$dev_port" |
|
else |
|
dev_ifs_down="$dev_ifs_down $dev_port" |
|
fi |
|
fi |
|
done |
|
|
|
if [ $print_module_id = true ]; then |
|
echo "$dev_name (module ID $module_id)" |
|
else |
|
echo "$dev_name" |
|
fi |
|
|
|
if [ -z "$dev_ifs_up" ] && [ -z "$dev_ifs_down" ]; then |
|
echo "no interfaces were detected" |
|
continue |
|
fi |
|
|
|
|
|
dev_ifs_up=$(echo $dev_ifs_up | xargs -n1 | sort -n | xargs) |
|
dev_ifs_down=$(echo $dev_ifs_down | xargs -n1 | sort -n | xargs) |
|
|
|
dev_ifs_up=${dev_ifs_up//" "/", "} |
|
dev_ifs_down=${dev_ifs_down//" "/", "} |
|
|
|
if [ $dev_num_up -gt 0 ]; then |
|
echo "$dev_num_up ports up ($dev_ifs_up)" |
|
fi |
|
if [ $dev_num_down -gt 0 ]; then |
|
echo "$dev_num_down ports down ($dev_ifs_down)" |
|
fi |
|
done |
|
else |
|
build_net_ifs_global_list |
|
fi |
|
|
|
if [ $up -eq 1 ] || [ $down -eq 1 ]; then |
|
if [ $up -eq 1 ]; then |
|
op="up" |
|
else |
|
op="down" |
|
fi |
|
|
|
toggle_links $op |
|
|
|
echo -e "$(wc -w <<< "$habana_net_list") $HABANA_DRIVER_NAME network interfaces were toggled $op" |
|
fi |
|
|
|
if [ $set_pfc -eq 1 ] || [ $unset_pfc -eq 1 ] || [ $check_pfc -eq 1 ]; then |
|
type lldptool &> /dev/null |
|
if [ $? -ne 0 ]; then |
|
echo "lldptool is not installed" |
|
exit 1 |
|
fi |
|
|
|
if [ $set_pfc -eq 1 ]; then |
|
op="set_pfc" |
|
elif [ $unset_pfc -eq 1 ]; then |
|
op="unset_pfc" |
|
else |
|
op="check_pfc" |
|
fi |
|
|
|
for net_if in $habana_net_list; do |
|
if [ $check_pfc -eq 1 ] || [ $verbose = true ]; then |
|
echo -e "$op '$net_if'" |
|
fi |
|
if [ $set_pfc -eq 1 ]; then |
|
$my_sudo lldptool -T -i $net_if -V PFC enabled=0,1,2,3 > /dev/null |
|
elif [ $unset_pfc -eq 1 ]; then |
|
$my_sudo lldptool -T -i $net_if -V PFC enabled=none > /dev/null |
|
else |
|
$my_sudo lldptool -t -i $net_if -V PFC -c enabled |
|
fi |
|
|
|
if [ $? -ne 0 ]; then |
|
echo "Error, $op '$net_if'" |
|
exit 1 |
|
fi |
|
done |
|
|
|
if [ $verbose = true ]; then |
|
echo -e "" |
|
fi |
|
fi |
|
|
|
if [ $l3 -eq 1 ]; then |
|
$my_sudo /software/devops/l3/l3-routes |
|
fi |
|
|
|
|
|
|
|
exit 0 |
|
|