ipw 19.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
#!/bin/bash

#    Copyright (C) 2013 Artyom A Anikeev <anikeev@ut.mephi.ru> 0xB5385841,
#                       Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
#
#    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 <http://www.gnu.org/licenses/>.

CMD="$1"; shift;

DEFAULT_BONDING_MODE='balance-tlb'

modprobe bonding

eerror() {
	echo "$@" >&2
}

newconfig_open() {
	echo -n > "$NEWCONFIG_TMPPATH"
}

newconfig_write() {
	LINE="$1"; shift;

	[[ "$NEWCONFIG" -eq 0 ]] && return

	echo "$LINE" >> "$NEWCONFIG_TMPPATH"
}

newconfig_commit() {
	rm -f "$CONFIG_PATH"
	mv "$NEWCONFIG_TMPPATH" "$CONFIG_PATH"
}

newconfig_push() {
	BUFNUM="$1";	shift;
	LINE="$1";	shift;

	BUF[$BUFNUM]="${BUF[$BUFNUM]}
$LINE"
}

newconfig_pushpopall() {
	BUFNUMTO="$1";		shift;
	BUFNUMFROM="$1";	shift;

	newconfig_push "$BUFNUMTO" "${BUF[$BUFNUMFROM]}"
	newconfig_popall "$BUFNUMFROM"
}

newconfig_popall() {
	BUFNUM="$1";	shift;

	BUF[$BUFNUM]=""
}

newconfig_popallwrite() {
	BUFNUM="$1";	shift;

	newconfig_write "${BUF[$BUFNUM]}"
	BUF[$BUFNUM]=""
}

