Skip to content

Commit

Permalink
Fixing parallel iptables problem
Browse files Browse the repository at this point in the history
Summary: iptables rule addition and deletion used to fail in case of
simultaneous access. So, added a lock to prevent simultaneous access

Reviewed By: @​@ldemailly

Differential Revision: D2468476
  • Loading branch information
uddipta authored and ldemailly committed Sep 22, 2015
1 parent a4f8512 commit 55d478c
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 94 deletions.
81 changes: 58 additions & 23 deletions common_functions.sh
Original file line number Diff line number Diff line change
@@ -1,33 +1,56 @@
IPTABLE_LOCK_FILE="/tmp/wdt.iptable.lock"

acquireIptableLock() {
while true
do
lockfile -r 0 $IPTABLE_LOCK_FILE
STATUS=$?
if [ $STATUS -eq 0 ]; then
break
fi
echo "Failed to get iptable lock"
done
}

releaseIptableLock() {
rm -f $IPTABLE_LOCK_FILE
}

blockSportByDropping() {
UNBLOCK_CMD="sudo ip6tables -D INPUT -p tcp --sport "$1" -j DROP"
acquireIptableLock
sudo ip6tables -A INPUT -p tcp --sport "$1" -j DROP
releaseIptableLock
}

blockDportByDropping() {
UNBLOCK_CMD="sudo ip6tables -D INPUT -p tcp --dport "$1" -j DROP"
acquireIptableLock
sudo ip6tables -A INPUT -p tcp --dport "$1" -j DROP
releaseIptableLock
}

blockSportByRejecting() {
UNBLOCK_CMD="sudo ip6tables -D INPUT -p tcp --sport "$1" -j REJECT"
acquireIptableLock
sudo ip6tables -A INPUT -p tcp --sport "$1" -j REJECT
releaseIptableLock
}

blockDportByRejecting() {
UNBLOCK_CMD="sudo ip6tables -D INPUT -p tcp --dport "$1" -j REJECT"
acquireIptableLock
sudo ip6tables -A INPUT -p tcp --dport "$1" -j REJECT
releaseIptableLock
}

unblockSportByDropping() {
sudo ip6tables -D INPUT -p tcp --sport "$1" -j DROP
}

unblockDportByDropping() {
sudo ip6tables -D INPUT -p tcp --dport "$1" -j DROP
}

unblockSportByRejecting() {
sudo ip6tables -D INPUT -p tcp --sport "$1" -j REJECT
}

unblockDportByRejecting() {
sudo ip6tables -D INPUT -p tcp --dport "$1" -j REJECT
undoLastIpTableChange() {
if [ ! -z "$UNBLOCK_CMD" ]; then
acquireIptableLock
eval "$UNBLOCK_CMD"
releaseIptableLock
unset UNBLOCK_CMD
fi
}

simulateNetworkGlitchesByRejecting() {
Expand All @@ -41,13 +64,13 @@ simulateNetworkGlitchesByRejecting() {
sleep 0.7 # sleep for 700ms, read/write timeout is 500ms, so must sleep
# more than that
echo "unblocking $port"
unblockSportByRejecting $port
undoLastIpTableChange
else
blockDportByRejecting $port
sleep 0.7 # sleep for 700ms, read/write timeout is 500ms, so must sleep
# more than that
echo "unblocking $port"
unblockDportByRejecting $port
undoLastIpTableChange
fi
done
}
Expand All @@ -63,13 +86,13 @@ simulateNetworkGlitchesByDropping() {
sleep 0.7 # sleep for 700ms, read/write timeout is 500ms, so must sleep
# more than that
echo "unblocking $port"
unblockSportByDropping $port
undoLastIpTableChange
else
blockDportByDropping $port
sleep 0.7 # sleep for 700ms, read/write timeout is 500ms, so must sleep
# more than that
echo "unblocking $port"
unblockDportByDropping $port
undoLastIpTableChange
fi
done
}
Expand All @@ -79,12 +102,17 @@ printServerLog() {
cat $DIR/server${TEST_COUNT}.log
}

wdtExit() {
undoLastIpTableChange
exit $1
}

checkLastCmdStatus() {
LAST_STATUS=$?
if [ $LAST_STATUS -ne 0 ] ; then
echo "exiting abnormally with status $LAST_STATUS - aborting/failing test"
printServerLog
exit $LAST_STATUS
wdtExit $LAST_STATUS
fi
}

Expand All @@ -93,7 +121,7 @@ checkLastCmdStatusExpectingFailure() {
if [ $LAST_STATUS -eq 0 ] ; then
echo "expecting wdt failure, but transfer was successful, failing test"
printServerLog
exit 1
wdtExit 1
fi
}

Expand Down Expand Up @@ -127,12 +155,12 @@ generateRandomFiles() {
if [ -z "$1" ]; then
echo "generateRandomFile expects the directory to be passed as first \
argument"
exit 1
wdtExit 1
fi
if [ -z "$2" ]; then
echo "generateRandomFile expects base file size to be passed as second \
argument"
exit 1
wdtExit 1
fi
mkdir -p $1
cd $1
Expand Down Expand Up @@ -203,6 +231,13 @@ verifyTransferAndCleanup() {
removeDestination
else
echo "Test $TEST_COUNT failed"
exit $STATUS
wdtExit $STATUS
fi
}

signalHandler() {
echo "Caught signal, exiting..."
wdtExit 1
}

trap signalHandler SIGINT
14 changes: 7 additions & 7 deletions wdt_download_resumption_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ STARTING_PORT=25000

if [ "$1" == "-h" ]; then
echo "$usage"
exit 0
wdtExit 0
fi
while getopts ":c:s:p:r:h:" opt; do
case $opt in
Expand Down Expand Up @@ -76,12 +76,12 @@ using transfer log"
BLOCK_SIZE_MBYTES=0
;;
*) echo "Invalid combination, valid values are 1, 2, 3 and 4"
exit 1
wdtExit 1
;;
esac
;;
h) echo "$usage"
exit
wdtExit
;;
\?) echo "Invalid option -$OPTARG" >&2
;;
Expand Down Expand Up @@ -236,7 +236,7 @@ if (( $DURATION > $EXPECTED_TRANSFER_DURATION_MILLIS \
|| $DURATION < $ABORT_AFTER_MILLIS )); then
echo "Abort timing test failed, exiting"
printServerLog
exit 1
wdtExit 1
fi
WDTBIN_CLIENT=$WDTBIN_CLIENT_OLD
removeDestination
Expand All @@ -256,12 +256,12 @@ wait $pidofsender
DURATION=$((END_TIME_MILLIS - START_TIME_MILLIS))
echo "Abort timing test, transfer duration ${DURATION} ms, expected duration \
${EXPECTED_TRANSFER_DURATION_MILLIS} ms."
unblockDportByDropping "$STARTING_PORT"
undoLastIpTableChange
if (( $DURATION > $EXPECTED_TRANSFER_DURATION_MILLIS \
|| $DURATION < $ABORT_AFTER_MILLIS )); then
echo "Abort timing test failed, exiting"
printServerLog
exit 1
wdtExit 1
fi
WDTBIN_SERVER=$WDTBIN_SERVER_OLD
removeDestination
Expand Down Expand Up @@ -314,4 +314,4 @@ TEST_COUNT=$((TEST_COUNT + 1))
echo "Good run, deleting logs in $DIR"
rm -rf "$DIR"

exit 0
wdtExit 0
Loading

0 comments on commit 55d478c

Please sign in to comment.