2015年1月5日 星期一

[Linux 文章收集] 18 commands to monitor network bandwidth on Linux server

Source From Here
Network monitoring on Linux
This post mentions some linux command line tools that can be used to monitor the network usage. These tools monitor the traffic flowing through network interfaces and measure the speed at which data is currently being transferred. Incoming and outgoing traffic is shown separately.

Some of the commands, show the bandwidth used by individual processes. This makes it easy to detect a process that is overusing network bandwidth. The tools have different mechanisms of generating the traffic report. Some of the tools like nload read the "/proc/net/dev" file to get traffic stats, whereas some tools use the pcap library to capture all packets and then calculate the total size to estimate the traffic load.

Here is a list of the commands, sorted by their features.
1. Overall bandwidth - nloadbmonslurmbwm-ng, cbm, speedometer, netload
2. Overall bandwidth (batch style output) - vnstatifstatdstatcollectl
2. Bandwidth per socket connection - iftopiptraftcptrack, pktstat, netwatch, trafshow
3. Bandwidth per process - nethogs

Nload
nload is a commandline tool that allows users to monitor the incoming and outgoing traffic separately. It also draws out a graph to indicate the same, the scale of which can be adjusted. Easy and simple to use, and does not support many options.
$ nload

iftop
Iftop measures the data flowing through individual socket connections, and it works in a manner that is different from Nload. Iftop uses the pcap library to capture the packets moving in and out of the network adapter, and then sums up the size and count to find the total bandwidth under use.

Although iftop reports the bandwidth used by individual connections, it cannot report the process name/id involved in the particular socket connection. But being based on the pcap library, iftop is able to filter the traffic and report bandwidth usage over selected host connections as specified by the filter.
$ sudo iftop -n # The n option prevents iftop from resolving ip addresses to hostname

iptraf
Iptraf is an interactive and colorful IP Lan monitor. It shows individual connections and the amount of data flowing between the hosts. Here is a screenshot
$ sudo iptraf

nethogs
Nethogs is a small 'net top' tool that shows the bandwidth used by individual processes and sorts the list putting the most intensive processes on top. In the event of a sudden bandwidth spike, quickly open nethogs and find the process responsible. Nethogs reports the PID, user and the path of the program.
$ sudo nethogs

bmon
Bmon (Bandwidth Monitor) is a tool similar to nload that shows the traffic load over all the network interfaces on the system. The output also consists of a graph and a section with packet level details.


slurm
Slurm is 'yet' another network load monitor that shows device statistics along with an ascii graph. It supports 3 different styles of graphs each of which can be activated using the cs and l keys. Simple in features, slurm does not display any further details about the network load.
$ slurm -s -i eth0

tcptrack
Tcptrack is similar to iftop, and uses the pcap library to capture packets and calculate various statistics like the bandwidth used in each connection. It also supports the standard pcap filters that can be used to monitor specific connections.


Vnstat
Vnstat is bit different from most of the other tools. It actually runs a background service/daemon and keeps recording the size of data transfer all the time. Next it can be used to generate a report of the history of network usage.
$ service vnstat status
vnstatd (pid 33010) is running...

Running vnstat without any options would simply show the total amount of data transfer that took place since the date the daemon is running.


To monitor the bandwidth usage in realtime, use the '-l' option (live mode). It would then show the total bandwidth used by incoming and outgoing data, but in a very precise manner without any internal details about host connections or processes.


Vnstat is more like a tool to get historic reports of how much bandwidth is used everyday or over the past month. It is not strictly a tool for monitoring the network in real time.

bwm-ng
Bwm-ng (Bandwidth Monitor Next Generation) is another very simple real time network load monitor that reports a summary of the speed at which data is being transferred in and out of all available network interfaces on the system.
$ bwm-ng


cbm - Color Bandwidth Meter
A tiny little simple bandwidth monitor that displays the traffic volume through network interfaces. No further options, just the traffic stats are display and updated in realtime.


speedometer
Another small and simple tool that just draws out good looking graphs of incoming and outgoing traffic through a given interface.
$ speedometer -r eth0 -t eth0

Pktstat
Pktstat displays all the active connections in real time, and the speed at which data is being transferred through them. It also displays the type of the connection, i.e. tcp or udp and also details about http requests if involved.
$ sudo pktstat -i eth0 -nt

