# traffic shaping

## neuron

are there any scripts / programs that can help me?  I'v been using wondershaper for some time, and it works.  But I wanna set up something like this:

```

     Main upstream

           |

           |

         25kbyte/sec

           |

           |

          /   \

         /      \

        /         \

       /            \

norm prio         21kbyte/sec

                       |

                       |

                       |

            All non classified traffic

                       |

                       |

                       |

            Low priority (ie, it gets resources if all non classified isn't using anything)

```

/me should sit down and mod a script, but bah, I'm so tired of using time on this.  So I'd rather ask in hope that somene with l33t tc skills can cook up something fast  :Wink: 

Also, how does traffic shaping work with masquerade?  Becouse I have many hosts on this network, and it'd be great if I could get some examples on that too.  Though generaly it'd probably be fine if they ran into the normal traffic control system.

----------

## 69link

This is my script. It is based mostly on wondershaper, but I included alot of my own tricks aswell.

You edit the DOWNLOAD and UPLINK stuff in the beginning. Also if you want to add more iptables rules you mark them as 20 (fast) 21 (medium) and 22 (slow, bulk).

Maybe its something to start with?

```

#!/bin/bash

# The Ultimate Setup For Your Internet Connection At Home

#

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

# and uplink speed. In kilobits

DOWNLINK=2450

UPLINK=710

UPLINK_FAST=510

UPLINK_BULK=100

UPLINK_MED=100

DEV=eth0

INT_DEV=eth1

IPTABLES=/sbin/iptables

TC=/usr/sbin/tc

echo ${UPLINK}

# Kill tc settings

$TC qdisc del dev $DEV root

$IPTABLES -t mangle -F

$IPTABLES -t mangle -X

# Some iptables markings

$IPTABLES -t mangle -N MANGLE_MARK

# Set ACK as prioritized traffic

$IPTABLES -t mangle -A MANGLE_MARK -p tcp -m length --length 0:80 -j MARK --set-mark 20

$IPTABLES -t mangle -A MANGLE_MARK -p tcp -m length --length 0:80 -j RETURN

# Set icmp as prioritized traffic

$IPTABLES -t mangle -A MANGLE_MARK -p icmp -j MARK --set-mark 20

$IPTABLES -t mangle -A MANGLE_MARK -p icmp -j RETURN

# Set udp as prioritized traffic

$IPTABLES -t mangle -A MANGLE_MARK -p udp -j MARK --set-mark 20

$IPTABLES -t mangle -A MANGLE_MARK -p udp -j RETURN

# Set ftp as prioritized traffic

$IPTABLES -t mangle -A MANGLE_MARK -p tcp --sport 21 -j MARK --set-mark 20

$IPTABLES -t mangle -A MANGLE_MARK -p tcp --sport 21 -j RETURN

# Set ssh as prioritized traffic

$IPTABLES -t mangle -A MANGLE_MARK -p tcp --sport ssh -j MARK --set-mark 20

$IPTABLES -t mangle -A MANGLE_MARK -p tcp --sport ssh -j RETURN

# Set WWW as medium prioritized traffic

$IPTABLES -t mangle -A MANGLE_MARK -p tcp --sport www -j MARK --set-mark 21

$IPTABLES -t mangle -A MANGLE_MARK -p tcp --sport www -j RETURN

# Set SMTP as medium prioritized traffic

$IPTABLES -t mangle -A MANGLE_MARK -p tcp --dport smtp -j MARK --set-mark 21

$IPTABLES -t mangle -A MANGLE_MARK -p tcp --dport smtp -j RETURN

# Set everything as bulk traffic

$IPTABLES -t mangle -A MANGLE_MARK -j MARK --set-mark 22

$IPTABLES -t mangle -A MANGLE_MARK -j RETURN

# Jump to tables

#$IPTABLES -t mangle -I OUTPUT -o $DEV -j MANGLE_MARK

$IPTABLES -t mangle -I POSTROUTING -o $DEV -j MANGLE_MARK

$IPTABLES -t mangle -I PREROUTING -i $DEV -j MANGLE_MARK

# clean existing down- and uplink qdiscs, hide errors

$TC qdisc del dev $DEV root    2> /dev/null > /dev/null

$TC qdisc del dev $DEV ingress 2> /dev/null > /dev/null

###### uplink

# install root HTB, point default traffic to 1:20:

$TC qdisc add dev $DEV root handle 1: htb default 22 r2q 2

# shape everything at $UPLINK speed - this prevents huge queues in your

# DSL modem which destroy latency:

$TC class add dev $DEV parent 1: classid 1:1 htb rate ${UPLINK}kbit

$TC class add dev $DEV parent 1:1 classid 1:20 htb rate ${UPLINK_FAST}kbit ceil ${UPLINK}kbit prio 0

$TC class add dev $DEV parent 1:1 classid 1:21 htb rate ${UPLINK_MED}kbit ceil ${UPLINK}kbit prio 5

$TC class add dev $DEV parent 1:1 classid 1:22 htb rate ${UPLINK_BULK}kbit ceil ${UPLINK}kbit prio 6

# both get Stochastic Fairness:

$TC qdisc add dev $DEV parent 1:20 handle 20: sfq perturb 10

$TC qdisc add dev $DEV parent 1:21 handle 21: sfq perturb 10

$TC qdisc add dev $DEV parent 1:22 handle 22: sfq perturb 10

# Match marked packets with tc (20=fast, 22=bulk)

$TC filter add dev $DEV parent 1: protocol ip prio 0 handle 20 fw flowid 1:20

$TC filter add dev $DEV parent 1: protocol ip prio 0 handle 21 fw flowid 1:21

$TC filter add dev $DEV parent 1: protocol ip prio 0 handle 22 fw flowid 1:22

########## downlink #############

# slow downloads down to somewhat less than the real speed  to prevent

# queuing at our ISP. Tune to see how high you can set it.

# ISPs tend to have *huge* queues to make sure big downloads are fast

#

# attach ingress policer:

$TC qdisc add dev $DEV handle ffff: ingress

# filter *everything* to it (0.0.0.0/0), drop everything that's

# coming in too fast:

$TC filter add dev $DEV parent ffff: protocol ip prio 50 u32 \

  match ip protocol 6 0xff \

  match ip src 0.0.0.0/0 \

  police rate ${DOWNLINK}kbit burst 10k drop flowid :1

```

----------

## neuron

defenatly much better to start with than wondershaper, thanks a lot.

----------

## carbon

so all that applies to one machines,right?

what if under the router somewhere i have another machine, someone told me all machines that wants to be benefited by the traffic shaping needs to use the "traffic shaped" machine as a gateway in order to work.

can someone elaborate on that for me a lil bit?   :Rolling Eyes:   :Rolling Eyes: 

----------

## 69link

If you look at the following simple diagram:

```

                eth0       eth1      | <---(clients)--->

[Internet] <---> [Gentoo box] <----> | <---(clients)--->

                                     | <---(clients)--->

```

Then all clients on the right side would be trafficshaped by the gentoobox.

----------

