#! /bin/bash
# hpc-testing
# Copyright (C) 2025 SUSE LLC
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

setup_ssh()
{
    local host=$1
    local remote_ip=$2

    # Make sure we accepted the remote SSH key so MPI can work
    tp $host "touch .ssh/known_hosts &&
	   		  sed -i '/^$remote_ip /d' .ssh/known_hosts &&
			  ssh-keyscan $remote_ip >> .ssh/known_hosts"
}

nmcli_disable()
{
    local host=$1
    local port=$2
    tp $host "nmcli device set $port managed no || true"
}

get_srdma_port()
{
    local host=$1
    local host_id=$2
    local port_type=$3
    local eth

    eth=$(eval echo \$IPPORT$host_id)
    tpq $host "rdma link del ${port_type}0 2>/dev/null || true"
    tpq $host "rdma link add ${port_type}0 type ${port_type} netdev $eth"

    res=$(tpq $host "rdma link show" | grep $eth)
    if [ "$res" == "" ]; then
	fatal_error "Failed to find an active port on $host"
    fi

    ip=$(echo $res | awk '{ print $5}')
    rdma_if=$(echo $res | awk '{ print $2}' | sed -e 's/\/.*//')
    port=$(echo $res | awk '{ print $2}' | sed -e 's/.*\///')

    eval export HCA$host_id=$rdma_if
    eval export IBPORT$host_id=$port

    ip=$(tpq $host "ip addr show $eth" | ip_addr_show_to_ip)
    eval export IP$host_id=$ip

    juLogSetProperty $host.ip_if $ip
    juLogSetProperty $host.port $rdma_if

    echo "[SUCCESS] Host $host uses interface $rdma_if."
}

do_disable_unused_rdma_ports()
{
    local used_interface=$1

    cd /sys/class/infiniband || return 0
    for hca in $(ls); do
        if [ "$hca" == "$used_interface" ]; then
            continue
        fi
        if [ -e "${hca}/device" ]; then
            basename $(readlink -f "${hca}/device") > "${hca}/device/driver/unbind"
        else
            rdma link del "${hca}"
        fi
    done
    true
}

disable_unused_rdma_ports()
{
    local host=$1
    local used_interface=$2

    tp_fun $host do_disable_unused_rdma_ports $used_interface
}
