Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throttling Bandwidth on Ethernet Port

I'm writing an application to run on a server where I need to be able to set the maximum bandwidth for each Ethernet port (there will be up to 6 ports).

Obviously I can throttle the bandwidth that my application uses but I haven't yet found any information about throttling the actual Ethernet port bandwidth on the computer.

Would this need to be done by creating a driver to monitor all of the ports? Could anyone point me in the right direction?

like image 792
DawnMage77 Avatar asked Nov 12 '22 15:11

DawnMage77


1 Answers

If you have access to a Linux box, its easy-peasy to do such a thing. In fact you can add all sorts of network impairments to make it interesting.

Just install two network cards and set-up netem to intermediate the traffic. (i.e. a netem blip in the wire, adding up impairments like delay, jitter, bandwidth rate etc.)

Here is the script I use to test the performance of my Android audio streaming apps by passing the android traffic through the Linux box (with a wifi AP connected to one of the interfaces).

Added: By testing the performance of my App, I mean how would the App behave on a 4G network while driving (i.e. a lot of Jitter). Or a use case in home with Wi-Fi; what if everyone in the home decides to stream HD videos simultaneously (i.e. bandwidth contention, with a lot of packet loss).

#!/bin/bash

ORIGINAL_PATH=$PATH

#echo $ORIGINAL_PATH

export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/game"

#echo $PATH 
CTOSINTERFACE=eth1  # EGRESS interface on Bridge, facing the SERVER
STOCINTERFACE=eth0  # EGGRES interface on Bridge, facing the CLIENT

# Client To Server
CDELAY=$1   
CJITTER=$2
CLOSS=$3
CDUPLICATE=$4
CCORRUPT=$5
CREORDER=$6

# Server to Client
SDELAY=$7
SJITTER=$8
SLOSS=$9
SDUPLICATE=$10
SCORRUPT=$11
SREORDER=$12

# Clear Latency/Loss/Jitter
/sbin/tc qdisc del root dev $CTOSINTERFACE 2>/dev/null
/sbin/tc qdisc del root dev $STOCINTERFACE 2>/dev/null


# Client to Server Impairments (on Bridge)
qdisc add dev $CTOSINTERFACE root netem delay ${CDELAY}ms ${CJITTER}ms loss $CLOSS% duplicate ${CDUPLICATE}% corrupt ${CCORRUPT} reorder ${CREORDER}% limit 10000000 2>&1 >/dev/null
qdisc add dev $STOCINTERFACE root netem delay ${SDELAY}ms ${SJITTER}ms loss $SLOSS% duplicate ${SDUPLICATE}% corrupt ${SCORRUPT} reorder ${SREORDER}% limit 10000000 2>&1 >/dev/null

#$PATH=$ORIGINAL_PATH
export PATH=$ORIGINAL_PATH
#echo $PATH

and call the script as

#sudo impare_network.sh 100 20 30 0 0 0 0 0 0 0 0 0 0

This would - on the up-link (client to server)- add delay of 100ms, a jitter of 20% (using normal distribution), Loss of 30% packets.

like image 156
Waqas Avatar answered Nov 15 '22 05:11

Waqas