Remove TCP Vegas support (ARM7)
[tomato.git] / release / src-rt-6.x.4708 / router / others / LinkAgg
blobfe02a73197f54ae94f5e5f65f81219a33f0f15f6
1 #!/bin/sh
2 # Written By KAD
3 # Dynamic Link Aggregation Setup
4 # Version 1.5
6 Help()
8 echo -e "\n---- Link Aggregation Version 1.5 Help ----"
9 echo -e "\nDynamically enable Link Aggregation using 802.3ad"
10 echo "802.3ad requires a Switch/PC/NAS which..."
11 echo "also supports 802.3ad to function correctly"
12 echo -e "\nUsage: /path/to/LinkAgg <port> <port>"
13 echo "Example: /path/to/LinkAgg 3 4"
14 echo -e "Only 2 ports are currently supported\n"
15 echo -e "\n--- Special Flags ---"
16 echo -e "\nHelp: -h or --help"
17 echo "Status: -s or --status"
18 echo -e "Delete: -d or --delete"
19 echo -e "Version: -v or --version\n"
20 exit
23 #Set all needed bonding parameters
24 Create_Bond()
26 # Insert bonding module and set parameters (802.3ad, 100ms MII link monitoring)
27 modprobe bonding
28 # 802.3ad mode
29 echo 802.3ad > /sys/class/net/${1}/bonding/mode
30 # LACPDUs every 1 sec
31 echo fast > /sys/class/net/${1}/bonding/lacp_rate
32 # Bring up bond
33 ip link set ${1} up
34 # 100msec MII link monitoring
35 echo 100 > /sys/class/net/${1}/bonding/miimon
36 # enslave vlans to bond
37 echo +${2} > /sys/class/net/${1}/bonding/slaves
38 echo +${3} > /sys/class/net/${1}/bonding/slaves
39 # Bridge the bond allowing AP access
40 brctl addif br0 ${1}
41 # We allow these VLANs to access the AP
42 iptables -I INPUT 1 -i ${2} -j ACCEPT
43 iptables -I INPUT 1 -i ${3} -j ACCEPT
44 iptables -I INPUT 1 -i ${1} -j ACCEPT
45 #Check Bond Status
46 sleep 10
47 Check_Bond_Status BondCreated
48 echo -e "\nBond Created Successfully\n"
51 # Delete Bond Function
52 Delete_Bond()
54 # Get vlan's used in current bond
55 DELETEVLAN1=$(cat /sys/class/net/bond0/bonding/slaves | cut -d ' ' -f 1)
56 DELETEVLAN2=$(cat /sys/class/net/bond0/bonding/slaves | cut -d ' ' -f 2)
57 # Remove bonding module, this also deletes bond0
58 modprobe -r bonding
59 # Remove dead vlan's
60 if [[ $DELETEVLAN1 != "" -o $DELETEVLAN1 != NULL ]]; then
61 vconfig rem $DELETEVLAN1
62 DeadVLAN1="Removed Dead $DELETEVLAN1"
64 if [[ $DELETEVLAN2 != "" -o $DELETEVLAN2 != NULL ]]; then
65 vconfig rem $DELETEVLAN2
66 DeadVLAN2="Removed Dead $DELETEVLAN2"
68 #Second check for -d flag allows delete function to be used in new bond creation as well as old bond removal
69 if [[ "$1" = "-d" -o "$1" = "--delete" ]]; then
70 # Set vlan1 back to default
71 robocfg vlan 1 ports "1 2 3 4 8t"
72 ReturnDefaultPorts="vlan1 default ports restored to - 1 2 3 4 8t"
73 echo -e "\nbond0 Deleted\n"
74 echo "$ReturnDefaultPorts"
75 echo "$DeadVLAN1"
76 echo -e "$DeadVLAN2\n"
77 exit
78 else
79 return
83 # Status Checker
84 Check_Bond_Status()
86 Check1=$(ip link show | grep bond0: | cut -d '<' -f 2 | cut -d ',' -f 4)
87 if [ "$Check1" != "UP" ]; then
88 Check1=NONE
89 Error1="Status : bond0 not UP"
91 CHECKVLAN1=$(cat /sys/class/net/bond0/bonding/slaves | cut -d ' ' -f 1)
92 CHECKVLAN2=$(cat /sys/class/net/bond0/bonding/slaves | cut -d ' ' -f 2)
93 if [[ "$CHECKVLAN1" = "" -o "$CHECKVLAN1" = NULL ]]; then
94 CHECKVLAN1=NONE
95 PORT1=NONE
96 Check2=NONE
97 Error2="Status : Slave 1 does not exist"
98 else
99 Check2=$(ip link show | grep "$CHECKVLAN1" | cut -d '<' -f 2 | grep -o ,UP, | grep -o UP)
100 if [ "$Check2" != "UP" ]; then
101 PORT1=NONE
102 Check2=Down
103 Error3="Status : Slave 1 not UP"
104 elif [ "$Check2" = "UP" ]; then
105 PORT1=$(robocfg show | grep "$CHECKVLAN1" | cut -d ':' -f 3 | sed -e "s/^ //" | cut -d ' ' -f 1)
108 if [[ "$CHECKVLAN2" = "" -o "$CHECKVLAN2" = NULL ]]; then
109 CHECKVLAN2=NONE
110 PORT2=NONE
111 Check3=NONE
112 Error4="Status : Slave 2 does not exist"
113 else
114 Check3=$(ip link show | grep "$CHECKVLAN2" | cut -d '<' -f 2 | grep -o ,UP, | grep -o UP)
115 if [ "$Check3" != "UP" ]; then
116 PORT2=NONE
117 Check3=Down
118 Error5="Status : Slave 2 not UP"
119 elif [ "$Check3" = "UP" ]; then
120 PORT2=$(robocfg show | grep "$CHECKVLAN2" | cut -d ':' -f 3 | sed -e "s/^ //" | cut -d ' ' -f 1)
123 WANACCESS=$(brctl show | grep bond0 | awk '{$1=$1}{ print }')
124 if [ "$WANACCESS" != "bond0" ]; then
125 WANACCESS=NONE
126 Error6="Status : bond0 not part of br0 - no WAN Access"
128 # check if status call was done by Create bond function
129 if [ "$1" = "BondCreated" ]; then
130 # if -d flag is set and any of these status checks fail, delete bond0, this is part of error recovery after new bond creation
131 if [[ "$WANACCESS" = "NONE" -o "$Check1" = "NONE" -o "$CHECKVLAN1" = "NONE" -o "$PORT1" = "NONE" -o "$Check2" = "NONE" -o "$Check2" = "Down" -o "$CHECKVLAN2" = "NONE" -o "$PORT2" = "NONE" -o "$Check3" = "NONE" -o "$Check3" = "Down" ]]; then
132 Delete_Bond -d
133 else
134 return
137 echo -e "\n--- Bond Errors ---"
138 echo "$Error1"
139 echo "$Error2"
140 echo "$Error3"
141 echo "$Error4"
142 echo "$Error5"
143 echo "$Error6"
144 echo -e "\n--- Bond Status ---\n"
145 echo -e "Bond Status: bond0 $Check1"
146 echo -e "Bridge to WAN Status: Member of br0=$WANACCESS"
147 echo -e "Slave 1 Status: vlan=$CHECKVLAN1 Link=$Check2 Port=$PORT1"
148 echo -e "Slave 2 Status: vlan=$CHECKVLAN2 Link=$Check3 Port=$PORT2\n"
149 exit
152 #Version Flag
153 if [[ "$1" = "-v" -o "$1" = "--version" ]]; then
154 Help
157 # Status Check Flag
158 if [[ "$1" = "-s" -o "$1" = "--status" ]]; then
159 Check_Bond_Status
162 # Delete Bond Flag
163 if [[ "$1" = "-d" -o "$1" = "--delete" ]]; then
164 Delete_Bond $1
167 # Enable Help Menu
168 if [[ "$1" = "-h" -o "$1" = "--help" ]]; then
169 Help
172 # Number of ports must equal 2
173 if [[ "$#" -lt "2" -o "$#" -gt "2" ]]; then
174 echo -e "\nError : Incorrect Number of Ports\n"
175 Help
178 # The Ports Must not be the same
179 if [ "$1" = "$2" ]; then
180 echo -e "\nError : Port Entries Must Be Unique\n"
181 Help
184 # Valid port1 entries are 1,2,3,4
185 if [[ "$1" != "1" && "$1" != "2" && "$1" != "3" && "$1" != "4" ]]; then
186 echo -e "\nError : Port1 : Not A Valid Port\n"
187 Help
190 # Valid port2 entries are 1,2,3,4
191 if [[ "$2" != "1" && "$2" != "2" && "$2" != "3" && "$2" != "4" ]]; then
192 echo -e "\nError : Port2 : Not A Valid Port\n"
193 Help
196 #Find how many vlans exist
197 VLANS=$(ip link show | grep vlan* | cut -d ' ' -f 2 | cut -d '@' -f 1)
198 #If only 1 vlan exist
199 if [ ${#VLANS} = "5" ]; then
200 #Check if only 1 vlan exist it should be vlan1
201 if [ "$VLANS" = "vlan1" ]; then
202 VLAN1="1 2 3 4 8t"
203 TMP=$(echo $VLAN1 | sed -e "s/$1 //g")
204 TMP1=$(echo $TMP | sed -e "s/$2 //g")
205 robocfg vlan 1 ports "$TMP1"
206 robocfg vlan 3 ports "$1 8t"
207 robocfg vlan 4 ports "$2 8t"
208 # Create the interfaces
209 vconfig add eth0 $1
210 vconfig add eth0 $2
211 BONDS=$(ip link show | grep bond0: | cut -d ' ' -f 2 | cut -d ':' -f 1)
212 #Check that bond0 does not exist
213 if [ ${#BONDS} = "0" ]; then
214 Create_Bond bond0 vlan3 vlan4
215 #If bond0 already exist remove it
216 else
217 Delete_Bond
218 Create_Bond bond0 vlan3 vlan4
220 #Only 1 vlan exist but it's not vlan1
221 else
222 echo -e "\nBonding Failed WTF!!\n"
223 Help
225 # if more than 1 vlan exist
226 else
227 #Find first available vlan
228 VLAN2=vlan$1
229 tmp=$(echo "$VLANS" | grep "$VLAN2")
230 a=$1
231 # vlan2 is used by system internals and does not show up in ip link, useage of vlan2 is not allowed
232 if [ $VLAN2 = "vlan2" ]; then
233 a=`expr $a + 1`
234 VLAN2=vlan$a
235 tmp=$(echo "$VLANS" | grep "$VLAN2")
237 while [[ "$tmp" = "$VLAN2" ]];
239 a=`expr $a + 1`
240 VLAN2=vlan$a
241 tmp=$(echo "$VLANS" | grep "$VLAN2")
242 # vlan2 is used by system internals and does not show up in ip link, useage of vlan2 is not allowed
243 if [ $VLAN2 = "vlan2" ]; then
244 a=`expr $a + 1`
245 VLAN2=vlan$a
246 tmp=$(echo "$VLANS" | grep "$VLAN2")
248 done
249 # Create First vlan for bond
250 # Remove port from vlan1 so we can use it in bond
251 VLAN2=$( echo -e "$VLAN2" | sed -e "s/vlan//g" )
252 VLAN1="1 2 3 4 8t"
253 TMP=$(echo "$VLAN1" | sed -e "s/$1 //g")
254 robocfg vlan 1 ports "$TMP"
255 robocfg vlan $VLAN2 ports "$1 8t"
256 vconfig add eth0 $VLAN2
257 #Find second available vlan
258 VLAN3=vlan$2
259 VLANS=$(ip link show | grep vlan* | cut -d ' ' -f 2 | cut -d '@' -f 1)
260 tmp1=$(echo "$VLANS" | grep "$VLAN3")
261 b=$2
262 # vlan2 is used by system internals and does not show up in ip link, useage of vlan2 is not allowed
263 if [ $VLAN3 = "vlan2" ]; then
264 b=`expr $b + 1`
265 VLAN3=vlan$b
266 tmp1=$(echo "$VLANS" | grep "$VLAN3")
268 while [[ "$tmp1" = "$VLAN3" ]];
270 b=`expr $b + 1`
271 VLAN3=vlan$b
272 tmp1=$(echo "$VLANS" | grep "$VLAN3")
273 # vlan2 is used by system internals and does not show up in ip link, useage of vlan2 is not allowed
274 if [ $VLAN3 = "vlan2" ]; then
275 b=`expr $b + 1`
276 VLAN3=vlan$b
277 tmp1=$(echo "$VLANS" | grep "$VLAN3")
279 done
280 # Create Second vlan for bond
281 # Remove port from vlan1 so we can use it in bond
282 VLAN3=$( echo "$VLAN3" | sed -e "s/vlan//g" )
283 TMP1=$(echo "$TMP" | sed -e "s/$2 //g")
284 robocfg vlan 1 ports "$TMP1"
285 robocfg vlan $VLAN3 ports "$2 8t"
286 vconfig add eth0 $VLAN3
287 BONDS=$(ip link show | grep bond0: | cut -d ' ' -f 2 | cut -d ':' -f 1)
288 #Check that bond0 does not exist
289 if [ ${#BONDS} = "0" ]; then
290 Create_Bond bond0 vlan$VLAN2 vlan$VLAN3
291 else
292 #If bond0 already exist remove it
293 Delete_Bond
294 Create_Bond bond0 vlan$VLAN2 vlan$VLAN3