Netwatch
Netwatch is part of the netdiag collection of tools, and it too displays the connections between local host and other remote hosts, and the speed at which data is transferring on each connection.
$ sudo netwatch -e eth0 -nt

Trafshow
Like netwatch and pktstat, trafshow reports the current active connections, their protocol and the data transfer speed on each connection. It can filter out connections using pcap type filters. Monitor only tcp connections
$ sudo trafshow -i eth0 tcp

Netload
The netload command just displays a small report on the current traffic load, and the total number of bytes transferred since the program start. No more features are there. Its part of the netdiag.
$ netload eth0

ifstat
The ifstat reports the network bandwidth in a batch style mode. The output is in a format that is easy to log and parse using other programs or utilities.


dstat
Dstat is a versatile tool (written in python) that can monitor different system statistics and report them in a batch style mode or log the data to a csv or similar file. This example shows how to use dstat to report network bandwidth


collectl
Collectl reports system statistics in a style that is similar to dstat, and like dstat it is gathers statistics about various different system resources like cpu, memory, network etc. Over here is a simple example of how to use it to report network usage/bandwidth.


Summary
Those were a few handy commands to quickly check the network bandwidth on your linux server. However these need the user to login to the remote server over ssh. Alternatively web based monitoring tools can also be used for the same task.

Ntop and Darkstat are some of the basic web based network monitoring tools available for Linux. Beyond these lie the enterprise level monitoring tools likeNagios that provide a host of features to not just monitor a server but entire infrastructure.

Supplement
Linux Network Statistics Tools / Commands
iftop – finding traffic hogs
How to install iftop?
Install EPEL and additional repositories on CentOS and Red Hat

* CentOS 6.3 Instance Giving "Cannot retrieve metalink for repository: epel" Error
$ sudo sed -i "s/mirrorlist=https/mirrorlist=http/" /etc/yum.repos.d/epel.repo
$ yum -y update

iftop 流量監控使用教學,立刻抓出吃流量兇手


2015年1月4日 星期日

[Linux 文章收集] TC 入門 (traffic control)

Source From Here 
TC 原理介紹 
Linux 操作系統中的流量控制器 TCTraffic Control)用於 Linux 內核的流量控制,主要是通過在輸出端口處建立一個隊列來實現流量控制。Linux流量控制的基本原理如下圖所示。 
 

接收包從輸入接口(Input Interface)進來後,經過流量限制(Ingress Policing)丟棄不符合規定的數據包,由輸入多路分配器(Input De-Multiplexing)進行判斷選擇:如果接收包的目的是本主機,那麼將該包送給上層處理;否則需要進行轉發,將接收包交到轉發塊(Forwarding Block)處理。轉發塊同時也接收本主機上層(TCP、UDP等)產生的包。轉發塊通過查看路由表,決定所處理包的下一跳。然後,對包進行排列以便將它們傳送到輸出接口(Output Interface)。一般我們只能限制網卡發送的數據包,不能限制網卡接收的數據包,所以我們可以通過改變發送次序來控制傳輸速率。Linux 流量控制主要是在輸出接口排列時進行處理和實現的。 

TC 規則 

1. 流量控制方式 
流量控制包括以下幾種方式: 
* SHAPING (限制) 
當流量被限制,它的傳輸速率就被控制在某個值以下。限制值可以大大小於有效帶寬,這樣可以平滑突發數據流量,使網絡更爲穩定。shaping(限制)只適用於向外的流量。
When traffic is shaped, its rate of transmission is under control. Shaping may be more than lowering the available bandwidth - it is also used to smooth out bursts in traffic for better network behaviour. Shaping occurs on egress.

* SCHEDULING(調度) 
通過調度數據包的傳輸,可以在帶寬範圍內,按照優先級分配帶寬。SCHEDULING (調度) 也只適於向外的流量。
By scheduling the transmission of packets it is possible to improve interactivity for traffic that needs it while still guaranteeing bandwidth to bulk transfers. Reordering is also called prioritizing, and happens only on egress.

* POLICING(策略) 
SHAPING用於處理向外的流量,而POLICIING(策略)用於處理接收到的數據。
Where shaping deals with transmission of traffic, policing pertains to traffic arriving. Policing thus occurs on ingress.

