使用 linux 下的 tc 流量控制测试-凯发app官方网站

凯发app官方网站-凯发k8官网下载客户端中心 | | 凯发app官方网站-凯发k8官网下载客户端中心
  • 博客访问: 6315102
  • 博文数量: 162
  • 博客积分: 3600
  • 博客等级: 中校
  • 技术积分: 10366
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-19 11:23
个人简介

专注系统运维、网络架构,研究技术凯发app官方网站的解决方案,记录我的思想轨迹、工作学习、生活和关注的领域

文章分类
文章存档

2014年(2)

2013年(16)

2012年(27)

2011年(117)

相关博文
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·

分类: 系统运维

2013-08-08 18:18:59

需要对网关做流量控制,针对ip和网段做控制,也有结合iptables实现方式,可能也有针对内外网的服务器,规则明白了,都很容易。
可以查看这篇参考文章,讲的很详细,下面是我改写的shell脚本,非iptables方式:


  1. #!/bin/bash
  2. # set the following values to somewhat less than your actual download
  3. # and uplink speed. in kilobits. also set the device that is to be shaped.
  4. #ingoing traffic (gateway)
  5. in=eth0
  6. #what ip do you want to limit
  7. inet="192.168.138."
  8. ips="100"
  9. ipe="254"
  10. #total downlink
  11. down="100mbit"
  12. #ensure rate speed of downlink
  13. downloadrate="100kbit"
  14. #allow max rate speed of downlink
  15. downloadceil="250kbit"
  16. start(){
  17.     #clean eth1 eth0 existing down- and uplink qdiscs, hide errors
  18.     /sbin/tc qdisc del dev $in root 2>/dev/null
  19.     # install root htb of downlink and uplink
  20.     # main class
  21.     /sbin/tc qdisc add dev $in root handle 1: htb
  22.     /sbin/tc class add dev $in parent 1: classid 1:1 htb rate $down ceil $down
  23.     #simgle ip limit
  24.     /sbin/tc class add dev $in parent 1:1 classid 1:2 htb rate $downloadrate ceil $downloadrate
  25.     /sbin/tc qdisc add dev $in parent 1:2 sfq perturb 2
  26.     /sbin/tc filter add dev $in protocol ip parent 1: prio 49 u32 match ip dst 192.168.138.10 flowid 1:2
  27.     /sbin/tc filter add dev $in protocol ip parent 1: prio 49 u32 match ip dst 192.168.2.0/32 flowid 1:2
  28.     #net1 limit
  29.     for (( i=$ips; i<=$ipe; i=i1 ))
  30.     do
  31.         #####control downlink
  32.         /sbin/tc class add dev $in parent 1:1 classid 1:1$i htb rate $downloadrate ceil $downloadceil
  33.         /sbin/tc qdisc add dev $in parent 1:1$i sfq perturb 1$i
  34.         /sbin/tc filter add dev $in protocol ip parent 1: prio 50 u32 match ip dst $inet$i flowid 1:1$i
  35.     done
  36.     #net2 limit
  37.     #for (( i=$ips; i<=$ipe; i=i1 ))
  38.     #do
  39.     # #####control downlink
  40.     # /sbin/tc class add dev $in parent 1:1 classid 1:2$i htb rate $downloadrate ceil $downloadceil
  41.     # /sbin/tc qdisc add dev $in parent 1:2$i sfq perturb 2$i
  42.     # /sbin/tc filter add dev $in protocol ip parent 1: prio 50 u32 match ip dst $inet$i flowid 1:2$i
  43.     #done
  44.     #other traffic
  45.     /sbin/tc filter add dev $in protocol ip parent 1: prio 2 u32 match ip dst 0.0.0.0/32 flowid 1:1
  46. }
  47. stop(){
  48.     echo -n "(delete all qdisc......)"
  49.     (/sbin/tc qdisc del dev $in root 2>/dev/null && echo "ok.delete sucessfully!") || echo "error."
  50. }
  51. #show status
  52. status() {
  53.     echo "1.show qdisc $in:----------------------------------------------"
  54.     /sbin/tc -s qdisc show dev $in
  55.     echo "2.show class $in:----------------------------------------------"
  56.     n1=`/sbin/tc class show dev $in | wc -l`
  57.     if [ $n1 == 0 ];then
  58.         echo "null, off limiting "
  59.     else
  60.         /sbin/tc -s class show dev $in
  61.         echo "it work"
  62.     fi
  63. }
  64. #show help
  65. usage() {
  66.     echo "(usage): `basename $0` [start | stop | restart | status ]"
  67.     echo "help:"
  68.     echo "start -- tc flow control start"
  69.     echo "stop -- tc flow control stop"
  70.     echo "restart -- tc flow control restart"
  71.     echo "status -- tc show all tc qdisc and class"
  72. }
  73. case "$1" in
  74.     start)
  75.         ( start && echo "flow control! tc started!" ) || echo "error."
  76.         exit 0
  77.         ;;

  78.     stop)
  79.         ( stop && echo "flow control tc stopped!" ) || echo "error."
  80.         exit 0
  81.         ;;
  82.     restart)
  83.         stop
  84.         start
  85.         echo "flow control restart"
  86.         ;;
  87.     status)
  88.         status
  89.         ;;

  90.     *) usage
  91.         exit 1
  92.         ;;
  93. esac

