# hpc-testing
# Copyright (C) 2018 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/>.

set_ipoib_mode()
{
	local host=$1
	local port=$2
	local mode=$3

	tp $host "echo $mode > /sys/class/net/$port/mode"
	R_MODE=$(tpq $host "cat /sys/class/net/$port/mode")
	if [ "$R_MODE" != "$mode" ]; then
		fatal_error "Failed to set IPoIB mode"
	fi
}
set_ipoib_up()
{
	local host=$1
	local port=$2
	local ipaddr=$3 #ip/netmask

	tp $host "ip link set dev $port up &&
			  ip addr add $ipaddr dev $port"
}
set_ipoib_down()
{
	local host=$1
	local port=$2

	tp $host "ip link set dev $port down &&
	   		  ip addr flush $port"
}

test_ping()
{
	local host=$1
	local remote_addr=$2
	local pkt_size=$3

	tp $host "ip neigh flush $remote_addr && ping -i 0.01 -t 3 -c 100 -s $pkt_size $remote_addr"
}

test_sftp()
{
	local host=$1
	local remote_addr=$2

	# Generate a random file
	tp $host "rm -f sftp.orig && dd if=/dev/urandom bs=1M count=64 of=sftp.orig"
	# Copy back and forth
	tp $host "scp sftp.orig $remote_addr:sftp.orig && scp $remote_addr:sftp.orig sftp.copy"
	# Check file
	tp $host "diff -q sftp.orig sftp.copy"
}

is_connected_supported()
{
	local host=$1
	local port=$2

	mac_first_byte=$(tpq $host 'ip link show '$port' | grep link/infiniband | 
	 sed -e "s/.*link\/infiniband \([0-9a-f]\)[0-9a-f]:.*/\1/"')

	if [ $mac_first_byte -ge 8 ]; then
		return 0
	else
		return 1
	fi
}
