#!/bin/sh
#
# /etc/init.d/rcS
#

echo -n "mounting swap..."
/sbin/swapon /dev/hdc
echo "done."

echo -n "setting up network..."
/sbin/ifconfig lo 127.0.0.1
/sbin/ifconfig eth0 192.168.0.10 txqueuelen 1000

echo "1" > /proc/sys/net/ipv4/tcp_tw_recycle
echo "1" > /proc/sys/net/ipv4/tcp_tw_reuse
echo "1" > /proc/sys/net/ipv4/tcp_window_scaling
echo "0" > /proc/sys/net/ipv4/tcp_timestamps
echo "0" > /proc/sys/net/ipv4/tcp_sack
echo "15" > /proc/sys/net/ipv4/tcp_fin_timeout
echo "16384" > /proc/sys/net/ipv4/tcp_max_syn_backlog
echo "262144" > /proc/sys/net/ipv4/ip_conntrack_max
echo "1024 65535" > /proc/sys/net/ipv4/ip_local_port_range
echo "10000000 10000000 10000000" > /proc/sys/net/ipv4/tcp_rmem
echo "10000000 10000000 10000000" > /proc/sys/net/ipv4/tcp_wmem
echo "10000000 10000000 10000000" > /proc/sys/net/ipv4/tcp_mem
#echo "262144" > /proc/sys/net/ipv4/ip_conntrack_max
echo "524287" > /proc/sys/net/core/rmem_max
echo "524287" > /proc/sys/net/core/wmem_max
echo "524287" > /proc/sys/net/core/optmem_max
echo "300000" > /proc/sys/net/core/netdev_max_backlog
echo "131072" > /proc/sys/fs/file-max
echo "1" > /proc/sys/vm/dirty_writeback_centisecs
echo "done."

cat > /etc/initiatorname.iscsi <<EOF
InitiatorName=iqn.1987-05.10.0.168.192
EOF

cat > /etc/iscsid.conf <<EOF
node.active_cnx = 1
node.startup = manual
#node.session.auth.username = dima
#node.session.auth.password = aloha
node.session.timeo.replacement_timeout = 0
node.session.err_timeo.abort_timeout = 10
node.session.err_timeo.reset_timeout = 30
node.session.iscsi.InitialR2T = No
node.session.iscsi.ImmediateData = Yes
node.session.iscsi.FirstBurstLength = 262144
node.session.iscsi.MaxBurstLength = 16776192
node.session.iscsi.DefaultTime2Wait = 0
node.session.iscsi.DefaultTime2Retain = 0
node.session.iscsi.MaxConnections = 0
node.cnx[0].iscsi.HeaderDigest = None
node.cnx[0].iscsi.DataDigest = None
node.cnx[0].iscsi.MaxRecvDataSegmentLength = 65536
#discovery.sendtargets.auth.authmethod = CHAP
#discovery.sendtargets.auth.username = dima
#discovery.sendtargets.auth.password = aloha
EOF

#mount -t sysfs none /sys

echo "" > /var/log/iscsi.log
chmod 0600 /var/log/iscsi.log
chmod 0666 -R /var/db/iscsi

# Required for udev to activate/deactivate devices.
echo "/sbin/hotplug" > /proc/sys/kernel/hotplug	

/sbin/insmod /modules/scsi_transport_iscsi.ko
/sbin/insmod /modules/iscsi_tcp.ko

echo "Starting iscsid..."
/iscsi/iscsid -f &

sleep 1

echo "Waiting for server..."
netcat -c -l -p 8000 

echo "Attaching target..."
/iscsi/iscsiadm -m discovery -t st -p 192.168.0.1
/iscsi/iscsiadm -m node -r a0c049 --login
echo "done."

sleep 5

#PARAMS="-n 8 -s 8 -r .666 -c 50"         # lisa default
#PARAMS="-n 8 -s 5120 -r .666 -c 20"      # very unstable
#PARAMS="-n 8 -s 8 -r 0 -c 20"            # very unstable
#PARAMS="-n 8 -s 8 -r 1 -c 20"            # stable 960 Mbps
#PARAMS="-n 8 -s 1024 -r 1 -c 20"         # very unstable
#PARAMS="-n 16 -s 32 -r 1 -c 50"          # stable ~1.5 Gbps
#PARAMS="-n 50 -s 32 -r 1 -c 50"          # stable ~1.5 Gbps
PARAMS="-n 50 -s 8 -r .666 -c 50"
COMMAND="/benchmarks/aio-bench $PARAMS -i 1000000 -C 50000 /dev/sda"

echo "Starting aio benchmark..."
echo $COMMAND
eval $COMMAND

echo "starting bash shell..."
/bin/bash

echo -n "halting machine"
m5 exit