* DROPPING(丟棄) 
如果流量超過某個設定的帶寬,就丟棄數據包,不管是向內還是向外。
Traffic exceeding a set bandwidth may also be dropped forthwith, both on ingress and on egress.

2. 流量控制處理對象 
流量的處理由三種對象控制,它們是:qdisc (排隊規則)、class (類別) 和 filter (過濾器)。 

* QDISC (排隊規則) 
QDisc (排隊規則) 是queueing discipline 的簡寫,它是理解流量控制(traffic control)的基礎。無論何時,內核如果需要通過某個網絡接口發送數據包,它都需要按照爲這個接口配置的qdisc(排隊規則)把數據包加入隊列。然後,內核會儘可能多地從qdisc裏面取出數據包,把它們交給網絡適配器驅動模塊。最簡單的QDisc是pfifo它不對進入的數據包做任何的處理,數據包採用先入先出的方式通過隊列。不過,它會保存網絡接口一時無法處理的數據包。
qdisc is short for ’queueing discipline’ and it is elementary to understanding traffic control. Whenever the kernel needs to send a packet to an interface, it is enqueued to the qdisc configured for that interface. Immediately afterwards, the kernel tries to get as many packets as possible from the qdisc, for giving them to the network adaptor driver.

* CLASS(類) 
某些QDisc(排隊規則)可以包含一些類別,不同的類別中可以包含更深入的QDisc(排隊規則),通過這些細分的QDisc還可以爲進入的隊列的數據包排隊。通過設置各種類別數據包的離隊次序,QDisc可以爲設置網絡數據流量的優先級。
Some qdiscs can contain classes, which contain further qdiscs - traffic may then be enqueued in any of the inner qdiscs, which are within the classes. When the kernel tries to dequeue a packet from such a classful qdisc it can come from any of the classes. A qdisc may for example prioritize certain kinds of traffic by trying to dequeue from certain classes before others.

* FILTER(過濾器) 
Filter(過濾器)用於爲數據包分類,決定它們按照何種QDisc進入隊列。無論何時數據包進入一個劃分子類的類別中,都需要進行分類。分類的方法可以有多種,使用fileter(過濾器)就是其中之一。使用filter(過濾器)分類時,內核會調用附屬於這個類(class)的所有過濾器,直到返回一個判決。如果沒有判決返回,就作進一步的處理,而處理方式和QDISC有關。需要注意的是,filter(過濾器)是在QDisc內部,它們不能作爲主體。
A filter is used by a classful qdisc to determine in which class a packet will be enqueued. Whenever traffic arrives at a class with subclasses, it needs to be classified. Various methods may be employed to do so, one of these are the filters. All filters attached to the class are called, until one of them returns with a verdict. If no verdict was made, other criteria may be available. This differs per qdisc.


3. 操作原理 
Classes form a tree, where each class has a single parent. A class may have multiple children. Some qdiscs allow for runtime addition of classes (CBQ, HTB) while others (PRIO) are created with a static number of children. Qdiscs which allow dynamic addition of classes can have zero or more subclasses to which traffic may be enqueued. 

Furthermore, each class contains a leaf qdisc which by default has pfifo behaviour though another qdisc can be attached in place. This qdisc may again contain classes, but each class can have only one leaf qdisc. 

When a packet enters a classful qdisc it can be classified to one of the classes within. Three criteria are available, although not all qdiscs will use all three: 
* tc 過濾器(tc filter) 
如果過濾器附屬於一個類,相關的指令就會對它們進行查詢。過濾器能夠匹配數據包頭所有的域,也可以匹配由ipchains或者iptables做的標記。
If tc filters are attached to a class, they are consulted first for relevant instructions. Filters can match on all fields of a packet header, as well as on the firewall mark applied by ipchains or iptables.

* 服務類型(Type of Service) 
某些QDisc有基於服務類型(Type of Service,ToS)的內置的規則爲數據包分類。
Some qdiscs have built in rules for classifying packets based on the TOS field.

* skb->priority 
用戶空間的應用程序可以使用 SO_PRIORITY 選項在 skb->priority 域設置一個類的 ID。 樹的每個節點都可以有自己的過濾器,但是高層的過濾器也可以直接用於其子類。 如果數據包沒有被成功歸類,就會被排到這個類的葉子QDisc的隊中。相關細節在各個QDisc的手冊頁中。
Userspace programs can encode a class-id in the ’skb->priority’ field using the SO_PRIORITY option. Each node within the tree can have its own filters but higher level filters may also point directly to lower classes. If classification did not succeed, packets are enqueued to the leaf qdisc attached to that class. Check qdisc specific manpages for details, however.

