Skip to content

Instantly share code, notes, and snippets.

@guddl
Created June 30, 2016 08:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guddl/edc113cb08219f8b6a5f658d2781f822 to your computer and use it in GitHub Desktop.
Save guddl/edc113cb08219f8b6a5f658d2781f822 to your computer and use it in GitHub Desktop.
Simple traffic shaper script for Linux
#! /bin/bash
NETCARD=eth0
MAXBANDWIDTH=100000
# reinit
tc qdisc del dev $NETCARD root handle 1
tc qdisc add dev $NETCARD root handle 1: htb default 9999
# create the default class
tc class add dev $NETCARD parent 1:0 classid 1:9999 htb rate $(( $MAXBANDWIDTH ))kbit ceil $(( $MAXBANDWIDTH ))kbit burst 5k prio 9999
# control bandwidth per IP
declare -A ipctrl
# define list of IP and bandwidth (in kilo bits per seconds) below
ipctrl[172.17.10.46]="256"
ipctrl[172.17.10.47]="256"
ipctrl[172.17.10.48]="256"
mark=0
for ip in "${!ipctrl[@]}"
do
mark=$(( mark + 1 ))
bandwidth=${ipctrl[$ip]}
# traffic shaping rule
tc class add dev $NETCARD parent 1:0 classid 1:$mark htb rate $(( $bandwidth ))kbit ceil $(( $bandwidth ))kbit burst 5k prio $mark
# netfilter packet marking rule
iptables -t mangle -A INPUT -i $NETCARD -s $ip -j CONNMARK --set-mark $mark
# filter that bind the two
tc filter add dev $NETCARD parent 1:0 protocol ip prio $mark handle $mark fw flowid 1:$mark
echo "IP $ip is attached to mark $mark and limited to $bandwidth kbps"
done
#propagate netfilter marks on connections
iptables -t mangle -A POSTROUTING -j CONNMARK --restore-mark
@C0deWithAj
Copy link

I m getting Invalid Argument after running the script , I just replace with my ip ipctrl[10.24.24.1]="256":
Following is the output:
RTNETLINK answers: Invalid argument
IP 10.24.24.1 is attached to mark 1 and limited to 256 kbps

Is there something wrong? Also, i read somewhere that individual ip tc rules work only when the client is already connected and then you run the script for shaping bandwidth? Is that right ?

I am using strongswan ikev2 as server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment