用linux做nat服務,用tc限制流量

來源:互聯網
上載者:User

最近有同事用bt和電驢瘋狂下載,我們上網打cs受到極大影響,所以對nat上網做了流量控制,將一點經驗介紹給網友,希望對cs fans 有所協助.
我們上網環境如下:
    eth0 外網ip :a.b.c.d
    eth1 內網ip1:192.168.0.0/24  給老闆和bt
    eth2 內網ip2:192.168.1.0/24  給我和csfans
用linux 做nat 命令如下:
echo 1 >; /proc/sys/net/ipv4/ip_forward
iptables -F
iptables -t nat -F    ----清除舊規則
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eth0 -j SNAT --to a.b.c.d   ---為內網ip1 做nat
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j SNAT -- to a.b.c.d   ---為內網ip2 做nat
------------為流量控製做基於fw過濾器的標記
iptables -I PREROUTING -t mangle -p tcp -s 192.168.0.0/24 -j MARK --set-mark 1
iptables -I PREROUTING -t mangle -p tcp -s 192.168.1.0/24 -j MARK --set-mark 2
------------為上傳速率做流量控制
tc 要求核心2.4.18以上,所以不夠的要升級
tc 只能控制網卡發送包的速率,所以上傳速率的限制要在eth0上做
----刪除舊有隊列
tc qdisc del dev eth0 root
----加一個根隊列,速率用網卡的速率10Mbit,也可用上傳的速率
tc qdisc add dev eth0 root handle 100: cbq bandwidth 10Mbit avpkt 1000
----加一個根類
tc class add dev eth0 parent 100:0 classid 100:1 cbq bandwidth 10Mbit rate 10Mbit allot 1514 weight 1Mbit prio 8 maxburst 8 avpkt 1000 bounded
----加一個子類用於內網1速率限制為300Kbit
tc class add dev eth0 parent 100:1 classid 100:2 cbq bandwidth 10Mbit rate 300Kbit allot 1513 weight 30Kbit prio 5 maxburst 8 avpkt 1000 bounded
----加一個子類用於內網2速率限制為320Kbit
tc class add dev eth0 parent 100:1 classid 100:3 cbq bandwidth 10Mbit rate 320Kbit allot 1513 weight 32Kbit prio 6 maxburst 8 avpkt 1000 bounded
----設定隊列規則
tc qdisc add dev eth0 parent 100:2 sfq quantum 1514b perturb 15
tc qdisc add dev eth0 parent 100:3 sfq quantum 1514b perturb 15
------將隊列和fw過濾器映射起來 其中hand 1 的1是開始用iptables 做的標記,hand 2 的2也是開始用iptables 做的標記
tc filter add dev eth0 parent 100:0 protocol ip prio 1 handle 1 fw classid 100:2
tc filter add dev eth0 parent 100:0 protocol ip prio 2 handle 2 fw classid 100:3
-----------------------再做下載限制我只限制了老闆和bt的下載速率,過濾器是用u32
tc qdisc del dev eth1 root
tc qdisc add dev eth1 root handle 200: cbq bandwidth 10Mbit avpkt 1000
tc class add dev eth1 parent 200:0 classid 200:1 cbq bandwidth 10Mbit rate 10Mbit allot 1514 weight 2Kbit prio 8 maxburst 8 avpkt 1000 bounded
tc class add dev eth1 parent 200:1 classid 200:2 cbq bandwidth 10Mbit rate 1000Kbit allot 1513 weight 1Mbit prio 5 maxburst 8 avpkt 1000 bounded
tc qdisc add dev eth1 parent 200:2 sfq quantum 1514b perturb 15
tc filter add dev eth1 parent 200:0 protocol ip prio 25 u32 match ip dst 192.168.0.0/24 flowid 200:2

----------------------
現在可以用tc -s qdisc ls dev eth0
     tc -s qdisc ls dev eth1
          tc -s class ls dev eth0
          tc -s class ls dev eth1 監視流量

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.