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 ~]# 

你可能感兴趣的:(Linux下用tc控制网络延时和丢包率)