高手請問:如何在上傳時限速???



贊助商連結


beppin
2001-11-24, 07:12 AM
我是與鄰居分享ip方式使用adsl...
但是鄰居常上傳資料,例如寄e-mail,以至於拖累我下傳速度...
我找了許多文章,內容只有指ftp用限速,在流量上似乎只有裝設硬體...
難道沒有其他方法嗎?

贊助商連結


bx2aa
2001-11-24, 11:03 AM
最初由 beppin 發表
我是與鄰居分享ip方式使用adsl...
但是鄰居常上傳資料,例如寄e-mail,以至於拖累我下傳速度...
我找了許多文章,內容只有指ftp用限速,在流量上似乎只有裝設硬體...
難道沒有其他方法嗎?
 
利用 Linux 的 TC(Traffic Control)
 
轉貼 Linux下實現流量限制
 
Linux內核2.1.x及後續版本堶悼D要是引入了traffic control代碼,
來實現IP帶寬的分配和管理。
 
TC的特點
 
TC是Traffic Control的縮寫,中文意思即為"流量控制"。
TC有很大的伸縮性。
作為一個提供虛擬主機服務的ISP來說,
它可以利用Linux的流量控制來給不同的客戶提供不同的服務質量保證。
傳統的出售虛擬主機或者提供主頁存放服務的ISP
通常是提供不同的磁碟空間來作為不同檔次的服務,
例如一個月100元可以獲得100M的空間。
如果使用Linux的流量控制(TC),我們就可以多提供一種有區分的不同的服務,
例如你是一個提供虛擬主機服務的ISP,你可以有如下多的不同的服務選擇:
· 服務水平1: 花費¥100/月??流覽者獲得250Kbps的流覽頁面速度。
· 服務水平2: 花費¥150/月--250Kbps,
但是如果帶寬允許的話在午夜0點到臨晨6點之間可以達到1Mbps。
· 服務水平3: 花費¥200/月--250Kbps, 但是如果帶寬允許的話可以最大達到1Mbps,
不限制時間(與服務水平2區別之處)。
· 服務水平4:花費¥500/月--提供1Mbps的高速的帶寬保證。
適合視頻和音頻流的服務。
由上面的例子,我們可以利用TC很方便的指定各種有控制的服務規則。
上面提到的每天不同時段提供不同的服務,
我們可以很容易的用crontab來定時運行一些腳本去改變 帶寬分配規則來達到。
這是一個比較好的例子,我們在後面將介紹一個實例。
 
TC的使用實例說明
 
下面我們提供一個利用TC來實現對在一個Linux伺服器上的
兩個虛擬主機實行不同的帶寬策略的例子。
在該例中,我們將講述如何配置和測試TC。
 
編譯內核
至於如何編譯一個新的內核已經不屬於本章節討論的範圍,
我們假設你已經知道如何重新編譯一個內核。
編譯內核時將以下幾個內核選項選中:
"kernel/User netlink socket"和"Netlink device emulation"。
這樣TC就可以利用netlink來與內核傳送資訊。
同時將所有的排隊演算法選上,位於包括
"Fair queueing"
"CBQ packet scheduler"
"CSZ packet scheduler"
"the simples PRIO pseudoscheduler"
"RED queue"
"SFQ queue"
"TBF queue"
"QoS support"
"rate estimator"
"packet classifier API"
"routing-tables-based classifier"
"U32 classifier"
"special RSVP classifier and special RSVP classifier for IPv6"。
 
選中這些選項以後,按正常的編譯內核步驟編譯內核,
然後安裝新內核,並用新內核重新啟動系統。
 
編譯TC套裝軟體
 
可以在下面的位址下載到我們需要的軟體,然後按照套裝軟體堛獄〝編譯它:
ftp://linux.wauug.org/pub/net/ip-routing/iproute2-current.tar.gz
通常我們要做的只是簡單的輸入make就可以了。
 
TC 的設定
 
