Browse Source

Added "access interfaces" support

Dmitry Yu Okunev 7 years ago
parent
commit
e6877c5ac7
1 changed files with 59 additions and 73 deletions
  1. 59 73
      ipw

+ 59 - 73
ipw

@@ -112,9 +112,9 @@ for VLAN_N_BRIDGE in $(find /sys/devices/virtual/net -wholename '*/brif/*' | awk
 	get_brname_byslave_assoc[${words[0]}]=${words[1]}
 done
 get_brname_byslave() {
-	SLAVE="$1"; shift
+	_SLAVE="$1"; shift
 
-	echo ${get_brname_byslave_assoc[$SLAVE]}
+	echo ${get_brname_byslave_assoc[$_SLAVE]}
 }
 
 get_vlan_n_bridges() {
@@ -124,11 +124,23 @@ get_vlan_n_bridges() {
 }
 
 iface_isexists() {
-	IFACE="$1"; shift;
-	[ -e /sys/devices/virtual/net/"$IFACE" ]
+	_IFACE="$1"; shift;
+	[ -e /sys/devices/virtual/net/"$_IFACE" ]
 	return $?
 }
 
+ip_cmds() {
+	_IFACE="$1"; shift
+
+	ip addr flush dev "$_IFACE"
+	IP_CMDS_VARIABLE="${_IFACE//[.]/_}_IP"
+	IP_CMDS_VARIABLE="${IP_CMDS_VARIABLE}[@]"
+	for IP_CMD in "${!IP_CMDS_VARIABLE}"; do
+		[[ "$IP_CMD" != "" ]] &&
+			ip $IP_CMD dev "$_IFACE"
+	done
+}
+
 case "$CMD" in
 	down)
 		IPW_CONFIG_PATH=/dev/null
@@ -324,9 +336,6 @@ case "$CMD" in
 						done
 
 						if [[ "$SLAVE_IS_USED" == 0 ]]; then
-							#echo "ip link set dev $OLD_SLAVE down"
-							#echo "echo '-$OLD_SLAVE' > /sys/class/net/"$OLD_D1Q_IFACE"/bonding/slaves"
-#							[[ "$OLD_SLAVE" != "" ]] &&
 							echo "-$OLD_SLAVE" > /sys/class/net/"$OLD_D1Q_IFACE"/bonding/slaves
 						fi
 					done
@@ -336,13 +345,11 @@ case "$CMD" in
 			done
 
 			if [[ "$D1Q_IS_USED" == 0 ]]; then #let's kick out useless d1q
-				#echo "ip link set dev $OLD_D1Q_IFACE down"
-				#echo "echo '-$D1Q_IFACE' > /sys/class/net/bonding_masters"
 				echo "-$OLD_D1Q_IFACE" > /sys/class/net/bonding_masters
 			fi
 		done
 
-		#REMOVING UNNECESSARY BRIDGES AND VLANS
+		#REMEMBERING REQUIRED INTERFACES
 
 		declare -A REQUIRED_FULLNAME_HASH
 		declare -A REQUIRED_BRNAME_HASH
@@ -367,16 +374,40 @@ case "$CMD" in
 			REQUIRED_FULLNAME_HASH["$LXCIF_N_BRIDGE"]="$LXCIF_N_BRIDGE"
 		done
 
+		#CONFIGURING AND REMEMBERING ACCESS INTERFACES (NOT D1Q)
+
+		for ACCESS_IFACE in ${ACCESS_IFACES[@]}; do
+			oldIFS="$IFS"; IFS=":"
+			WORDS=(${ACCESS_IFACE})
+			IFS="$oldIFS"
+
+			IFACE="${WORDS[0]}"
+			BRNAME="${WORDS[1]}"
+
+			if [[ "$BRNAME" != "" ]]; then
+				REQUIRED_BRNAME_HASH["$BRNAME"]="$BRNAME"
+				
+				if ! iface_isexists "$BRNAME"; then
+					ip link add "$BRNAME" type bridge
+					ip link set dev "$BRNAME" up
+				fi
+				ip link set "$IFACE" master "$BRNAME"
+
+				ip_cmds "$BRNAME"
+			fi
+
+			REQUIRED_IFACENAME_HASH["$IFACE"]="$IFACE"
+			ip_cmds "$IFACE"
+		done
+
+		#REMOVING UNNECESSARY BRIDGES AND VLANS
+
 		OLD_VLAN_N_BRIDGES=(`get_vlan_n_bridges`)
 		for LINE in ${OLD_VLAN_N_BRIDGES[@]}; do
 			#echo ITERATION
 			parse_vlan_subvlan_bridge "$LINE"
 			OLD_BRNAME="$BRNAME"
 			OLD_VLANNAME="$VLANNAME"
-			##
-			#echo "BRNAME $BRNAME"
-			#echo "VLANNAME $VLANNAME"
-			##
 			IS_SLAVE=0
 			
 			BRIDGE_IN_USE=0
@@ -442,9 +473,7 @@ case "$CMD" in
 							fi	
 						done
 						if [[ "$SLAVE_EXISTS" == 0 ]]; then
-							#echo "ip link set dev $SLAVE down" #enslaved int should be shutted down
 							ip link set dev "$SLAVE" down #enslaved int should be shutted down
-							#echo "echo '+$SLAVE' > /sys/class/net/"$D1Q_IFACE"/bonding/slaves"
 							echo "+$SLAVE" > /sys/class/net/"$D1Q_IFACE"/bonding/slaves
 						fi
 					done
@@ -455,24 +484,19 @@ case "$CMD" in
 
 			# Creating bond-ifaces
 			if [[ "$D1Q_EXISTS" == 0 ]]; then #make all
-				#echo "echo '+$D1Q_IFACE' > /sys/class/net/bonding_masters"
 				echo "+$D1Q_IFACE" > /sys/class/net/bonding_masters
 				echo "$MODE" > /sys/class/net/"$D1Q_IFACE"/bonding/mode
 
 				for SLAVE in "${!SLAVES_VARIABLE}"; do
-					#echo "ip link set dev $SLAVE down"  #enslaved int should be shutted down
 					ip link set dev "$SLAVE" down  #enslaved int should be shutted down
-					#echo "echo '+$SLAVE' > /sys/class/net/"$D1Q_IFACE"/bonding/slaves"
 					echo "+$SLAVE" > /sys/class/net/"$D1Q_IFACE"/bonding/slaves
 				done
 
 			fi
 
-			#echo "ip link set dev $D1Q_IFACE up"
 			ip link set dev "$D1Q_IFACE" up
 
 			for SLAVE in "${!SLAVES_VARIABLE}"; do #slave starts after bond
-				#echo "ip link set dev $SLAVE up"
 				ip link set dev "$SLAVE" up
 			done
 
@@ -482,104 +506,56 @@ case "$CMD" in
 			#CREATING NECESSARY BRIDGES AND VLANS
 
 			for VLAN_N_BRIDGE in "${!VLAN_N_BRIDGE_VARIABLE}"; do
-				##
-				#echo $VLAN_N_BRIDGE
-				##
 				parse_vlan_subvlan_bridge "${VLAN_N_BRIDGE}"
-				##
-				#echo BRNAME $BRNAME
-				#echo VLANNAME $VLANNAME
-				#echo VLAN $VLApN
-				#echo SUBVLAN $SUBVLAN
-				##
 
 				FULL_VLAN_NAME="$D1Q_IFACE"."$VLANNAME"
-				##
-				#echo FULL_VLAN_NAME $FULL_VLAN_NAME
-				##
 
 				#VLAN STUFF
+
 				if  [[ "$VLAN" != "" ]]; then
 					if ! iface_isexists "$FULL_VLAN_NAME"; then #should we create a new vlan?
-					#ip link show | grep -q "$FULL_VLAN_NAME"
-					#if $?; then #should we create a new vlan?
 						if [[ "$SUBVLAN" != "" ]]; then #is it subvlan?
 							FULL_PARVLAN_NAME="$D1Q_IFACE"."$VLAN"
-							##
-							#echo FULL_PARVLAN_NAME $FULL_PARVLAN_NAME
-							#echo D1Q_IFACE $D1Q_IFACE 
-							##
+
 							if ! iface_isexists "$FULL_PARVLAN_NAME"; then #should we create a new parent vlan?
-								#echo "ip link add link $D1Q_IFACE name $FULL_PARVLAN_NAME type vlan id $VLAN" #vlan in bond
 								ip link add link "$D1Q_IFACE" name "$FULL_PARVLAN_NAME" type vlan id "$VLAN" #vlan in bond
-								#echo "ip link set dev $FULL_PARVLAN_NAME up"
 								ip link set dev "$FULL_PARVLAN_NAME" up
 							fi
-							#echo "ip link add link $FULL_PARVLAN_NAME name $FULL_VLAN_NAME type vlan id $SUBVLAN" #subvlan in vlan
 							ip link add link "$FULL_PARVLAN_NAME" name "$FULL_VLAN_NAME" type vlan id "$SUBVLAN" #subvlan in vlan
-							#echo "ip link set dev $FULL_VLAN_NAME up"
 							ip link set dev "$FULL_VLAN_NAME" mtu 1496
 							ip link set dev "$FULL_VLAN_NAME" up
 						else
-							#echo "ip link add link $D1Q_IFACE name $FULL_VLAN_NAME type vlan id $VLANNAME" #vlan in bond
 							ip link add link "$D1Q_IFACE" name "$FULL_VLAN_NAME" type vlan id "$VLANNAME" #vlan in bond
-							#echo "ip link set dev $FULL_VLAN_NAME up"
 							ip link set dev "$FULL_VLAN_NAME" up
 						fi
 					fi
 
-					ip addr flush dev "$FULL_VLAN_NAME"
-					IP_CMDS_VARIABLE="${FULL_VLAN_NAME//[.]/_}_IP"
-					IP_CMDS_VARIABLE="${IP_CMDS_VARIABLE}[@]"
-					for IP_CMD in "${!IP_CMDS_VARIABLE}"; do
-						ip $IP_CMD dev "$FULL_VLAN_NAME"
-					done
+					ip_cmds "$FULL_VLAN_NAME"
 				fi
 
 				#BRIDGE STUFF
 
-				##
-				#echo BRIDGE STUFF "$BRNAME"
-				##
 				if [[ "$BRNAME" != "" ]]; then
 
-					#if [[ "`brctl show | grep "$BRNAME"`" = "" ]]; then #do this bridge exists?
 					if ! iface_isexists "$BRNAME"; then #do this bridge exists?
-						#brctl addbr "$BRNAME"
-						#echo "ip link add "$BRNAME" type bridge"
 						ip link add "$BRNAME" type bridge
 					fi
 
 					if [[ "$VLAN" != "" ]]; then
-						#OLD_BRNAME="`brctl show | grep "$FULL_VLANNAME" | awk '{print $1}'`"
 						OLD_BRNAME="`get_brname_byslave "$FULL_VLAN_NAME"`"
 						if [[ "$OLD_BRNAME" = "" ]]; then #is this vlan have been already enslaved?
-							#brctl addif "$BRNAME" "$FULL_VLANNAME"
-							#echo "ip link set "$FULL_VLAN_NAME" master "$BRNAME
 							ip link set "$FULL_VLAN_NAME" master "$BRNAME"
-							#echo "ip link set dev $BRNAME up"
 							ip link set dev "$BRNAME" up
 						else
 							if [[ "$BRNAME" != "$OLD_BRNAME" ]]; then
-								#brctl delif "$OLD_BRNAME" "FULL_VLANNAME"
-								#echo "ip link set "$FULL_VLAN_NAME" nomaster"
 								ip link set "$FULL_VLAN_NAME" nomaster
-								#brctl addif "$BRNAME" "$FULL_VLANNAME"
-								#echo "ip link set "$FULL_VLAN_NAME" master "$BRNAME
 								ip link set "$FULL_VLAN_NAME" master "$BRNAME"
-								#echo "ip link set dev $BRNAME up"
 								ip link set dev "$BRNAME" up
 							fi
 						fi
 					fi
 
-					ip addr flush dev "$BRNAME"
-					IP_CMDS_VARIABLE="${BRNAME//[.]/_}_IP"
-					IP_CMDS_VARIABLE="${IP_CMDS_VARIABLE}[@]"
-					for IP_CMD in "${!IP_CMDS_VARIABLE}"; do
-						[[ "$IP_CMD" != "" ]] &&
-							ip $IP_CMD dev "$BRNAME"
-					done
+					ip_cmds "$BRNAME"
 				fi
 			done
 		done
@@ -672,6 +648,16 @@ for D1Q_IFACE in ${D1Q_IFACES[@]}; do
 	newconfig_push 1 ")"
 
 done
+for ACCESS_IFACE in ${ACCESS_IFACES[@]}; do
+	oldIFS="$IFS"; IFS=":"
+	WORDS=(${ACCESS_IFACE})
+	IFS="$oldIFS"
+
+	IFACE="${WORDS[0]}"
+	BRNAME="${WORDS[1]}"
+
+	printf "access: %-12s bridge: %-8s\n" "$IFACE" "$BRNAME"
+done
 
 newconfig_write ")"