4. 命名規則 
所有的 QDisc、類 和 過濾器 都有ID。ID可以手工設置,也可以有內核自動分配。ID由一個主序列號和一個從序列號組成,兩個數字用一個冒號分開。 

* QDISC 
一個QDisc會被分配一個主序列號,叫做句柄(handle),然後把從序列號作爲類的命名空間。句柄採用象10:一樣的表達方式。習慣上需要爲有子類的QDisc顯式地分配一個句柄。
A qdisc, which potentially can have children, gets assigned a major number, called a ’handle’, leaving the minor number namespace available for classes. The handle is expressed as ’10:’. It is customary to explicitly assign a handle to qdiscs expected to have children.

* 類(CLASS) 
在同一個 QDisc 裏面的類分享這個 QDisc 的主序列號,但是每個類都有自己的從序列號,叫做 類識別符 (classid)。類識別符只與父 QDisc 有關,和父類無關。類的命名習慣和 QDisc 的相同。
Classes residing under a qdisc share their qdisc major number, but each have a separate minor number called a ’classid’ that has no relation to their parent classes, only to their parent qdisc. The same naming custom as for qdiscs applies.

* 過濾器(FILTER) 
過濾器的ID有三部分,只有在對過濾器進行 hashed filter hierarchy 會用到。詳情請參考 tc-filters 手冊頁。
Filters have a three part ID, which is only needed when using a hashed filter hierarchy.

5、單位 
tc 命令的所有參數都可以使用浮點數,可能會涉及到以下計數單位。 
* 帶寬或者流速單位: 
kbps 千字節/秒
mbps 兆字節/秒
kbit KBits/秒
mbit MBits/秒
bps 或者一個無單位數字 字節數/秒

* 數據的數量單位 
kb或者k 千字節
mb或者m 兆字節
mbit 兆bit
kbit 千bit
b或者一個無單位數字 字節數

* 時間的計量單位 
s、sec或者secs 秒
ms、msec或者msecs 分鐘
us、usec、usecs或者一個無單位數字 微秒

TC 命令 
tc 可以使用以下命令對 QDisc、類和過濾器進行操作: 
- add 
在一個節點里加入一個 QDisc、類或者過濾器。添加時,需要傳遞一個 parent 作爲參數,傳遞參數時既可以使用 ID 也可以直接傳遞設備的 root。如果要建立一個 QDisc 或者過濾器,可以使用句柄 (handle) 來命名;如果要建立一個類,可以使用類識別符 (classid) 來命名。
Add a qdisc, class or filter to a node. For all entities, a parent must be passed, either by passing its ID or by attaching directly to the root of a device. When creating a qdisc or a filter, it can be named with the handle parameter. A class is named with the classid parameter.

- remove 
刪除有某個句柄 (handle) 指定的 QDisc,根 QDisc (root)也可以刪除。被刪除 QDisc 上的所有子類以及附屬於各個類的過濾器都會被自動刪除。
A qdisc can be removed by specifying its handle, which may also be ’root’. All subclasses and their leaf qdiscs are automatically deleted, as well as any filters attached to them.

- change 
以替代的方式修改某些條目。除了句柄 (handle) 和 parent 不能修改以外,change命令的語法和add命令相同。換句話說,change 命令不能移動節點的位置。
Some entities can be modified ’in place’. Shares the syntax of ’add’, with the exception that the handle cannot be changed and neither can the parent. In other words, change cannot move a node.

- replace 
對一個現有節點進行近於原子操作的刪除/添加。如果節點不存在,這個命令就會建立節點。
Performs a nearly atomic remove/add on an existing node id. If the node does not exist yet it is created.

- link 
只適用於 DQisc,替代一個現有的節點。
Only available for qdiscs and performs a replace where the node must exist already.

