From fda9f25fdb9b4ede985e4d0bb82ba7bc2fde2073 Mon Sep 17 00:00:00 2001 From: Ole Tange Date: Tue, 14 Sep 2021 22:10:28 +0200 Subject: [PATCH] bwlimit: Initial release. --- Makefile | 22 +++++------ bwlimit/bwlimit | 99 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 11 deletions(-) create mode 100755 bwlimit/bwlimit diff --git a/Makefile b/Makefile index dcc9a32..1614c22 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,15 @@ -CMD = blink 2grep 2search burncpu drac duplicate-packets em emoticons \ - encdir fanspeed field find-first-fail forever fxkill G \ - gitnext gitundo goodpasswd histogram Loffice mtrr mirrorpdf \ - neno not off pdfman pidcmd pidtree plotpipe puniq ramusage \ - rand rclean rina rn rrm seekmaniac shython sound-reload \ - splitvideo stdout swapout T teetime timestamp tracefile \ - transpose upsidedown vid w4it-for-port-open whitehash \ - wifi-reload wssh youtube-lbry ytv yyyymmdd +CMD = 2grep 2search blink burncpu bwlimit drac duplicate-packets em \ + emoticons encdir fanspeed field find-first-fail forever \ + fxkill G gitnext gitundo goodpasswd histogram Loffice mtrr \ + mirrorpdf neno not off pdfman pidcmd pidtree plotpipe puniq \ + ramusage rand rclean rina rn rrm seekmaniac shython \ + sound-reload splitvideo stdout swapout T teetime timestamp \ + tracefile transpose upsidedown vid w4it-for-port-open \ + whitehash wifi-reload wssh youtube-lbry ytv yyyymmdd -all: blink/blink.1 2search/2grep.1 2search/2search.1 \ - burncpu/burncpu.1 drac/drac.1 encdir/encdir.1 \ - fanspeed/fanspeed.1 field/field.1 \ +all: 2search/2grep.1 2search/2search.1 blink/blink.1 \ + burncpu/burncpu.1 bwlimit/bwlimit.1 drac/drac.1 \ + encdir/encdir.1 fanspeed/fanspeed.1 field/field.1 \ find-first-fail/find-first-fail.1 G/G.1 gitnext/gitnext.1 \ gitundo/gitundo.1 goodpasswd/goodpasswd.1 \ histogram/histogram.1 mirrorpdf/mirrorpdf.1 neno/neno.1 \ diff --git a/bwlimit/bwlimit b/bwlimit/bwlimit new file mode 100755 index 0000000..ef799d6 --- /dev/null +++ b/bwlimit/bwlimit @@ -0,0 +1,99 @@ +#!/bin/bash + +: <<=cut +=pod + +=head1 NAME + +bwlimit - Limit bandwidth based on interface and port + + +=head1 SYNOPSIS + +B I I [I] + + +=head1 DESCRIPTION + +B limits incoming trafic to I megabits/second. + +If I is not given, B will look for B in +B. + +=head1 EXAMPLE + +Limit eth0 to 2 megabits/seconds on port 9001 + + bwlimit eth0 2 9001 + + +=head1 AUTHOR + +Copyright (C) 2021 Ole Tange, +http://ole.tange.dk and Free Software Foundation, Inc. + + +=head1 LICENSE + +Copyright (C) 2012 Free Software Foundation, Inc. + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 3 of the License, or +at your option any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + + +=head1 DEPENDENCIES + +B uses B. + + +=head1 SEE ALSO + +B + + +=cut + +bwlimit() { + help() { + echo "Limit bandwidth of interface" + echo + echo "$0 interface mbps [port]" + echo "E.g. $0 eth0 10 9001" + return 255 + } + bwlimitport() { + iface="$1" + port="$2" + mbps="$3" + tc filter add dev "$iface" ingress protocol ip \ + basic match "cmp(u16 at 2 layer transport eq $port)" \ + action police rate "$mbps"mibit burst 256k + } + iface="$1" + mbps="$2" + port="$3" + + orporttorrc=$(grep -E ^ORPort /etc/tor/torrc | awk '{print $2}') + ORPort=${port:-$orporttorrc} + if [[ "$iface" = "--help" ]] ; then + help + return $! + fi + if [[ "$mbps" = "off" ]] ; then + tc qdisc del dev "$iface" ingress + else + tc qdisc add dev "$iface" ingress + bwlimitport "$iface" "$ORPort" "$mbps" + fi +} +bwlimit "$@"