圖1. CBQ 樹圖
圖一是我們將配置的一個系統的簡單的樹狀圖示範。
兩個葉子節點從父節點分配帶寬,
IP地址10.0.0.10(識別字1:1)和地址10.0.0.11(識別字1:2) 是介面eth0上的IP別名,
它們共同分享父節點(識別字1:0)的帶寬。
這個例子堶悼u涉及到了對一個介面上的流量控制,
大家可以仿照這個例子構造自己感興趣的控制多個 周邊設備的配置。
配置QoS特性的第一步就是將qdisc加入到一個介面上,例如本例子:
qdisc add dev eth0 root handle 1: ...
 
然後定義你需要區別的類別。
不同的類別對應不同的流量控制類型。
我們的例子中,使用如下的語句:
 
tc class add dev eth0 parent 1:0 classid X:Y ...
 
我們的例子中只使用了一層深的類別樹。
當然,我們可以構造多層深度的複雜的樹,基本的原則是一樣的:
就是一個子節點(如圖1所示)繼承一個父節點的資源同時進一步根據類
的定義去分配父節點的資源。
例如,父類1:0擁有該設備的全部帶寬,那麼子節點1:1不可能獲得超過10Mbits的帶寬,
當然本例子中是限制為1Mbps。
最後定義"IP分組--類別"的映射規則,用來告訴系統的分類器,
經過路由器調度的某IP分組該對應什麼類型。
首先,將一個分類器與輸出介面關聯起來:
filter add dev eth0 parent 1:0 protocol ip ...
 
然後,定義"IP分組--類別"的映射規則。
本例子中,將利用IP分組的源位址來作為分類的關鍵字。
下面的腳本完成了這個功能。
關於腳本中TC等命令的參數,大家可以參考隨機的文檔,這堶酋騣g幅,不做介紹了。
 