命令格式如下: 
tc qdisc [ add | change | replace | link ] dev DEV [ parent qdisc-id | root ] [ handle qdisc-id ] qdisc [ qdisc specific parameters ]
tc class [ add | change | replace ] dev DEV parent qdisc-id [ classid class-id ] qdisc [ qdisc specific parameters ]
tc filter [ add | change | replace ] dev DEV [ parent qdisc-id | root ] protocol protocol prio priority filtertype [ filtertype specific parameters ] flowid flow-id
tc [-s | -d ] qdisc show [ dev DEV ]
tc [-s | -d ] class show dev DEV tc filter show dev DEV

相關參數說明如下: 
-s, -stats, -statistics: output more statistics about packet usage.
-d, -details: output more detailed information about rates and cell sizes.
-r, -raw: output raw hex values for handles.
-p, -pretty: decode filter offset and mask values to equivalent filter commands based on TCP/IP.
-iec: print rates in IEC units (ie. 1K = 1024).

具體操作 
Linux 流量控制主要分爲建立隊列、建立分類和建立過濾器三個方面。基本實現步驟爲: 
(1) 針對網絡物理設備(如以太網卡eth0)綁定一個隊列QDisc;
(2) 在該隊列上建立分類class;
(3) 爲每一分類建立一個基於路由的過濾器filter;
(4) 最後與過濾器相配合,建立特定的路由表。

環境模擬實例 
流量控制器上的以太網卡 (eth1) 的 IP 地址爲 10.5.0.6,在其上建立一個 CBQ (Class Based Queue) 隊列。假設包的平均大小爲 1000 bytes,包間隔發送單元的大小爲 8 byte,可接收衝突的發送最長包數目爲 20 byte。假如有三種類型的流量需要控制: 
1) 是發往主機1的,其IP地址爲 10.5.0.7。其流量帶寬控制在 8 Mbit,優先級爲2;
2) 是發往主機2的,其IP地址爲 10.5.0.8。其流量帶寬控制在 1 Mbit,優先級爲1;
3) 是發往子網1的,其子網號爲 10.5.1.0,子網掩碼爲 255.255.255.0。流量帶寬控制在 1 Mbit,優先級爲6。

1. 建立隊列 
一般情況下,針對一個網卡只需建立一個隊列。底下命令將一個 cbq 佇列綁定到網絡物理設備 eth1上,其編號爲1:0;網絡物理設備 eth1 的實際帶寬爲10 Mbit,包的平均大小爲 1000 byte;包間隔發送單元的大小爲 8 byte,最小傳輸包大小爲 64 byte。 
$ tc qdisc add dev eth1 root handle 1: cbq bandwidth 10Mbit avpkt 1000 cell 8 mpu 64
$ tc qdisc show dev eth1
qdisc cbq 1: root refcnt 2 rate 10000Kbit (bounded,isolated) prio no-transmit

2. 建立分類 
分類建立在隊列之上。一般情況下,針對一個隊列需建立一個根分類,然後再在其上建立子分類。對於分類,按其分類的編號順序起作用,編號小的優先;一旦符合某個分類匹配規則,通過該分類發送數據包,則其後的分類不再起作用。 

1) 創建根分類1:1;分配帶寬爲 10Mbit,優先級別爲 8。 
$ tc class add dev eth1 parent 1:0 classid 1:1 cbq bandwidth 10Mbit rate 10Mbit maxburst 20 allot 1514 prio 8 avpkt 1000 cell 8 weight 1Mbit
# 該隊列的最大可用帶寬爲10Mbit,實際分配的帶寬爲10Mbit,可接收衝突的發送最長包數目爲20字節;最大傳輸單元加MAC頭的大小爲1514字節,
# 優先級別爲8,包的平均大小爲1000字節,包間隔發送單元的大小爲8字節,相應於實際帶寬的加權速率爲1Mbit。

2)創建分類1:2,其父分類爲1:1,分配帶寬爲 8Mbit,優先級別爲 2。 
$ tc class add dev eth1 parent 1:1 classid 1:2 cbq bandwidth 10Mbit rate 8Mbit maxburst 20 allot 1514 prio 2 avpkt 1000 cell 8 weight 800Kbit split 1:0 bounded
# 該隊列的最大可用帶寬爲10Mbit,實際分配的帶寬爲 8Mbit,可接收衝突的發送最長包數目爲20字節;最大傳輸單元加MAC頭的大小爲1514字節,優先級別爲 2,
# 包的平均大小爲1000字節,包間隔發送單元的大小爲8字節,相應於實際帶寬的加權速率爲800Kbit,分類的分離點爲1:0,且不可借用未使用帶寬。

