Test TC traffic control in Linux

Source: Internet
Author: User

You need to control the traffic of the gateway, control the IP address and network segment, and use iptables to implement the traffic. You may also have the server for the Intranet and Internet, and the rules are easy to understand.

The following is a shell script, not in iptables mode:

#! /Bin/bash

# Set the following values to somewhat less than your actual download

# And uplink speed. In kilobits. Also set the device that is to be shaped.

# INGOING traffic (gateway)

IN = eth0

# What ip do you want to limit

INET = "192.168.138 ."

IPS = "100"

Rip = "254"

# Total DOWNLINK

DOWN = "100 mbit"

# Ensure rate speed of DOWNLINK

DOWNLOADrate = "100 kbit"

# Allow max rate speed of DOWNLINK

DOWNLOADceil = "250 kbit"

Start (){

# Clean eth1 eth0 existing down-and uplink qdiscs, hide errors

/Sbin/tc qdisc del dev $ IN root 2>/dev/null

# Install root htb of downlink and uplink

# Main class

/Sbin/tc qdisc add dev $ IN root handle 1: htb

/Sbin/tc class add dev $ IN parent 1: classid 1:1 htb rate $ DOWN ceil $ DOWN

# Simgle ip limit

/Sbin/tc class add dev $ IN parent classid htb rate $ DOWNLOADrate ceil $ DOWNLOADrate

/Sbin/tc qdisc add dev $ IN parent sfq perturb 2

/Sbin/tc filter add dev $ IN protocol ip parent 1: prio 49 u32 match ip dst 192.168.138.10 flowid

/Sbin/tc filter add dev $ IN protocol ip parent 1: prio 49 u32 match ip dst 192.168.2.0/32 flowid

# Net1 limit

For (I = $ IPS; I <= $ IPE; I = I + 1 ))

Do

##### Control DOWNLINK

/Sbin/tc class add dev $ IN parent 1:1 classid 1:1 $ I htb rate $ DOWNLOADrate ceil $ DOWNLOADceil

/Sbin/tc qdisc add dev $ IN parent 1:1 $ I sfq perturb 1 $ I

/Sbin/tc filter add dev $ IN protocol ip parent 1: prio 50 u32 match ip dst $ INET $ I flowid 1:1 $ I

Done

# Net2 limit

# For (I = $ IPS; I <= $ IPE; I = I + 1 ))

# Do

###### Control DOWNLINK

#/Sbin/tc class add dev $ IN parent classid $ I htb rate $ DOWNLOADrate ceil $ DOWNLOADceil

#/Sbin/tc qdisc add dev $ IN parent $ I sfq perturb 2 $ I

#/Sbin/tc filter add dev $ IN protocol ip parent 1: prio 50 u32 match ip dst $ INET $ I flowid $ I

# Done

# Other traffic

/Sbin/tc filter add dev $ IN protocol ip parent 1: prio 2 u32 match ip dst 0.0.0.0/32 flowid 1:1

}

Stop (){

Echo-n "(Delete all qdisc ......)"

(/Sbin/tc qdisc del dev $ IN root 2>/dev/null & echo "OK. Delete sucessfully! ") | Echo" error ."

}

# Show status

Status (){

Echo "1. show qdisc $ IN :----------------------------------------------"

/Sbin/tc-s qdisc show dev $ IN

Echo "2. show class $ IN :----------------------------------------------"

N1 = '/sbin/tc class show dev $ IN | wc-l'

If [$ N1 = 0]; then

Echo "NULL, OFF Limiting"

Else

/Sbin/tc-s class show dev $ IN

Echo "It work"

Fi

}

# Show help

Usage (){

Echo "(usage): 'basename $ 0' [start | stop | restart | status]"

Echo "help :"

Echo "start -- TC Flow Control start"

Echo "stop -- TC Flow Control stop"

Echo "restart -- TC Flow Control restart"

Echo "status -- TC Show all TC Qdisc and class"

}

Case "$1" in

Start)

(Start & echo "Flow Control! TC started! ") | Echo" error ."

Exit 0

;;

 

Stop)

(Stop & echo "Flow Control TC stopped! ") | Echo" error ."

Exit 0

;;

Restart)

Stop

Start

Echo "Flow Control restart"

;;

Status)

Status

;;

 

*) Usage

Exit 1

;;

Esac

  • 1
  • 2
  • Next Page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.