# INPUT: 10.2:thor.ext; OUTPUT: environments: VLAN, SUBVLAN, BRNAME
parse_vlan_subvlan_bridge() {
	VLAN_N_BRIDGE="$1"; shift

	if [[ "${VLAN_N_BRIDGE:0:1}" = ":" ]]; then 
		VLAN=""
		SUBVLAN=""
		VLANNAME=""
		BRNAME="${VLAN_N_BRIDGE:1}"
	else
		# Splitting by ":"
		WORDS=( ${VLAN_N_BRIDGE//[:]/ } )

		# The second word defines interface name. If it's empty, "vlan$VLAN.$SUBVLAN" is used.
		VLANNAME="${WORDS[0]}"
		BRNAME="${WORDS[1]}"

		# Splitting by "."
		WORDS=( ${VLANNAME//[.]/ } )

		# The second word defines subvlan (1q-in-1q) if it's used. It's empty if no subvlan is used.
		VLAN="${WORDS[0]}"
		SUBVLAN="${WORDS[1]}"
	fi

	# Checking if no "vlan(.subvlan)?" is set
	if ! [[ "${VLAN_N_BRIDGE: -1}" = ":" ]]; then
		# Setting default values
		[[ "$SUBVLAN" = "" ]] && BRNAME="${BRNAME:-vlan${VLAN}}" || BRNAME="${BRNAME:-vlan${VLAN}.${SUBVLAN}}"
	fi

	return;
}

declare -A get_brname_byslave_assoc
for VLAN_N_BRIDGE in $(find /sys/devices/virtual/net -wholename '*/brif/*' | awk -F '/' '{print $8":"$6}'); do
	oldIFS="$IFS"; IFS=':'; words=($VLAN_N_BRIDGE); IFS="$oldIFS"
	get_brname_byslave_assoc[${words[0]}]=${words[1]}
done
get_brname_byslave() {
	_SLAVE="$1"; shift

	echo ${get_brname_byslave_assoc[$_SLAVE]}
}

get_vlan_n_bridges() {
	awk '{if(NR>2) if( system( "[ -d /sys/devices/virtual/net/*/brif/"$1" ] " )  != 0 ) print $1":"}' < /proc/1/net/vlan/config
	find /sys/devices/virtual/net -wholename '*/brif' -empty | awk -F '/' '{print   ":"$6}'
	find /sys/devices/virtual/net -wholename '*/brif/*'      | awk -F '/' '{print $8":"$6}'
}

iface_isexists() {
	_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
		CMD='fix'
		;;
esac

IPW_CONFIG_PATH="${IPW_CONFIG_PATH:-/etc/ipw.conf}"

BRIDGE_FORWARD_DELAY=0

source "$IPW_CONFIG_PATH"

NEWCONFIG_TMPPATH="/tmp/.~ipw"

NEWCONFIG=0
case "$CMD" in
	add1qif) #allow to add slave interface in bonding
		NEWCONFIG=1	# we need to update the config after the command

		NEW_D1Q_IFACE="$1"; shift
		NEW_SLAVE="$1"; shift

		# checking for duplicated slaves or d1q_ifaces
		D1Q_IFACE_USED=0
		for D1Q_IFACE in ${D1Q_IFACES[@]}; do
			if [[ "$D1Q_IFACE" = "$NEW_D1Q_IFACE" ]]; then
				D1Q_IFACE_USED=1
			fi
			SLAVES_VARIABLE="${D1Q_IFACE}_SLAVES"
			SLAVES_VARIABLE="${SLAVES_VARIABLE//[.]/_}[@]"
			for SLAVE in "${!SLAVES_VARIABLE}"; do
				if [[ "$SLAVE" = "$NEW_SLAVE" ]]; then
					eerror "This interface have already been enslaved"
					exit -1
				fi
			done
		done

		if [[ $D1Q_IFACE_USED -eq 0 ]]; then
			D1Q_IFACES[ ${#D1Q_IFACES} ]="$NEW_D1Q_IFACE"
		fi

		SLAVES_VARIABLE="${NEW_D1Q_IFACE}_SLAVES"
		SLAVES_VARIABLE="${SLAVES_VARIABLE//[.]/_}"
		eval "${SLAVES_VARIABLE}[\${#${SLAVES_VARIABLE}}]=\"$NEW_SLAVE\""

		;;
	del1qif)
		;;
	addbrif) #allow to add bridge/vlan in bonding
		NEWCONFIG=1	# we need to update the config after the command

		NEW_D1Q_IFACE="$1"; shift
		NEW_VLAN_SUBVLAN="$1"; shift
		NEW_BRIDGE="$1"; shift

		[[ -z $NEW_BRIDGE ]] && NEW_VLAN_N_BRIDGE="${NEW_VLAN_SUBVLAN}" || NEW_VLAN_N_BRIDGE="${NEW_VLAN_SUBVLAN}:${NEW_BRIDGE}"

		D1Q_IFACE_USED=0

		for D1Q_IFACE in ${D1Q_IFACES[@]}; do
			if [[ "$D1Q_IFACE" = "$NEW_D1Q_IFACE" ]]; then
				D1Q_IFACE_USED=1
				break;
			fi
		done

		parse_vlan_subvlan_bridge "${NEW_VLAN_N_BRIDGE}"
		NEW_VLAN=$VLAN
		NEW_SUBVLAN=$SUBVLAN

		if [[ $D1Q_IFACE_USED -eq 0 ]]; then
			eerror "There's no such interface: $NEW_D1Q_IFACE"
			exit -1
		fi

		VLAN_N_BRIDGE_VARIABLE="${NEW_D1Q_IFACE}_VLAN_N_BRIDGES"
		VLAN_N_BRIDGE_VARIABLE="${VLAN_N_BRIDGE_VARIABLE//[.]/_}[@]"
		for VLAN_N_BRIDGE in "${!VLAN_N_BRIDGE_VARIABLE}"; do
			parse_vlan_subvlan_bridge "${VLAN_N_BRIDGE}"
 			if [[ "$VLAN" = "$NEW_VLAN" && "$SUBVLAN" = "$NEW_SUBVLAN" ]]; then
				eerror "This vlan is alredy used"
				exit -1
			fi		
		done

		VLAN_N_BRIDGE_VARIABLE="${NEW_D1Q_IFACE}_VLAN_N_BRIDGES"
		VLAN_N_BRIDGE_VARIABLE="${VLAN_N_BRIDGE_VARIABLE//[.]/_}"
		eval "${VLAN_N_BRIDGE_VARIABLE}[\${#${VLAN_N_BRIDGE_VARIABLE}}]=\"$NEW_VLAN_N_BRIDGE\""

			
		#D1Q_IFACES[ ${#D1Q_IFACES}]
		
		;;
	delbrif)
		;;
	addbr) #allow to add bridge without interfaces
		NEWCONFIG=1	# we need to update the config after the command

		NEW_D1Q_IFACE="$1"; shift
		NEW_BRIDGE="$1"; shift

		VLAN_N_BRIDGE_VARIABLE="${NEW_D1Q_IFACE}_VLAN_N_BRIDGES"
		VLAN_N_BRIDGE_VARIABLE="${VLAN_N_BRIDGE_VARIABLE//[.]/_}"
		eval "${VLAN_N_BRIDGE_VARIABLE}[\${#${VLAN_N_BRIDGE_VARIABLE}}]=\":${NEW_BRIDGE}\""

		;;
	delbr)
		;;
	addif) #allow to add interface without bridging
		;;
	delif)
		;;
	addipcmd) #allow to add ip commnds for bridges/vlans
		NEWCONFIG=1	# we need to update the config after the command
		NEW_BRIDGE="$1"; shift

		NEW_IP_CMD="$@"

		if [[ "${NEW_BRIDGE}" != "" ]]; then

			# Getting commands for "ip"
			IP_CMDS_VARIABLE="${NEW_BRIDGE//[.]/_}_IP"
			IP_CMDS_VARIABLE="${IP_CMDS_VARIABLE}"
			eval "${IP_CMDS_VARIABLE}[\${#${IP_CMDS_VARIABLE}}]=\"$NEW_IP_CMD\""

		fi

		;;
	delipcmd)
		NEWCONFIG=1	# we need to update the config after the command
		NEW_BRIDGE="$1"; shift

		NEW_IP_CMD="$@"

		if [[ "${NEW_BRIDGE}" != "" ]]; then

			IP_CMDS_VARIABLE="${NEW_BRIDGE//[.]/_}_IP"
			IP_CMDS_VARIABLE="${IP_CMDS_VARIABLE}"
			i=0
			CMD_founded=0
			for IP_CMD in "${!IP_CMDS_VARIABLE}"; do
				if [[ "$IP_CMD" = "$NEW_IP_CMD" ]]; then
					CMD_founded=1
				else
					TMP[$i]=$IP_CMD
					(( i++ ))
				fi
			done

			if [[ $CMD_founded -eq 0 ]]; then
				eerror "There's no such command"
				exit -1
	
			fi

			i=0
			for IP_CMD in "${!TMP}"; do
				IP_CMDS_VARIABLE[$i]=$IP_CMD
				(( i++ ))
			done

			#eval "${IP_CMDS_VARIABLE}[\${#${IP_CMDS_VARIABLE}}]=\"$NEW_IP_CMD\""
			#echo $IP_CMDS_VARIABLE

		fi	;;
	fix)	#fix all interfaces to config state
		#brctl addif bond0 eth2

		#GETTING INFORMATION ABOUT LXC INTERFACES IN BRIDGES

		if [[ "$LXC_DIR" != "" ]]; then
			LXC_BRIDGE_SLAVES=($(cat "$LXC_DIR"/*/config | tr -d " \t" | awk -F '=' '{if($1=="lxc.network.link") {BRIDGE=$2}; if($1=="lxc.network.veth.pair") {if(BRIDGE!="") {print $2":"BRIDGE; BRIDGE=""}} }'))
		fi
		
		#REMOVING UNNECESSARY D1Q

		OLD_D1Q_IFACES="`cat /sys/class/net/bonding_masters`"

		for OLD_D1Q_IFACE in ${OLD_D1Q_IFACES[@]}; do
			D1Q_IS_USED=0
			OLD_SLAVES="`cat /sys/class/net/"$OLD_D1Q_IFACE"/bonding/slaves`"
			for D1Q_IFACE in ${D1Q_IFACES[@]}; do
				if [[ "$D1Q_IFACE" == "$OLD_D1Q_IFACE" ]]; then #let's kick out useless slaves
					SLAVES_VARIABLE="${D1Q_IFACE}_SLAVES"
					SLAVES_VARIABLE="${SLAVES_VARIABLE//[.]/_}[@]"
					for OLD_SLAVE in ${OLD_SLAVES[@]}; do
						SLAVE_IS_USED=0
						for SLAVE in "${!SLAVES_VARIABLE}"; do
							if [[ "$SLAVE" == "$OLD_SLAVE" ]]; then
								SLAVE_IS_USED=1
								break;
							fi
						done

						if [[ "$SLAVE_IS_USED" == 0 ]]; then
							echo "-$OLD_SLAVE" > /sys/class/net/"$OLD_D1Q_IFACE"/bonding/slaves
						fi
					done
					D1Q_IS_USED=1
					break;
				fi
			done

			if [[ "$D1Q_IS_USED" == 0 ]]; then #let's kick out useless d1q
				echo "-$OLD_D1Q_IFACE" > /sys/class/net/bonding_masters
			fi
		done

		#REMEMBERING REQUIRED INTERFACES

		declare -A REQUIRED_FULLNAME_HASH
		declare -A REQUIRED_BRNAME_HASH
		declare -A REQUIRED_IFACENAME_HASH

		for D1Q_IFACE_TMP in ${D1Q_IFACES[@]}; do #we should keep slave ifaces
			VLAN_N_BRIDGE_VARIABLE="${D1Q_IFACE_TMP}_VLAN_N_BRIDGES"
			VLAN_N_BRIDGE_VARIABLE="${VLAN_N_BRIDGE_VARIABLE//[.]/_}[@]"
			for VLAN_N_BRIDGE in "${!VLAN_N_BRIDGE_VARIABLE}"; do
				parse_vlan_subvlan_bridge "${VLAN_N_BRIDGE}"
				REQUIRED_FULLNAME_HASH["$D1Q_IFACE_TMP"."$VLAN_N_BRIDGE"]="$D1Q_IFACE_TMP"."$VLAN_N_BRIDGE"
				if [[ "$BRNAME" != "" ]]; then
					REQUIRED_BRNAME_HASH["$BRNAME"]="$BRNAME"
				fi
				if [[ "$VLANNAME" != "" ]]; then
					REQUIRED_IFACENAME_HASH["$VLANNAME"]="$VLANNAME"
					REQUIRED_IFACENAME_HASH["$D1Q_IFACE_TMP"."$VLAN"]="$D1Q_IFACE_TMP"."$VLAN" 
				fi
			done
		done
		for LXCIF_N_BRIDGE in ${LXC_BRIDGE_SLAVES[@]}; do
			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"
			IS_SLAVE=0
			
			BRIDGE_IN_USE=0
			IFACE_IN_USE=0
			VLAN_N_BRIDGE_IN_USE=0
			if [[ "${REQUIRED_FULLNAME_HASH[$LINE]}" != "" ]]; then
				BRIDGE_IN_USE=1
				IFACE_IN_USE=1
				VLAN_N_BRIDGE_IN_USE=1
			else
				[[ "$OLD_BRNAME" != "" ]] &&
					if [[ "${REQUIRED_BRNAME_HASH[$OLD_BRNAME]}" != "" ]]; then
						BRIDGE_IN_USE=1
					fi
				[[ "$OLD_VLANNAME" != "" ]] &&
					if [[ "${REQUIRED_IFACENAME_HASH[$OLD_VLANNAME]}" != "" ]]; then
						IFACE_IN_USE=1
					fi
			fi
				
			if [[ "$BRIDGE_IN_USE" = 0 ]]; then
				[[ "$OLD_BRNAME" != '' ]] &&
					ip link delete "$OLD_BRNAME"
			fi
			if [[ "$IFACE_IN_USE" = 0 ]]; then
				if [[ "$OLD_VLANNAME" != '' ]]; then
					ip link set    "$OLD_VLANNAME" nomaster
					ip link delete "$OLD_VLANNAME"
				fi
			fi
		done

		#CREATING OF NECESSARY D1Q

		for D1Q_IFACE in ${D1Q_IFACES[@]}; do

			# Fixing bond-ifaces if exists
			SLAVES_VARIABLE="${D1Q_IFACE}_SLAVES"
			SLAVES_VARIABLE="${SLAVES_VARIABLE//[.]/_}[@]"
			MODE_VARIABLE="${D1Q_IFACE}_MODE"
			MODE="${!MODE_VARIABLE}"; [[ "$MODE" = "" ]] && MODE="$DEFAULT_BONDING_MODE"
			D1Q_EXISTS=0
			for OLD_D1Q_IFACE in ${OLD_D1Q_IFACES[@]}; do
				if [[ "$D1Q_IFACE" == "$OLD_D1Q_IFACE" ]]; then #it's here. Let's fix it.
					OLD_SLAVES="`cat /sys/class/net/"$OLD_D1Q_IFACE"/bonding/slaves`"
					OLD_BONDING_MODE=(`cat /sys/class/net/"$OLD_D1Q_IFACE"/bonding/mode`)

					if [ "${OLD_BONDING_MODE[0]}" != "${MODE}" -a "${OLD_BONDING_MODE[1]}" != "${MODE}" ]; then
						for OLD_SLAVE in ${OLD_SLAVES[@]}; do
							echo "-$OLD_SLAVE" > /sys/class/net/"$OLD_D1Q_IFACE"/bonding/slaves
						done
						ip link set dev "$OLD_D1Q_IFACE" down
						echo "$MODE" > /sys/class/net/"$OLD_D1Q_IFACE"/bonding/mode
						OLD_SLAVES=()
					fi

					for SLAVE in "${!SLAVES_VARIABLE}"; do
						SLAVE_EXISTS=0
						for OLD_SLAVE in ${OLD_SLAVES[@]}; do
							if [[ "$SLAVE" == "$OLD_SLAVE" ]]; then
								SLAVE_EXISTS=1
								break;
							fi	
						done
						if [[ "$SLAVE_EXISTS" == 0 ]]; then
							ip link set dev "$SLAVE" down #enslaved int should be shutted down
							echo "+$SLAVE" > /sys/class/net/"$D1Q_IFACE"/bonding/slaves
						fi
					done
					D1Q_EXISTS=1
					break;
				fi
			done

			# Creating bond-ifaces
			if [[ "$D1Q_EXISTS" == 0 ]]; then #make all
				echo "+$D1Q_IFACE" > /sys/class/net/bonding_masters
				echo "$MODE" > /sys/class/net/"$D1Q_IFACE"/bonding/mode

				for SLAVE in "${!SLAVES_VARIABLE}"; do
					ip link set dev "$SLAVE" down  #enslaved int should be shutted down
					echo "+$SLAVE" > /sys/class/net/"$D1Q_IFACE"/bonding/slaves
				done

			fi

			ip link set dev "$D1Q_IFACE" up

			for SLAVE in "${!SLAVES_VARIABLE}"; do #slave starts after bond
				ip link set dev "$SLAVE" up
			done

			VLAN_N_BRIDGE_VARIABLE="${D1Q_IFACE}_VLAN_N_BRIDGES"
			VLAN_N_BRIDGE_VARIABLE="${VLAN_N_BRIDGE_VARIABLE//[.]/_}[@]"
	
			#CREATING NECESSARY BRIDGES AND VLANS

			for VLAN_N_BRIDGE in "${!VLAN_N_BRIDGE_VARIABLE}"; do
				parse_vlan_subvlan_bridge "${VLAN_N_BRIDGE}"

				FULL_VLAN_NAME="$D1Q_IFACE"."$VLANNAME"

				#VLAN STUFF

				if  [[ "$VLAN" != "" ]]; then
					if ! iface_isexists "$FULL_VLAN_NAME"; then #should we create a new vlan?
						if [[ "$SUBVLAN" != "" ]]; then #is it subvlan?
							FULL_PARVLAN_NAME="$D1Q_IFACE"."$VLAN"

							if ! iface_isexists "$FULL_PARVLAN_NAME"; then #should we create a new parent vlan?
								ip link add link "$D1Q_IFACE" name "$FULL_PARVLAN_NAME" type vlan id "$VLAN" #vlan in bond
								ip link set dev "$FULL_PARVLAN_NAME" up
							fi
							ip link add link "$FULL_PARVLAN_NAME" name "$FULL_VLAN_NAME" type vlan id "$SUBVLAN" #subvlan in vlan
							ip link set dev "$FULL_VLAN_NAME" mtu 1496
							ip link set dev "$FULL_VLAN_NAME" up
						else
							ip link add link "$D1Q_IFACE" name "$FULL_VLAN_NAME" type vlan id "$VLANNAME" #vlan in bond
							ip link set dev "$FULL_VLAN_NAME" up
						fi
					fi

					ip_cmds "$FULL_VLAN_NAME"
				fi

				#BRIDGE STUFF

				if [[ "$BRNAME" != "" ]]; then

					if ! iface_isexists "$BRNAME"; then #do this bridge exists?
						ip link add "$BRNAME" type bridge
						echo "$BRIDGE_FORWARD_DELAY" > /sys/devices/virtual/net/"$BRNAME"/bridge/forward_delay
						ip link set dev "$BRNAME" up
					fi

					if [[ "$VLAN" != "" ]]; then
						OLD_BRNAME="`get_brname_byslave "$FULL_VLAN_NAME"`"
						if [[ "$OLD_BRNAME" = "" ]]; then #is this vlan have been already enslaved?
							ip link set "$FULL_VLAN_NAME" master "$BRNAME"
						else
							if [[ "$BRNAME" != "$OLD_BRNAME" ]]; then
								ip link set "$FULL_VLAN_NAME" nomaster
								ip link set "$FULL_VLAN_NAME" master "$BRNAME"
							fi
						fi
					fi

					ip_cmds "$BRNAME"
				fi
			done
		done

		if [[ "$LXC_DIR" != "" ]]; then

			# CREATING LXC IFACES
			for CONTAINER in $(cat "$LXC_DIR"/*/config | tr -d " \t" | awk -F '=' '{if($1=="lxc.utsname") print $2}'); do
				CONTAINER_STATUS=$(lxc-info -n "$CONTAINER" | awk '{if ($1 == "State:") { print $2; exit }}')

				# Skip the container if it's not running on this host

				[[ "$CONTAINER_STATUS" != "RUNNING" ]] &&
					continue

				# Getting an already set up interfaces
				unset CONTAINER_CURRENT_IFACES
				declare -A CONTAINER_CURRENT_IFACES
				#for IFACE in $(lxc-info -n "$CONTAINER" | awk '{if ($1 == "Link:") print $2 }'); do
				for IFACE in $(lxc-attach -n "$CONTAINER" -- ls /sys/devices/virtual/net/); do
					CONTAINER_CURRENT_IFACES["$IFACE"]="$IFACE"
				done

				# Getting an interfaces that _should_ be set up
				CONTAINER_CONFIG_IFACES=($(cat "$LXC_DIR"/"$CONTAINER"/config | tr -d " \t" | awk -F '=' '{if($1=="lxc.network.veth.pair") {HNAME=$2}; if($1=="lxc.network.name") {GNAME=$2}; if ($1 == "lxc.network.type" && HNAME!="" && GNAME!="") {print HNAME":"GNAME; HNAME=""; GNAME=""} } END { if(HNAME!="" && GNAME!="") print HNAME":"GNAME }'))

				LXC_PID=''
				for CONTAINER_IF_N_BRIDGE in ${CONTAINER_CONFIG_IFACES[@]}; do
					WORDS=( ${CONTAINER_IF_N_BRIDGE//[:]/ } )
					HNAME="${WORDS[0]}"
					GNAME="${WORDS[1]}"

					# Skip if the interface is already exists
					[[ "${CONTAINER_CURRENT_IFACES[$GNAME]}" != '' ]] &&
						continue

					# Getting a pid of container's init
					[[ "$CONTAINER_PID" == '' ]] &&
						CONTAINER_PID=$(lxc-info -p -n "$CONTAINER" | awk '{ print $2 }')

					ip link add name "$HNAME" type veth peer name ".c$GNAME"
					ip link set "$HNAME" up
					ip link set ".c$GNAME" netns "$CONTAINER_PID"
					lxc-attach -n "$CONTAINER" ip link set ".c$GNAME" name "$GNAME"
					lxc-attach -n "$CONTAINER" ip link set "$GNAME" up
					lxc-attach -n "$CONTAINER" ifup "$GNAME"
				done
			done

			# ADDING LXC IFACES TO BRIDGES IF REQUIRED
			for LXCIF_N_BRIDGE in ${LXC_BRIDGE_SLAVES[@]}; do
				parse_vlan_subvlan_bridge "$LXCIF_N_BRIDGE"
				brctl addif "$BRNAME" "$VLANNAME" 2>/dev/null
			done
		fi

		;;
	*)
		;;
esac

newconfig_open
newconfig_write "#"
newconfig_write ""
newconfig_write "D1Q_IFACES=("

for D1Q_IFACE in ${D1Q_IFACES[@]}; do
	newconfig_write	"	$D1Q_IFACE"

	newconfig_push 128 "${D1Q_IFACE}_SLAVES=("

	haveaslave=0
	SLAVES_VARIABLE="${D1Q_IFACE}_SLAVES"
	SLAVES_VARIABLE="${SLAVES_VARIABLE//[.]/_}[@]"
	for SLAVE in "${!SLAVES_VARIABLE}"; do
		newconfig_push 128 "	$SLAVE";
		haveaslave=1
	done

	newconfig_push 128 ")"

	# If there're no slaves then don't write anything about them
	[[ $haveaslave -ne 0 ]] && newconfig_pushpopall 1 128 || newconfig_popall 128

	VLAN_N_BRIDGE_VARIABLE="${D1Q_IFACE}_VLAN_N_BRIDGES"
	VLAN_N_BRIDGE_VARIABLE="${VLAN_N_BRIDGE_VARIABLE//[.]/_}[@]"
	
	newconfig_push 1 "${D1Q_IFACE}_VLAN_N_BRIGES=("
	
	for VLAN_N_BRIDGE in "${!VLAN_N_BRIDGE_VARIABLE}"; do
		newconfig_push 1 "	${VLAN_N_BRIDGE}"

		parse_vlan_subvlan_bridge "${VLAN_N_BRIDGE}"

		# Checking if no "vlan(.subvlan)?" is set
		if [[ "${BRNAME}" != "" ]]; then
			#echo $BRNAME
			# Getting commands for "ip"
			IP_CMDS_VARIABLE="${BRNAME//[.]/_}_IP"
			newconfig_push 128 "${IP_CMDS_VARIABLE}=("
			IP_CMDS_VARIABLE="${IP_CMDS_VARIABLE}[@]"
			hasanIP_CMD=0
			for IP_CMD in "${!IP_CMDS_VARIABLE}"; do
				newconfig_push 128 "	\"$IP_CMD\""
				#echo ip $IP_CMD dev $BRNAME
				hasanIP_CMD=1
			done

			newconfig_push 128 ")"
			newconfig_push 128 ""

			[[ $hasanIP_CMD -ne 0 ]] && newconfig_pushpopall 2 128 || newconfig_popall 128
		else

			# Getting commands for "ip"
			#echo OK
			IP_CMDS_VARIABLE="vlan${VLANNAME//[.]/_}_IP"
			newconfig_push 128 "${IP_CMDS_VARIABLE}=("
			IP_CMDS_VARIABLE="${IP_CMDS_VARIABLE}[@]"
			hasanIP_CMD=0
			for IP_CMD in "${!IP_CMDS_VARIABLE}"; do
				newconfig_push 128 "	\"$IP_CMD\""
				#echo ip $IP_CMD dev $VLANNAME
				hasanIP_CMD=1
			done

			newconfig_push 128 ")"
			newconfig_push 128 ""

			[[ $hasanIP_CMD -ne 0 ]] && newconfig_pushpopall 2 128 || newconfig_popall 128
			
		fi

		printf "br: %-16s vlan:%5s subvlan:%5s\n" "$BRNAME" "$VLAN" "$SUBVLAN"
	done
	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 ")"

newconfig_popallwrite 1
newconfig_popallwrite 2
newconfig_popallwrite 3

#[[ $NEWCONFIG -ne 0 ]] && newconfig_commit

exit 0