$ tc -d class show dev eth1
...
class cbq 1:2 parent 1:1 rate 8000Kbit cell 8b (bounded) prio 2/2 weight 819200bit allot 1514b
level 0 ewma 5 avpkt 1000b maxidle 177us
split 1:
 

3)創建分類1:3,其父分類爲1:1,分配帶寬爲1Mbit,優先級別爲1。 
$ tc class add dev eth1 parent 1:1 classid 1:3 cbq bandwidth 10Mbit rate 1Mbit maxburst 20 allot 1514 prio 1 avpkt 1000 cell 8 weight 100Kbit split 1:0
# 該隊列的最大可用帶寬爲10Mbit,實際分配的帶寬爲 1Mbit,可接收衝突的發送最長包數目爲20字節;最大傳輸單元加MAC頭的大小爲1514字節,優先級別爲1,
# 包的平均大小爲1000字節,包間隔發送單元的大小爲8字節,相應於實際帶寬的加權速率爲100Kbit,分類的分離點爲1:0。

$ tc -d class show dev eth1
...
class cbq 1:3 parent 1:1 rate 1000Kbit cell 8b prio 1/1 weight 102400bit allot 1514b
level 0 ewma 5 avpkt 1000b maxidle 6.4ms
split 1:

4)創建分類1:4,其父分類爲1:1,分配帶寬爲1Mbit,優先級別爲6。 
$ tc class add dev eth1 parent 1:1 classid 1:4 cbq bandwidth 10Mbit rate 1Mbit maxburst 20 allot 1514 prio 6 avpkt 1000 cell 8 weight 100Kbit split 1:0
# 該隊列的最大可用帶寬爲10Mbit,實際分配的帶寬爲1Mbit,可接收衝突的發送最長包數目爲20字節;最大傳輸單元加MAC頭的大小爲1514字節,優先級別爲6,
# 包的平均大小爲1000字節,包間隔發送單元的大小爲8字節,相應於實際帶寬的加權速率爲100Kbit,分類的分離點爲1:0。

$ tc -d class show dev eth1
...
class cbq 1:4 parent 1:1 rate 1000Kbit cell 8b prio 6/6 weight 102400bit allot 1514b
level 0 ewma 5 avpkt 1000b maxidle 6.4ms
split 1:

3. 建立過濾器 
過濾器主要服務於分類。一般只需針對 root 分類提供一個過濾器,然後爲每個子分類提供路由映射。 
1) 應用路由分類器到 cbq 佇列的 root,父分類編號爲 1:0;過濾協議爲 ip,優先級別爲100,過濾器爲基於路由表。 
$ tc filter add dev eth1 parent 1:0 protocol ip prio 100 route
$ tc -d filter show dev eth1
filter parent 1: protocol ip pref 100 route

2) 建立路由映射分類1:2, 1:3, 1:4 
$ tc filter add dev eth1 parent 1:0 protocol ip prio 100 route to 2 flowid 1:2
$ tc filter add dev eth1 parent 1:0 protocol ip prio 100 route to 3 flowid 1:3
$ tc filter add dev eth1 parent 1:0 protocol ip prio 100 route to 4 flowid 1:4
$ tc -d filter show dev eth1
filter parent 1: protocol ip pref 100 route
filter parent 1: protocol ip pref 100 route fh 0xffff0002 flowid 1:2 to 2
filter parent 1: protocol ip pref 100 route fh 0xffff0003 flowid 1:3 to 3
filter parent 1: protocol ip pref 100 route fh 0xffff0004 flowid 1:4 to 4