测试:
开启流量控制tc:

  1. # ./tc_last.sh start
有限制的ip:192.168.138.131
  1. # ifconfig eth0
  2. eth0 link encap:ethernet hwaddr 00:0c:29:da:56:1c
  3.           inet addr:192.168.138.131 bcast:192.168.138.255 mask:255.255.255.0
  4.           up broadcast running multicast mtu:1500 metric:1
  5.           rx packets:1811422 errors:0 dropped:0 overruns:0 frame:0
  6.           tx packets:1083449 errors:0 dropped:0 overruns:0 carrier:0
  7.           collisions:0 txqueuelen:1000
  8.           rx bytes:943250761 (899.5 mib) tx bytes:87045802 (83.0 mib)

  9. # wget http://ftp13.enet.com.cn:88/pub/multimedia/video/uvs9_trial_e_rtm.rar
  10. --2013-08-08 17:44:01-- http://ftp13.enet.com.cn:88/pub/multimedia/video/uvs9_trial_e_rtm.rar
  11. resolving ftp13.enet.com.cn... 122.224.6.16
  12. connecting to ftp13.enet.com.cn|122.224.6.16|:88... connected.
  13. http request sent, awaiting response... 200 ok
  14. length: 180175741 (172m) [application/octet-stream]
  15. saving to: `uvs9_trial_e_rtm.rar.17

无限制ip:192.168.138.99


  1. # ifconfig eth0
  2. eth0 link encap:ethernet hwaddr 00:0c:29:88:ec:85
  3.           inet addr:192.168.138.99 bcast:192.168.138.255 mask:255.255.255.0
  4.           up broadcast running multicast mtu:1500 metric:1
  5.           rx packets:88578 errors:0 dropped:0 overruns:0 frame:0
  6.           tx packets:43771 errors:0 dropped:0 overruns:0 carrier:0
  7.           collisions:0 txqueuelen:1000
  8.           rx bytes:19401052 (18.5 mib) tx bytes:6876868 (6.5 mib)

  9. # wget http://ftp13.enet.com.cn:88/pub/multimedia/video/uvs9_trial_e_rtm.rar
  10. --2013-07-16 22:57:10-- http://ftp13.enet.com.cn:88/pub/multimedia/video/uvs9_trial_e_rtm.rar
  11. resolving ftp13.enet.com.cn... 122.224.6.16
  12. connecting to ftp13.enet.com.cn|122.224.6.16|:88... connected.
  13. http request sent, awaiting response... 200 ok
  14. length: 180175741 (172m) [application/octet-stream]
  15. saving to: `uvs9_trial_e_rtm.rar.2

效果还是很显著的,就是和设置的流量范围有偏差,有待继续研究!


阅读(27800) | 评论(3) | 转发(6) |
给主人留下些什么吧!~~

hjjie20062015-02-11 13:52:50

没看出来 有限制和没限制 区别啊  好歹有个流量 速率对比啊~~

|

2014-09-01 22:57:18

不能再多感谢!

|

2014-07-28 17:09:03

学习了

|
")); function link(t){ var href= $(t).attr('href'); href ="?url=" encodeuricomponent(location.href); $(t).attr('href',href); //setcookie("returnouturl", location.href, 60, "/"); }
网站地图