pfctl
我正在嘗試通過和在 Mac OS X 10.10 上進行流量整形(節流)dnctl
。
我已經使用 netcat ( ) 和合成隨機負載檔案實作了一個簡單的測試伺服器和客戶端,nc
以驗證我的虛擬網路管道節流是否正常運作。到目前為止,嘗試使用以下命令配置虛擬網路管道Murus 防火牆 GUI 應用程式似乎沒有正確限制流量(64MB 傳輸在約 200 毫秒內完成)。
下面是一個完整範例的OSX bash shell 腳本(需要brew install coreutils
) 。gdate
如果您在一個終端中運行它:
./throttle-test.sh server
另一個是
./throttle-test.sh client
他們將嘗試透過您的en0
介面傳輸 64MB 的有效負載(不使用,lo0
因為其龐大的 MTU 與 WAN 流量不同)。
我還測試了將文件傳輸到遠端Linux 筆記型電腦,以查看來源IP 和目標IP 均為本地IP 是否繞過限制,但即使到我的LAN/wifi 上的遠端計算機,速度也比限制限制快得多。
我的問題是什麼是正確的腳本來配置pfctl
和dnctl
限制此文件傳輸到給定的頻寬限制(例如 8mbps)。限制的範圍可以是特定的TCP埠。
注意 OS X 10.10 不再包含,ipfw
因此我正在尋找使用pfctl
和的東西dnctl
。
這是我的throttle-test.sh
文件:
#!/bin/bash
set -o errexit # always exit on error
set -o errtrace # trap errors in functions as well
set -o pipefail # don't ignore exit codes when piping output
set -o posix # more strict failures in subshells
# set -x # enable debugging
IFS="$(printf "\n\t")"
setup_payload() {
local payload_path="$1"
local size_kbytes="$2"
mkdir -p $(basename "${payload_path}")
if [[ -f "${payload_path}" ]]; then
local on_disk=$(wc -c < "${payload_path}")
fi
if [[ ${on_disk} -ne $((${size_kbytes} * 1024)) ]]; then
echo "creating payload file ${payload_path}"
dd if=/dev/urandom of="${payload_path}" \
bs=1024 count="${size_kbytes}" &> /dev/null
fi
}
start_server() {
local payload_path="$1"
local ip="$2"
local port="$3"
while true; do
echo "Starting netcat server for ${payload_path} on ${ip}:${port}"
nc -l "${ip}" "${port}" < "${payload_path}"
sleep 1
done
}
hash() {
shasum -a 256 "$1" | cut -d " " -f 1
}
verify_hashes() {
# Sanity check no funny business
from_hash=$(hash "$1")
to_hash=$(hash "$2")
if [[ "${from_hash}" != "${to_hash}" ]]; then
echo "checksums did not match ${from_hash} ${to_hash}" 1>&2
exit 10
fi
}
client() {
local payload_path="$1"
local ip="$2"
local port="$3"
# time how long it takes to transfer the payload file
start=$(gdate +%s%3N)
nc -d "${ip}" "${port}" > "${payload_path}.client"
stop=$(gdate +%s%3N)
verify_hashes "${payload_path}" "${payload_path}.client"
local duration=$((${stop} - ${start}))
echo "${duration}"
}
main() {
local size_kbytes=$((64 * 1024)) # 64 MB
local payload_path="/tmp/throttle-test/data-${size_kbytes}.bin"
local port="${PORT-9112}"
# You may need to change this if you are on linux
local interface="${INTERFACE-en0}"
local ip=$(ipconfig getifaddr "${interface}")
setup_payload "${payload_path}" "${size_kbytes}"
case "$1" in
server)
start_server "${payload_path}" "${ip}" "${port}"
;;
client)
local duration=$(client "${payload_path}" "${ip}" "${port}")
echo "Transfered ${size_kbytes} kbytes in ${duration} ms"
;;
*)
echo "Usage: $0 <server|client>"
;;
esac
}
main "$@"
更新
這是我到目前為止所擁有的。這對於下載方向似乎可以正常工作,但在上傳方向上根本無法節流。
throttle_start() {
local down_mbps="$1"
local up_mbps="$2"
local delay=$(($3 / 2))
sudo dnctl pipe 1 config bw "${down_mbps}Mbit/s" delay "${delay}"
sudo dnctl pipe 2 config bw "${up_mbps}Mbit/s" delay "${delay}"
(cat /etc/pf.conf && \
echo 'dummynet-anchor "throttle"' && \
echo 'anchor "throttle"') | sudo pfctl -f -
cat << EOF | sudo pfctl -a throttle -f -
dummynet in quick proto tcp from any port = 9112 to any pipe 1
dummynet out quick proto tcp from any to any port = 9112 pipe 2
EOF
sudo pfctl -q -e
}
答案1
這是我在 El Capitan 10.11 上使用的腳本,並取得了一些成功:
#!/bin/bash
# Reset dummynet to default config
dnctl -f flush
# Compose an addendum to the default config: creates a new anchor
(cat /etc/pf.conf &&
echo 'dummynet-anchor "my_anchor"' &&
echo 'anchor "my_anchor"') | pfctl -q -f -
# Configure the new anchor
cat <<EOF | pfctl -q -a my_anchor -f -
no dummynet quick on lo0 all
dummynet out proto tcp from any to any port 1:65535 pipe 1
EOF
# Create the dummynet queue
dnctl pipe 1 config bw 40000byte/s
# Activate PF
pfctl -E
# to check that dnctl is properly configured: sudo dnctl list
唯一相關的區別似乎是no dummynet quick on lo0 all
,我真的不知道它的作用,在這裡找到:https://www.reddit.com/r/osx/comments/3g7dim/limiting_bandwidth_per_application/