4. 建立路由 
該路由是與前面所建立的路由映射一一對應。 
1) 發往主機 10.5.0.7 的數據包通過分類2轉發 (分類2的速率8Mbit
$ ip route add 10.5.0.7 dev eth1 via 10.5.0.6 realm 2 # 10.5.0.6 為 eth1 上綁定的 IP

2) 發往主機 10.5.0.8 的數據包通過分類3轉發 (分類3的速率1Mbit
$ ip route add 10.5.0.8 dev eth1 via 10.5.0.6 realm 3

3)發往子網 10.5.1.0/24 的數據包通過分類4轉發 (分類4的速率1Mbit
$ ip route add 10.5.1.0/24 dev eth1 via 10.5.0.6 realm 4
$ route -v
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.5.0.8 10.5.0.6 255.255.255.255 UGH 0 0 0 eth1
10.5.0.7 10.5.0.6 255.255.255.255 UGH 0 0 0 eth1

192.168.192.0 * 255.255.255.0 U 1 0 0 eth0
10.5.1.0 10.5.0.6 255.255.255.0 UG 0 0 0 eth1
10.5.0.0 * 255.255.255.0 U 0 0 0 eth1
...

注: 一般對於流量控制器所直接連接的網段建議使用IP主機地址流量控制限制,不要使用子網流量控制限制。如一定需要對直連子網使用子網流量控制限制,則在建立該子網的路由映射前,需將原先由系統建立的路由刪除,才可完成相應步驟。 

5. 監視 
主要包括對現有佇列、分類、過濾器和路由的狀況進行監視。 

1)顯示隊列的狀況 
# 簡單顯示指定設備 (這裏爲 eth1) 的隊列狀況
$ tc qdisc ls dev eth1
qdisc cbq 1: root refcnt 2 rate 10000Kbit (bounded,isolated) prio no-transmit

# 詳細顯示指定設備的佇列狀況
$ tc -s qdisc ls dev eth1
qdisc cbq 1: root refcnt 2 rate 10000Kbit (bounded,isolated) prio no-transmit
Sent 296525 bytes 3993 pkt (dropped 0, overlimits 0 requeues 0)
rate 0bit 0pps backlog 0b 0p requeues 0
borrowed 0 overactions 0 avgidle 12500 undertime 0

2)顯示分類的狀況 
# 簡單顯示指定設備 (這裏爲eth1) 的分類狀況
$ tc class ls dev eth1
class cbq 1: root rate 10000Kbit (bounded,isolated) prio no-transmit
class cbq 1:1 parent 1: rate 10000Kbit prio no-transmit
class cbq 1:2 parent 1:1 rate 8000Kbit (bounded) prio 2
class cbq 1:3 parent 1:1 rate 1000Kbit prio 1
class cbq 1:4 parent 1:1 rate 1000Kbit prio 6


# 詳細顯示指定設備的分類狀況
$ tc -s class ls dev eth1
class cbq 1: root rate 10000Kbit (bounded,isolated) prio no-transmit
Sent 319674 bytes 4305 pkt (dropped 0, overlimits 0 requeues 0)
rate 0bit 0pps backlog 0b 0p requeues 0
borrowed 0 overactions 0 avgidle 12500 undertime 0
class cbq 1:1 parent 1: rate 10000Kbit prio no-transmit
Sent 25941 bytes 340 pkt (dropped 0, overlimits 0 requeues 0)
rate 0bit 0pps backlog 0b 0p requeues 0
...

3) 顯示過濾器的狀況 
$ tc -s filter ls dev eth1
filter parent 1: protocol ip pref 100 route
filter parent 1: protocol ip pref 100 route fh 0xffff0002 flowid 1:2 to 2
filter parent 1: protocol ip pref 100 route fh 0xffff0003 flowid 1:3 to 3
filter parent 1: protocol ip pref 100 route fh 0xffff0004 flowid 1:4 to 4

# 這裏 flowid 1:2 代表分類 class cbq 1:2,to 2 代表通過路由2發送。

4) 顯示現有路由的狀況 
$ ip route
10.5.0.8 via 10.5.0.6 dev eth1 realm 3
10.5.0.7 via 10.5.0.6 dev eth1 realm 2
192.168.192.0/24 dev eth0 proto kernel scope link src 192.168.192.134 metric 1
10.5.1.0/24 via 10.5.0.6 dev eth1 realm 4
10.5.0.0/24 dev eth1 proto kernel scope link src 10.5.0.6
...
default via 192.168.192.2 dev eth0 proto static

如上所示,結尾包含有 realm 的顯示行是起作用的路由過濾器。 

6. 維護 
主要包括對隊列、分類、過濾器和路由的增添、修改和刪除。增添動作一般依照"隊列->分類->過濾器->路由"的順序進行;修改動作則沒有什麼要求;刪除則依照"路由->過濾器->分類->隊列"的順序進行。 

1)隊列的維護 
一般對於一臺流量控制器來說,出廠時針對每個以太網卡均已配置好一個隊列了,通常情況下對隊列無需進行增添、修改和刪除動作了。 

2)分類的維護 
增添: 增添動作通過 tc class add 命令實現,如前面所示。 

修改: 修改動作通過 tc class change 命令實現,如下所示: 
$ tc class change dev eth1 parent 1:1 classid 1:2 cbq bandwidth 10Mbit rate 7Mbit maxburst 20 allot 1514 prio 2 avpkt 1000 cell 8 weight 700Kbit split 1:0 bounded

對於bounded命令應慎用,一旦添加後就進行修改,只可通過刪除後再添加來實現。 

刪除: 刪除動作只在該分類沒有工作前纔可進行,一旦通過該分類發送過數據,則無法刪除它了。因此,需要通過shell文件方式來修改,通過重新啓動來完成刪除動作。 

3)過濾器的維護 
增添: 增添動作通過 tc filter add 命令實現,如前面所示。 

修改: 修改動作通過 tc filter change 命令實現,如下所示: 
$ tc filter change dev eth0 parent 1:0 protocol ip prio 100 route to 10 flowid 1:8

刪除: 刪除動作通過 tc filter del 命令實現,如下所示: 
$ tc filter del dev eth0 parent 1:0 protocol ip prio 100 route to 10

4)與過濾器一一映射路由的維護 
增添: 增添動作通過 ip route add 命令實現,如前面所示。 

修改: 修改動作通過ip route change命令實現,如下所示: 
$ ip route change 192.168.1.30 dev eth1 via 192.168.1.66 realm 8

刪除: 刪除動作通過 ip route del 命令實現,如下所示: 
$ ip route del 192.168.1.30 dev eth0 via 192.168.1.66 realm 8
$ ip route del 192.168.1.0/24 dev eth0 via 192.168.1.66 realm 4

Supplement 
用 CBQ 進行頻寬管理 (Using Class Based Queueing for bandwidth management) 
流量控管的兩個基本單元是﹕過濾器(filters) 和佇列(queues)。前者將流量安排至後者﹐後者則收集流量然後決定哪些要先送﹑哪些要晚送﹑或是丟棄掉(drop)。兩者都各有其不同系列。 最常見的過濾器是 fwmark 和 u32﹐前者讓您可以使用 Linux netfilter 程式選擇流量﹔而後者則允許您依據任何標頭(header)選擇流量。至於最常聽到的佇列演算法就是 Class Based Queue 了。CBQ 可以說是一個 super-queue﹐在其裡面包含了其它佇列 (甚至其它 CBQs)。

18 commands to monitor network bandwidth on Linux server

2015年1月3日 星期六

[ RubyAlg ] MIT Linear Algebra, Spring 2005 - Lec12

Source From Here 
 

Graph & Network 

- Incidence Matrix 
考慮我們有以下 Graph, 則可以使用右邊 Matrix 代表: 
 

由上可以發現 Edge1, Edge2, Edge3 形成一個 Loop, 而這些 Edges 對應的 Row 形成的 Vector Space 不是 independent! 也就是說如果不形成 Loop 的 Edge 則其形成的 Vector Space 會是 Independent, 反之如果 Vector Space 不是 Independent, 則其中必有 Loop

上面的 Matrix A 的 Null Space 解會是 Ax=0, 而 matrix x 稱為 Potential nodes; 而要解的方程式成為 Potential differences (Ohm's raw): 
 

可以看出只有一組 x 解為 c*[1,1,1,1], 也就是說 dim(N(A)) = 1 (n-r), 也就是說 Rank=4-1=3

接著考慮 N(A^T), 也就是解 A^T*y = 0 (Kirchoff's circuit raw): 
 

上式的的 y1+y2+y3=0 說明由 N1 流出的電流需要等於 0; y1-y2=0 說明流進 N2 等於流出 N2 的電流 etc. 此時可以推出 dim(N(A^T)) = m-r (Row space 與 Column space 的 Rank 相等, 即 r=2) -> 5-2 = 3. 而其中的一組 Basis 為 (考慮 Graph 中兩個 roop 的解): 
 

而經過推導可以知道 dim(N(A^T)) 就是 Graph 中 Loop 的數目. 且可以由下面公式決定: 
#Node - #Edge + #Loop = 1 
 

最後可以推導出解電路方程式 (下面的 f 為外部 source, 如電源 etc): 

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...