为什么要控制带宽使用?

带宽资源有限,你可能需要针对性的限制某些应用占用的带宽,保证其它服务的正常稳定,那你就需要分配好大家的使用量了。好久以前,我在同步文件的时候就占用带宽很多,这同步也很猛,基本上能尽力跑满,太英勇了。

那其实我们可以做到按远端发送的IP限制带宽使用,或者限制某些端口的带宽占用。

如何控制?

先引述一下TC (man tc):

引用
NAME
       tc - show / manipulate traffic control settings

DESCRIPTION
       Tc is used to configure Traffic Control in the Linux kernel. Traffic Control consists of the following:

       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
              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
              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.

       Processing of traffic is controlled by three kinds of objects: qdiscs, classes and filters.


网上搜索整理了一个脚本:
限制 192.168.1.1/24 网段使用带宽在4M-10M直接

#!/bin/sh

# http://www.aslibra.com/blog/post/tc_bandwidth_control.php
# code by hqlulu

tc qdisc del dev eth0 root handle 1:
tc qdisc add dev eth0 root handle 1: htb r2q 1
tc class add dev eth0 parent 1: classid 1:1 htb rate 4mbit ceil 10mbit
tc filter add dev eth0 parent 1: protocol ip prio 16 u32 match ip dst 192.168.1.1/24 flowid 1:1

# modify dst xxxx


检查的命令:

[root@localhost ~]# tc qdisc show dev eth0
qdisc htb 1: r2q 1 default 0 direct_packets_stat 11591872
[root@localhost ~]# tc class show dev eth0
class htb 1:1 root prio 0 rate 4Mbit ceil 10Mbit burst 2099b cburst 2848b
[root@localhost ~]# tc filter show dev eth0
filter parent 1: protocol ip pref 16 u32
filter parent 1: protocol ip pref 16 u32 fh 800: ht divisor 1
filter parent 1: protocol ip pref 16 u32 fh 800::800 order 2048 key ht 800 bkt 0 flowid 1:1
  match c0a80100/ffffff00 at 16


还谈不上对tc了解很多,以后再整理tc的资料,有需要这样的例子的朋友可以参考一下。
网上资料很多,搜一下就有应用的例子,暂时阿权这里就这个实例了,效果图:

点击在新窗口中浏览此图片

基本在4M-10M之间浮动,已经满足我的需求了,以后把参数都说明一下。


原创内容如转载请注明:来自 阿权的书房
收藏本文到网摘
Tags: ,
发表评论
AD
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML 打开UBB 打开表情 隐藏
昵称   密码   游客无需密码
网址   电邮   [注册]
               

 

阅读推荐

服务器相关推荐

开发相关推荐

应用软件推荐