2013年12月13日 星期五

模擬網路狀態,封包延遲與封包丟棄

以下為一個模擬網路狀態的script,可以模擬封包延遲與丟棄封包,並限制上下傳頻寬



#!/bin/bash # # tc uses the following units when passed as a parameter. # kbps: Kilobytes per second # mbps: Megabytes per second # kbit: Kilobits per second # mbit: Megabits per second # bps: Bytes per second # Amounts of data can be specified in: # kb or k: Kilobytes # mb or m: Megabytes # mbit: Megabits # kbit: Kilobits # To get the byte figure from bits, divide the number by 8 bit # INIF="eth0" EXTIF="eth1" DELAYTIME="250ms" LOSSRATE="0%" UPRATE="512kbit" DOWNRATE="5500kbit" export INIF EXTIF DELAYTIME LOSSRATE UPRATE DOWNRATE PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin; export PATH echo -e "\nCurrent qdisc define rules" tc qdisc show echo -e "\nRemove old define rules" tc qdisc del dev $EXTIF root tc qdisc del dev $INIF root echo -e "\nDefine new define rules" tc qdisc add dev $EXTIF root handle 1: htb default 11 tc class add dev $EXTIF parent 1: classid 1:1 htb rate 1000Mbps tc class add dev $EXTIF parent 1:1 classid 1:11 htb rate $UPRATE ceil $UPRATE tc qdisc add dev $EXTIF parent 1:11 handle 10: netem delay $DELAYTIME loss $LOSSRATE tc qdisc add dev $INIF root handle 1: htb default 11 tc class add dev $INIF parent 1: classid 1:1 htb rate 1000Mbps tc class add dev $INIF parent 1:1 classid 1:11 htb rate $DOWNRATE ceil $DOWNRATE tc qdisc add dev $INIF parent 1:11 handle 10: netem delay $DELAYTIME loss $LOSSRATE echo -e "\nCurrent qdisc define rules" tc qdisc show echo -e "\n"

若是要在 iOS 設備上模擬,iOS已經提供開發者此項模擬功能。可以直接透過GUI進行設定,設定過程如下:

選擇 [設定]

選擇 Netowrk Link Conditioner


選擇某個Profile


Profile 內容




參考資料:
頻寬限制
封包延遲與丟棄封包