# 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/>.

test_srp()
{
	local srp_server=$1
	local server_guid=$(echo $2| sed -e 's/0x//')
	local server_sysguid=$(echo $3 | sed -e 's/0x//')
	local server_wwn="fe80000000000000${server_guid}"
	local server_sys_wwn="fe80000000000000${server_sysguid}"
	local server_board=$4
	local server_port=$5

	local client=$6
	local client_guid=$(echo $7 |  sed -e 's/0x//')
	local client_wwn="0000000000000000${client_guid}"
	local client_board=$8
	local client_port=$9

	# Cleanup old stuff
	tp $client 'rmmod ib_srp || true'
	tp $srp_server "umount /tmp/hpc-test.mount || true;
	   			   	targetcli /backstores/fileio/ delete name=hpc-test || true;
                    targetcli /srpt delete ib.$server_wwn || true;
					rm -Rf /tmp/hpc-test.io /tmp/hpc-test.mount &&
					modprobe ib_srpt &&
					targetcli /backstores/fileio create name=hpc-test file_or_dev=/tmp/hpc-test.io size=256M &&
					targetcli /srpt create 0x$server_wwn &&
					targetcli /srpt/ib.$server_wwn/luns create /backstores/fileio/hpc-test &&
					targetcli /srpt/ib.$server_wwn/acls create 0x$client_wwn &&
					mkfs -t ext3 /tmp/hpc-test.io &&
					mkdir /tmp/hpc-test.mount && 
					mount -o loop /tmp/hpc-test.io /tmp/hpc-test.mount &&
					dd if=/dev/urandom bs=1M count=64 of=/tmp/hpc-test.mount/input &&
					umount /tmp/hpc-test.mount"
	tp $client 'umount /tmp/srp-hpc-test || true;
	   		    rm -Rf /tmp/srp-hpc-test || true;
				modprobe ib_srp &&
				mkdir /tmp/srp-hpc-test &&
				(ibsrpdm -c | grep "dgid='$server_wwn'";
				 ibsrpdm -c | grep "dgid='$server_sys_wwn'" | sed -e "s/'$server_sysguid'/'$server_guid'/g") >
						 /sys/class/infiniband_srp/srp-'$client_board'-'$client_port'/add_target &&
				sleep 1 &&
				block_device=$(ls /sys/class/infiniband/'$client_board'/device/host*/target*/*:*/block) &&
				mount /dev/$block_device /tmp/srp-hpc-test &&
				cp -R /tmp/srp-hpc-test/input /tmp/srp-hpc-test/output &&
				diff -q /tmp/srp-hpc-test/input /tmp/srp-hpc-test/output&&
				umount /tmp/srp-hpc-test &&
				rmmod ib_srp
	 '
	tp $srp_server "mount -o loop /tmp/hpc-test.io /tmp/hpc-test.mount &&
					diff -q /tmp/hpc-test.mount/input /tmp/hpc-test.mount/output &&
					umount /tmp/hpc-test.mount"
}
