Debian Codegraph @

src/setqos

raw
1 #!/bin/sh
2 #
3 # Network Performance Meter
4 # Copyright (C) 2009-2024 by Thomas Dreibholz
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #
19 # Contact: [email protected]
20 #
21
22 if [ $# -lt 1 ] ; then
23 echo >&2 "Usage: $0 id source destination bandwidth_kbit_per_s delay_ms loss_rate error_rate [-interfaces name] [-bidirectional] [-queueType type] [-frameCapacity packets] [-redMinTh threshold] [-redMaxTh threshold] [-redMaxP probability] [-redWq wq]"
24 exit 1
25 fi
26
27
28 # ====== Get parameters =====================================================
29 ID="$1"
30 if [ "$2" != "" ] ; then
31 NETWORK_SRC="$2"
32 else
33 NETWORK_SRC="0.0.0.0/0"
34 fi
35 if [ "$3" != "" ] ; then
36 NETWORK_DST="$3"
37 else
38 NETWORK_DST="0.0.0.0/0"
39 fi
40 INTERFACES=""
41 QUEUE_TYPE=red
42 FRAME_CAPACITY=80
43 RED_MINTH=20
44 RED_MAXTH=80
45 RED_MAXP=0.02
46 RED_WQ=0.002
47 BANDWIDTH=""
48 DELAY=""
49 LOSS=""
50 ERROR=""
51 addRule=0
52 bidirectionalQoS=0
53 if [ $# -ge 4 ] ; then
54 if [ $# -lt 8 ] ; then
55 echo >&2 "ERROR: Too few arguments!"
56 exit 1
57 fi
58 if [ "$4" != "" ] ; then
59 BANDWIDTH="$4"
60 addRule=1
61 fi
62 if [ "$5" != "" ] ; then
63 DELAY="$5"
64 addRule=1
65 fi
66 if [ "$6" != "" ] ; then
67 LOSS="$6"
68 addRule=1
69 fi
70 if [ "$7" != "" ] ; then
71 ERROR="$7"
72 addRule=1
73 fi
74
75 shift ; shift ; shift ; shift ; shift ; shift ; shift
76 while [ "$1" != "" ] ; do
77 if [ "$1" = "-bidirectional" ] ; then
78 bidirectionalQoS=1
79 elif [ "$1" = "-queueType" ] ; then
80 QUEUE_TYPE="$2"
81 shift
82 elif [ "$1" = "-redMinTh" ] ; then
83 RED_MINTH="$2"
84 shift
85 elif [ "$1" = "-redMaxTh" ] ; then
86 RED_MAXTH="$2"
87 shift
88 elif [ "$1" = "-redMaxP" ] ; then
89 RED_MAXP="$2"
90 shift
91 elif [ "$1" = "-redWq" ] ; then
92 RED_WQ="$2"
93 shift
94 elif [ "$1" = "-frameCapacity" ] ; then
95 FRAME_CAPACITY="$2"
96 shift
97 elif [ "$1" = "-interfaces" ] ; then
98 INTERFACES="$2"
99 shift
100 else
101 echo >&2 "ERROR: Invalid option $1!"
102 exit 1
103 fi
104 shift
105 done
106 fi
107
108 if [ $addRule -eq 0 ] ; then
109 echo "REM"
110 fi
111
112
113 # ====== Linux QoS setup ====================================================
114 success=1
115 SYSTEM=`uname`
116 if [ "$SYSTEM" = "Linux" ] ; then
117 if [ "$INTERFACES" = "" ] ; then # Use *all* interfaces
118 INTERFACES=`eval "ifconfig -a" 2>/dev/null | sed -ne 's|^\('$cur'[^[:space:][:punct:]]\{1,\}\).*$|\1|p'`
119 fi
120
121 for iface in $INTERFACES ; do
122 tc qdisc del dev $iface root 2>/dev/null # Ohne ID-Angabe wird Root mit jeder ID entfernt!
123 done
124
125 if [ $addRule -eq 1 ] ; then
126 bandwidthParameter=""
127 weightParameter=""
128 delayParameter=""
129 lossParameter=""
130 errorParameter=""
131 if [ "$BANDWIDTH" != "" ] ; then
132 weightParameter=""
133 bandwidthParameter="rate ${BANDWIDTH}Kbit"
134 fi
135 if [ "$DELAY" != "" ] ; then
136 delayParameter="delay ${DELAY}ms"
137 fi
138 if [ "$LOSS" != "" ] ; then
139 lossParameter="loss `echo "${LOSS}*100.0" | bc`%"
140 fi
141 if [ "$ERROR" != "" ] ; then
142 errorParameter="corrupt `echo "${ERROR}*100.0" | bc`%"
143 fi
144
145 for iface in $INTERFACES ; do
146 tc qdisc add dev $iface root handle 9$ID:0 cbq bandwidth 100Mbit avpkt 1500 cell 8 || success=0
147 tc class add dev $iface parent 9$ID:0 classid 9$ID:3 cbq \
148 bandwidth 100Mbit $bandwidthParameter $weightParameter \
149 prio 8 allot 1514 cell 8 maxburst 20 avpkt 1500 bounded || success=0
150
151 if [ "$QUEUE_TYPE" = "fifo" ] ; then
152 tc qdisc add dev $iface handle 8$ID:0 parent 9$ID:3 pfifo limit $FRAME_CAPACITY
153 elif [ "$QUEUE_TYPE" = "red" ] ; then
154 # tc qdisc add dev $iface parent 8$ID:0 red limit $FRAME_CAPACITY avpkt 1500
155 echo "RED configuration: IMPLEMENT ME!"
156 exit 1
157 else
158 echo >&2 "ERROR: Bad queue type?!"
159 exit 1
160 fi
161
162 tc filter add dev $iface protocol ip parent 9$ID:0 \
163 u32 match ip src $NETWORK_SRC match ip dst $NETWORK_DST flowid 9$ID:3 || success=0
164 if [ $bidirectionalQoS -eq 1 ] ; then
165 tc filter add dev $iface protocol ip parent 9$ID:0 \
166 u32 match ip src $NETWORK_DST match ip dst $NETWORK_SRC flowid 9$ID:3 || success=0
167 fi
168
169 # tc -s qdisc ls dev $iface || success=0
170 if [ $success -ne 1 ] ; then
171 echo >&2 "ERROR: Failed to configure QDiscs on interface $iface!"
172 fi
173 done
174 fi
175
176
177 # ====== FreeBSD QoS setup ==================================================
178 elif [ "$SYSTEM" = "FreeBSD" ] ; then
179 ID2=$(($ID+1))
180 /sbin/ipfw $ID delete pipe $ID 2>/dev/null # Delete old configuration
181 /sbin/ipfw $ID2 delete pipe $ID2 2>/dev/null # Delete old configuration
182 if [ $addRule -eq 1 ] ; then
183 if [ "$BANDWIDTH" != "" ] ; then
184 BANDWIDTH="bw ${BANDWIDTH}Kbit/s"
185 fi
186 if [ "$DELAY" != "" ] ; then
187 DELAY="delay ${DELAY}ms"
188 fi
189 if [ "$LOSS" != "" ] ; then
190 LOSS="plr ${LOSS}"
191 fi
192 if [ "$ERROR" != "" ] ; then
193 if [ ! $ERROR -eq 0 ] ; then
194 ERROR="ber ${ERROR}"
195 else
196 ERROR=""
197 fi
198 fi
199
200 queueParameters=
201 if [ "$QUEUE_TYPE" = "red" ] ; then
202 queueParameters="queue $FRAME_CAPACITY red $RED_WQ/$RED_MINTH/$RED_MAXTH/$RED_MAXP"
203 elif [ "$QUEUE_TYPE" = "fifo" ] ; then
204 queueParameters="queue $FRAME_CAPACITY"
205 else
206 echo >&2 "ERROR: Invalid queue type $QUEUE_TYPE!"
207 exit 1
208 fi
209
210 /sbin/ipfw add $ID pipe $ID out src-ip $NETWORK_SRC dst-ip $NETWORK_DST || success=0
211 /sbin/ipfw pipe $ID config $BANDWIDTH $DELAY $LOSS $ERROR $queueParameters || success=0
212 if [ $bidirectionalQoS -eq 1 ] ; then
213 /sbin/ipfw add $ID2 pipe $ID2 out src-ip $NETWORK_DST dst-ip $NETWORK_SRC || success=0
214 /sbin/ipfw pipe $ID2 config $BANDWIDTH $DELAY $LOSS $ERROR $queueParameters || success=0
215 fi
216 fi
217
218
219 # ====== Other systems ======================================================
220 else
221 echo >&2 "ERROR: Unsupported system $SYSTEM!"
222 exit 1
223 fi
224
225
226 # ====== Check whether QoS configuration has been successful ================
227 if [ $success -ne 1 ] ; then
228 echo >&2 "ERROR: setqos has failed to configure QoS settings!"
229 echo >&2 "Command on `hostname`: $0 $@"
230 exit 1
231 fi