LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 06-24-2015, 07:37 PM   #1
wastingtime
Member
 
Registered: Sep 2004
Posts: 86

Rep: Reputation: 17
how do I build a bridged network rate limiter


I want to test some equipment under limited bandwidth conditions

I set up a machine with two nics in a bridge configuration such that the equipment under test i connected to eth3 and the network connected to eth1. All traffic from/to eth3 is bridged to the network via eth1, and can be monitored using e.g. wireshark listening to the bridge br0.

I now want to limit the traffic going through eth3. I have read many a tutorial regarding using tc, with specific examples for my configuration.

Here's my updated script.

Code:
#!/bin/bash

UPDEV=eth1
DNDEV=eth3
BRDEV=br0
BRIP=10.0.0.2
BRGW=10.0.0.1
BRBC=10.0.0.255
RATE=1000kbit
MAXRATE=30mbit

echo $UPDEV is the sniffer connection to the network
echo $DNDEV is connected to the client machine to be monitored

echo Load class based queuing module
modprobe sch_cbq

echo Enable forwarding
echo "1" > /proc/sys/net/ipv4/ip_forward
echo Enable redirection of packets with foreign destination ip to local interface
echo "1" > /proc/sys/net/ipv4/conf/eth1/route_localnet 

echo Reset previous setting
###########################

tc qdisc del dev $UPDEV root
tc qdisc del dev $DNDEV root

iptables -D FORWARD -t mangle -m physdev --physdev-in $DNDEV -j MARK --set-mark 5
iptables -D FORWARD -t mangle -m physdev --physdev-out $DNDEV -j MARK --set-mark 5

iptables -D INPUT -i $BRDEV  -j ACCEPT
iptables -D INPUT -i $DNDEV -j ACCEPT
iptables -D INPUT -i $UPDEV -j ACCEPT

route del default gateway $BRGW $BRDEV
ifconfig $BRDEV down

brctl delif $BRDEV $DNDEV
brctl delif $BRDEV $UPDEV
brctl delbr $BRDEV

ifconfig $DNDEV down
ifconfig $UPDEV down

if [ "$1" = "clear" ] ; then 
ifconfig $UPDEV $BRIP netmask 255.255.255.0 broadcast $BRBC up
route add default gateway $BRGW $UPDEV
exit 0
fi

echo Setup bridge
#################

echo Get rid of interface IP addresses
ifconfig $UPDEV 0 0.0.0.0 promisc up
ifconfig $DNDEV 0 0.0.0.0 promisc up

echo Create the bridge
brctl addbr $BRDEV # create bridge device
brctl addif $BRDEV $UPDEV # Add $UPDEV to bridge
brctl addif $BRDEV $DNDEV # Add $DNDEV to bridge

echo Enable bridge with upstream network ip address
ifconfig $BRDEV $BRIP netmask 255.255.255.0 broadcast $BRBC up
route add default gateway $BRGW $BRDEV

echo Ensure firewall allows traffic across bridge
iptables -A INPUT -i $UPDEV -j ACCEPT
iptables -A INPUT -i $DNDEV -j ACCEPT
iptables -A INPUT -i $BRDEV -j ACCEPT

echo Mark $DNDEV packets
iptables -A FORWARD -t mangle -m physdev --physdev-in $DNDEV -j MARK --set-mark 5
iptables -A FORWARD -t mangle -m physdev --physdev-out $DNDEV -j MARK --set-mark 5

echo Setup upstream queues with rate limit
tc qdisc add dev $UPDEV handle 1:0 root htb default 2
tc class add dev $UPDEV parent 1:0 classid 1:1 htb rate ${RATE} ceil ${RATE}
tc class add dev $UPDEV parent 1:0 classid 1:2 htb rate ${MAXRATE} ceil ${MAXRATE}

echo "Limit egress rate; drop everything coming too fast"
tc filter add dev $UPDEV parent 1:0 protocol all prio 50 handle 5 \
 estimator 1sec 8sec fw classid 1:1 police rate ${RATE} burst ${RATE} drop

echo Setup downstream queues with rate limit
tc qdisc add dev $DNDEV handle 2:0 root htb default 2
tc class add dev $DNDEV parent 2:0 classid 2:1 htb rate ${RATE} ceil ${RATE}
tc class add dev $DNDEV parent 2:0 classid 2:2 htb rate ${MAXRATE} ceil ${MAXRATE}

echo "Limit ingress rate; drop everything coming too fast"
tc filter add dev $DNDEV parent 2:0 protocol all prio 50 handle 5 \
 estimator 1sec 8sec fw classid 2:1 police rate ${RATE} burst ${RATE} drop
#end

Last edited by wastingtime; 06-25-2015 at 12:59 AM. Reason: Updated script now works
 
Old 06-24-2015, 10:33 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,149

Rep: Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264
Before you can do class-based queuing you need to load the sch_cbq kernel module.
 
1 members found this post helpful.
Old 06-24-2015, 11:35 PM   #3
wastingtime
Member
 
Registered: Sep 2004
Posts: 86

Original Poster
Rep: Reputation: 17
Thanks! now upstream responds to RATE setting.

Downstream continues to be unaffected.
 
Old 06-25-2015, 12:54 AM   #4
wastingtime
Member
 
Registered: Sep 2004
Posts: 86

Original Poster
Rep: Reputation: 17
I was missing an estimator

Quote:
If you use this method you must provide an estimator, although
nothing will complain if you don't. It just won't police any
packets.
http://linux-tc-notes.sourceforge.net/tc/doc/police.txt

It also turns out that there's no need for special ingress filter.
It is clearer and simpler to define class based policies on
UPDEV and DNDEV that look for the marked packets.

So the updated configuration uses fw matching to place the packets into queues.

I test using scp to copy a file downstream or upstream.
When run separately the downstream is slightly above RATE (1mbit/s).
The upstream start fast and stabilizes slightly below rate.

However, when both run concurrently the upstream copy slows down to 10% of RATE,
while the downstream works as well as before.

I'm not sure what's causing that. Could it be that the upstream tcp ack's (which
travel in opposite direction downstream) are crowded by the downstream copy.

If so how can I control that?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
iptables rate limiting for bridged connection (kvm created bridge) tkmsr Linux - Networking 1 10-28-2010 07:50 AM
Network Limiter? brownjag Linux - Software 2 06-12-2005 07:15 PM
Network download limiter dude_011 Linux - Networking 1 06-29-2004 03:37 AM
Frame Rate LImiter qwacko Linux - Games 2 06-20-2004 05:23 PM
network limiter body00 Linux - Networking 6 05-10-2004 04:53 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 07:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration