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/