#! /bin/sh
#path to tc and the ip utilities;
#change to reflect yours.
TC=./iproute2/tc/tc
IP=./iproute2/ip/ip
##################################################
#Addresses to be aliased
#change or add more to reflect yours
#
ALIAS1=10.0.0.10
ALIAS2=10.0.0.11
##################################################
# add ip aliasing support
#uncomment if you want to use the ip utility to
#add ip-aliasing for you
#
#$IP addr add $ALIAS1 dev eth0
#$IP addr add $ALIAS2 dev eth0
##################################################
# Attaching a device queue discipline to an
# interface a device queue discipline is
# equivalent almost to a device manager
#
#Attach CBQ to eth0
#Things you might need to change:
# bandwidth -- the bandwidth of the eth0 device
# note it must match the devices real bandwidth
# allot -- it is safe to leave it at the MTU of
# the device
# avpkt -- the average packet size that you
# suspect will be seen safe to leave at 1000
# for Ethernet with MTU of 1514 bytes
# mpu -- minimum packet size
#
$TC qdisc add dev eth0 root handle 1: cbq
bandwidth 10Mbit allot 1514 cell 8 avpkt 1000
mpu 64
##################################################
# Attaching class queue disciplines
# bounded -- it is bound to the rate allocated;
# cant borrow even if there is a lot of idle
# bandwidth just sitting there isolated -- cannot
# share its bandwidth to other classes prio is the
# priority assigned 0 being the highest and 7 the
# lowest weight -- safer to leave at 1
# queue discipline setup. Classid 1:1 will have a
# rate of 1Mbps which is bounded.
#
$TC class add dev eth0 parent 1:0 classid 1:1 cbq
bandwidth 10Mbit rate 1Mbit avpkt 1000 prio 5
bounded isolated allot 1514 weight 1 maxburst 21
#rate 1Mbit avpkt 1000 prio 5 bounded allot 1514
#weight 1 maxburst 21
# Classid 1:2 will have a rate of 3Mbps which is
# bounded.
$TC class add dev eth0 parent 1:0 classid 1:2 cbq
bandwidth 10Mbit rate 3Mbit avpkt 1000 prio 5
bounded allot 1514 weight 1 maxburst 21
##################################################
# Define the filter to be attached to eth0
# Create with hash table of 256 slots with ID 1:
#
$TC filter add dev eth0 parent 1:0 protocol ip
prio 5 handle 1: u32 divisor 256
##################################################
# define the criteria for mapping incoming packets
# to classes. Add to the 5th slot of hash table a
# rule to select virtual address ALIAS1 direct it
# to class 1:1
#
$TC filter add dev eth0 parent 1:0 prio 5 u32
ht 1:6: match ip src $ALIAS1 flowid 1:1
# Add to 6th slot of hash table rule to select
# ALIAS2 direct it to class 1:2
$TC filter add dev eth0 parent 1:0 prio 5 u32
ht 1:6: match ip src $ALIAS2 flowid 1:2
## Lookup hash table, if it is not fragmented
## frame. Use protocol as hash key
#
$TC filter add dev eth0 parent 1:0 prio 5 handle
::1 u32 ht 800:: match ip nofrag
offset mask 0x0F00 shift 6
hashkey mask 0x00ff0000 at 8 link 1:
#
#some more examples of how to use u32
# Add to 4th slot of hash table rule to select
# tcp/telnet to 193.233.7.75 direct it to class
# 1:4 and prescribe to fall to best effort,
# if traffic violates TBF (32kbit,5K)
#$TC filter add dev eth1 parent 1:0 prio 5 u32
# ht 1:4: match ip dst 193.233.7.75
# match tcp dst 0x17 0xffff
# flowid 1:4
# police rate 32kbit buffer 5kb/8 mpu 64
# mtu 1514 index 1
## Add to 1st slot of hash table rule to select
## icmp to 193.233.7.75 direct it to class 1:3
## and prescribe to fall to best effort,
## if traffic violate TBF (10kbit,5K)
#$TC filter add dev eth1 parent 1:0 prio 5 u32
# ht 1:4: match ip dst 193.233.7.75
# match tcp dst 0x17 0xffff
# flowid 1:4
# police rate 32kbit buffer 5kb/8 mpu 64
# mtu 1514 index 1
## Add to 1st slot of hash table rule to select
## icmp to 193.233.7.75 direct it to class 1:3
## and prescribe to fall to best effort,
## if traffic violate TBF (10kbit,5K)
#$TC filter add dev eth1 parent 1:0 prio 5 u32
# ht 1:: sample ip protocol 1 0xff
# match ip dst 193.233.7.75 flowid 1:3
# police rate 10kbit buffer 5kb/8 mpu 64
# mtu 1514 index 2
##################################################
#Look at all that we created:
#
echo "---- qdisc parameters ----------"
$TC qdisc ls dev eth0
echo "---- Class parameters ----------"
$TC class ls dev eth0
echo "---- filter parameters ----------"
$TC filter ls dev eth0
 
測試
 
我們的例子當中,是有在同一個Linux伺服器上有兩個虛擬WWW伺服器,
我們的設定是採用了IP別名,
將兩個位址同時綁在一個介面上。
我們可以用ftp來測試。
首先用 ftp連接10.0.0.10伺服器,我們可以獲得大概1Mbps的速率;
然後ftp另外的位址10.0.0.11,我們可以獲得大概3Mbps的速率

powerbug
2001-11-24, 08:03 PM
最初由 beppin 發表
我是與鄰居分享ip方式使用adsl...
但是鄰居常上傳資料,例如寄e-mail,以至於拖累我下傳速度...
我找了許多文章,內容只有指ftp用限速,在流量上似乎只有裝設硬體...
難道沒有其他方法嗎?

最快就是跟你鄰居溝通了。

ghost1978
2001-11-25, 01:37 AM
請問你廣福路是在哪一縣市呀那邊的西施好看嗎
若好看的話我也要去瞧一瞧

powerbug
2001-11-25, 03:24 PM
最初由 ghost1978 發表
請問你廣福路是在哪一縣市呀那邊的西施好看嗎
若好看的話我也要去瞧一瞧
桃園八德市廣福路,大家審美觀念不同,我覺得好看並不代表他們一定好看,ghost兄如果
你不是住在桃園的話不用特別為了這件事跑來看。
離題太多了,版主sorry。