Linux下用tc控制网络延时和丢包率

 

tc修改网络延时:  

sudo tc qdisc add dev eth0 root netem delay 1000ms


删除策略:

sudo tc qdisc del dev eth0 root netem delay 1000ms


 

验证效果:

PING myhost (192.168.0.2) 56(84) bytes of data.
64 bytes from myhost (192.168.0.2): icmp_seq=1 ttl=64 time=1000 ms

64 bytes from myhost (192.168.0.2): icmp_seq=1 ttl=64 time=1000 ms

64 bytes from myhost (192.168.0.2): icmp_seq=1 ttl=64 time=1000 ms


修改丢包率:
sudo tc qdisc add dev eth0 root netem loss 10%

删除策略:
sudo tc qdisc del dev eth0 root netem loss 10%

=====================================================================

  • 查看流量管理   tc     qdisc    show
  • 配置网络延时:
  • # tc qdisc del dev eth0 root netem loss 10%
    [root@AutoTest ~]# tc qdisc show
    qdisc pfifo_fast 0: dev eth0 root refcnt 2 bands 3 priomap  1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
    [root@AutoTest ~]# tc qdisc add dev eth0 root netem delay 1000ms 
    [root@AutoTest ~]# ping 192.168.0.1
    PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.
    64 bytes from 192.168.0.1: icmp_seq=1 ttl=64 time=2001 ms
    64 bytes from 192.168.0.1: icmp_seq=2 ttl=64 time=1001 ms
    ^C
    --- 192.168.0.1 ping statistics ---
    6 packets transmitted, 5 received, 16% packet loss, time 5846ms
    rtt min/avg/max/mdev = 1000.520/1200.819/2001.001/400.092 ms, pipe 3
    [root@AutoTest ~]# tc qdisc del dev eth0 root netem delay 1000ms 
    [root@AutoTest ~]# ping 192.168.0.1
    PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.
    64 bytes from 192.168.0.1: icmp_seq=1 ttl=64 time=0.465 ms
    64 bytes from 192.168.0.1: icmp_seq=2 ttl=64 time=0.546 ms
    ^C
    --- 192.168.0.1 ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 1841ms
    rtt min/avg/max/mdev = 0.465/0.505/0.546/0.046 ms
    [root@AutoTest ~]# 
    • 配置网络丢包率
    • # tc qdisc del dev eth0 root netem loss 10%
      RTNETLINK answers: Invalid argument
      [root@AutoTest ~]# tc qdisc add dev eth0 root netem loss 10%
      [root@AutoTest ~]# tc qdisc show
      qdisc netem 8005: dev eth0 root refcnt 2 limit 1000 loss 10%
      [root@AutoTest ~]# ping 192.168.0.1 -n 20
      PING 20 (0.0.0.20) 56(124) bytes of data.
      ^C
      --- 20 ping statistics ---
      7 packets transmitted, 0 received, 100% packet loss, time 6371ms
      
      [root@AutoTest ~]# ping 192.168.0.1 -c 20
      PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.
      64 bytes from 192.168.0.1: icmp_seq=1 ttl=64 time=0.533 ms
      64 bytes from 192.168.0.1: icmp_seq=2 ttl=64 time=0.506 ms
      64 bytes from 192.168.0.1: icmp_seq=3 ttl=64 time=0.471 ms
      64 bytes from 192.168.0.1: icmp_seq=5 ttl=64 time=0.550 ms
      64 bytes from 192.168.0.1: icmp_seq=7 ttl=64 time=0.474 ms
      64 bytes from 192.168.0.1: icmp_seq=8 ttl=64 time=0.499 ms
      64 bytes from 192.168.0.1: icmp_seq=10 ttl=64 time=0.541 ms
      64 bytes from 192.168.0.1: icmp_seq=11 ttl=64 time=0.470 ms
      64 bytes from 192.168.0.1: icmp_seq=12 ttl=64 time=0.531 ms
      64 bytes from 192.168.0.1: icmp_seq=13 ttl=64 time=0.476 ms
      64 bytes from 192.168.0.1: icmp_seq=14 ttl=64 time=0.503 ms
      64 bytes from 192.168.0.1: icmp_seq=15 ttl=64 time=0.508 ms
      64 bytes from 192.168.0.1: icmp_seq=16 ttl=64 time=0.541 ms
      64 bytes from 192.168.0.1: icmp_seq=19 ttl=64 time=0.531 ms
      64 bytes from 192.168.0.1: icmp_seq=20 ttl=64 time=0.536 ms
      
      --- 192.168.0.1 ping statistics ---
      20 packets transmitted, 15 received, 25% packet loss, time 19999ms
      rtt min/avg/max/mdev = 0.470/0.511/0.550/0.033 ms
      
      # tc qdisc del dev eth0 root netem loss 10%
      [root@AutoTest ~]# ping 192.168.0.1 -c 20
      PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.
      64 bytes from 192.168.0.1: icmp_seq=1 ttl=64 time=0.827 ms
      64 bytes from 192.168.0.1: icmp_seq=2 ttl=64 time=0.508 ms
      64 bytes from 192.168.0.1: icmp_seq=3 ttl=64 time=0.506 ms
      64 bytes from 192.168.0.1: icmp_seq=4 ttl=64 time=0.574 ms
      64 bytes from 192.168.0.1: icmp_seq=5 ttl=64 time=0.555 ms
      64 bytes from 192.168.0.1: icmp_seq=6 ttl=64 time=0.495 ms
      64 bytes from 192.168.0.1: icmp_seq=7 ttl=64 time=0.517 ms
      64 bytes from 192.168.0.1: icmp_seq=8 ttl=64 time=0.529 ms
      64 bytes from 192.168.0.1: icmp_seq=9 ttl=64 time=0.515 ms
      64 bytes from 192.168.0.1: icmp_seq=10 ttl=64 time=0.571 ms
      64 bytes from 192.168.0.1: icmp_seq=11 ttl=64 time=0.508 ms
      64 bytes from 192.168.0.1: icmp_seq=12 ttl=64 time=0.533 ms
      64 bytes from 192.168.0.1: icmp_seq=13 ttl=64 time=0.526 ms
      64 bytes from 192.168.0.1: icmp_seq=14 ttl=64 time=0.520 ms
      64 bytes from 192.168.0.1: icmp_seq=15 ttl=64 time=0.532 ms
      64 bytes from 192.168.0.1: icmp_seq=16 ttl=64 time=0.549 ms
      64 bytes from 192.168.0.1: icmp_seq=17 ttl=64 time=0.527 ms
      64 bytes from 192.168.0.1: icmp_seq=18 ttl=64 time=0.575 ms
      64 bytes from 192.168.0.1: icmp_seq=19 ttl=64 time=0.534 ms
      64 bytes from 192.168.0.1: icmp_seq=20 ttl=64 time=0.556 ms
      
      --- 192.168.0.1 ping statistics ---
      20 packets transmitted, 20 received, 0% packet loss, time 19000ms
      rtt min/avg/max/mdev = 0.495/0.547/0.827/0.074 ms
      [root@AutoTest ~]# 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值