T#8458: Added a memory check before uploading or downloading a file

This commit is contained in:
Amin Ben Ramdhane 2022-08-04 11:30:04 +00:00
parent 8cbaadc557
commit 9f8c5f5f28
2 changed files with 13 additions and 14 deletions

View file

@ -6,8 +6,6 @@
. /usr/share/bbfdm/bbf_uci_api
CAPTURE_FILE="/tmp/download_dump"
DOWNLOAD_DIAGNOSTIC_FILE="/tmp/bbfdm_download_diagnostic"
CONNECTION_TIMEOUT=60
download_launch() {
url=$1
@ -18,20 +16,23 @@ download_launch() {
if [ "$protocol" = "IPv4" ]; then proto="--ipv4"; elif [ "$protocol" = "IPv6" ]; then proto="--ipv6"; else proto=""; fi
# Disable acceleration on Broadcom devices to capture all packets with tcpdump
[ -e /usr/sbin/fcctl ] && { fcctl disable >/dev/null 2>&1; fcctl flush >/dev/null 2>&1; }
tcpdump -i "$device" tcp -w ${CAPTURE_FILE} > /dev/null 2>&1 &
FREE_SIZE=`free | grep Mem | awk '{print $4}'`
FREE_SIZE_BYTE=$((${FREE_SIZE}/1000))
tcpdump -i "$device" -C ${FREE_SIZE_BYTE} tcp -w ${CAPTURE_FILE} > /dev/null 2>&1 &
PID=$!
sleep 1
if [ ${url:0:7} = http:// -o ${url:0:6} = ftp:// ]; then
tx_bytes_before=$(ubus call network.device status "{'name':'$device'}" | jsonfilter -e @.statistics.tx_bytes)
rx_bytes_before=$(ubus call network.device status "{'name':'$device'}" | jsonfilter -e @.statistics.rx_bytes)
time1=$(date +%s)
curl $proto --fail --silent --connect-timeout ${CONNECTION_TIMEOUT} -o ${DOWNLOAD_DIAGNOSTIC_FILE} "$url"
curl $proto --fail --silent -o /dev/null "$url"
error_code="$?"
time2=$(date +%s)
tx_bytes_after=$(ubus call network.device status "{'name':'$device'}" | jsonfilter -e @.statistics.tx_bytes)
rx_bytes_after=$(ubus call network.device status "{'name':'$device'}" | jsonfilter -e @.statistics.rx_bytes)
[ "$error_code" = "6" ] && { $UCI_SET_BBF_DMMAP dmmap_diagnostics.download.DiagnosticState=Error_CannotResolveHostName; $UCI_COMMIT_BBF_DMMAP; kill $PID 2> /dev/null; return; }
[ "$error_code" = "7" ] && { $UCI_SET_BBF_DMMAP dmmap_diagnostics.download.DiagnosticState=Error_InitConnectionFailed; $UCI_COMMIT_BBF_DMMAP; kill $PID 2> /dev/null; return; }
[ "$error_code" = "22" ] && { $UCI_SET_BBF_DMMAP dmmap_diagnostics.download.DiagnosticState=Error_NoResponse; $UCI_COMMIT_BBF_DMMAP; kill $PID 2> /dev/null; return; }
@ -56,7 +57,6 @@ download_launch() {
$UCI_DELETE_BBF_DMMAP dmmap_diagnostics.@DownloadPerConnection[0]
fi
$UCI_COMMIT_BBF_DMMAP
rm ${DOWNLOAD_DIAGNOSTIC_FILE} 2>/dev/null
sleep 1
kill $PID >/dev/null 2>&1
# Enable acceleration on Broadcom devices after killing the tcpdump pid

View file

@ -6,8 +6,6 @@
. /usr/share/bbfdm/bbf_uci_api
CAPTURE_FILE="/tmp/upload_dump"
UPLOAD_DIAGNOSTIC_FILE="/tmp/bbfdm_upload_diagnostic.bin"
CONNECTION_TIMEOUT=60
upload_launch() {
url=$1
@ -19,16 +17,18 @@ upload_launch() {
if [ "$protocol" = "IPv4" ]; then proto="--ipv4"; elif [ "$protocol" = "IPv6" ]; then proto="--ipv6"; else proto=""; fi
# Disable acceleration on Broadcom devices to capture all packets with tcpdump
[ -e /usr/sbin/fcctl ] && { fcctl disable >/dev/null 2>&1; fcctl flush >/dev/null 2>&1; }
tcpdump -i "$device" tcp -w ${CAPTURE_FILE} > /dev/null 2>&1 &
FREE_SIZE=`free | grep Mem | awk '{print $4}'`
FREE_SIZE_BYTE=$((${FREE_SIZE}/1000))
tcpdump -i "$device" -C ${FREE_SIZE_BYTE} tcp -w ${CAPTURE_FILE} > /dev/null 2>&1 &
PID=$!
sleep 1
dd if=/dev/zero of=${UPLOAD_DIAGNOSTIC_FILE} bs="$size" count=1 2>/dev/null
if [ ${url:0:7} = http:// -o ${url:0:6} = ftp:// ]; then
tx_bytes_before=$(ubus call network.device status "{'name':'$device'}" | jsonfilter -e @.statistics.tx_bytes)
rx_bytes_before=$(ubus call network.device status "{'name':'$device'}" | jsonfilter -e @.statistics.rx_bytes)
time1=$(date +%s)
curl $proto --fail --silent --connect-timeout ${CONNECTION_TIMEOUT} -T ${UPLOAD_DIAGNOSTIC_FILE} "$url"
dd if=/dev/zero bs="$size" count=1 2>/dev/null | curl $proto --fail --silent -T - "$url"
error_code="$?"
time2=$(date +%s)
tx_bytes_after=$(ubus call network.device status "{'name':'$device'}" | jsonfilter -e @.statistics.tx_bytes)
@ -60,7 +60,6 @@ upload_launch() {
$UCI_DELETE_BBF_DMMAP dmmap_diagnostics.@UploadPerConnection[0]
fi
$UCI_COMMIT_BBF_DMMAP
rm ${UPLOAD_DIAGNOSTIC_FILE} >/dev/null 2>&1
sleep 3
kill $PID >/dev/null 2>&1
# Enable acceleration on Broadcom devices after killing the tcpdump pid