#!/bin/sh

set -e

if ! [ -r /etc/ikvswitch/ikvswitch.conf ] ; then
        echo "Could not read configuration file: exiting."
        exit 1
fi
# The user-owned values (MY_IP, DHCP_DOMAIN_NAME, DHCP_DNS_SERVERS,
# DEBIAN_MIRROR, DEBIAN_SECURITY_MIRROR, VM_ROOT_PASS) are sourced
# BEFORE the stock configuration file, so that the derived variables
# of ikvswitch.conf are evaluated with them. They live only in the
# local file, which is mandatory: it is sourced first, and the
# scripts refuse to run without it.
if [ -r /etc/ikvswitch/ikvswitch-local.conf ] ; then
        . /etc/ikvswitch/ikvswitch-local.conf
else
        echo "Could not read /etc/ikvswitch/ikvswitch-local.conf: exiting."
        exit 1
fi
if ! [ -r /etc/ikvswitch/ikvswitch.conf ] ; then
        echo "Could not read configuration file: exiting."
        exit 1
fi
. /etc/ikvswitch/ikvswitch.conf

ME=$(basename $0)

# The switch template customization mounts the partition of a raw file
# through a loop device. If anything fails, or if the script gets
# killed, in the middle of it, clean what is left behind: otherwise,
# the next run of this script would fail as well, because of the busy
# loop device and the mounted partition.
ikvswitch_cleanup_mounted_partition () {
	if [ -n "${MOUNT_DIR:-}" ] && [ -d "${MOUNT_DIR}" ] ; then
		umount ${MOUNT_DIR} 2>/dev/null || true
		rm -rf ${MOUNT_DIR}
	fi
	if [ -n "${LOOP_DEVICE:-}" ] ; then
		kpartx -d /dev/${LOOP_DEVICE} 2>/dev/null || true
	fi
	if [ -n "${LOOPRAW_DEVICE:-}" ] ; then
		losetup -d /dev/${LOOPRAW_DEVICE} 2>/dev/null || true
	fi
	MOUNT_DIR=""
	LOOP_DEVICE=""
	LOOPRAW_DEVICE=""
}
trap ikvswitch_cleanup_mounted_partition EXIT
trap 'ikvswitch_cleanup_mounted_partition ; exit 130' INT
trap 'ikvswitch_cleanup_mounted_partition ; exit 143' TERM

if [ -r /root/.ssh/authorized_keys ] ; then
	ROOT_SSH_KEY_PARAM="--root-ssh-key /root/.ssh/authorized_keys"
else
	echo "Could not read /root/.ssh/authorized_keys, please make one."
	exit 1
fi

HOST_IPMI_PORT=9000
GUEST_IPMI_CHART_PORT=9100

mac_add () {
  if [ -z "$1" ]; then
    echo "Missing MAC address in mac_add"
    exit 1
  fi
  if [ -z "$2" ]; then
    echo "Missing Increment value in mac_add"
    exit 1
  fi
  MAC_ADDR="$1"
  VALUE="$2"

  printf "%X\n" $((0x${MAC_ADDR} + VALUE))
}

ikvswitch_build_template () {
	# Auto-login root on the VGA console (reachable over VNC) and on
	# the serial console (reachable over the IPMI SOL) of the switch
	# VMs: the consoles are only reachable from the host, and typing
	# the root password on each of them is tedious.
	build-openstack-debian-image \
		--release ${DEBIAN_RELEASE} \
		--output ikvswitch-template \
		--debootstrap-url ${DEBIAN_MIRROR} \
		--sources.list-mirror ${DEBIAN_MIRROR} \
		--security-mirror ${DEBIAN_SECURITY_MIRROR} \
		--image-size 10 \
		--extra-packages frr,bind9-host,joe,rsyslog,most,lldpd,bridge-utils,vlan,bash-completion \
		--password ${VM_ROOT_PASS} \
		--no-cloud-init \
		${ROOT_SSH_KEY_PARAM} \
		--no-remove-host-keys \
		--permit-ssh-as-root \
		--tty-autologin yes \
		--add-serial-getty ttyS0 \
		--static-iface type=normal,iface0=enp0s4,addr=${HOST_VIRTUAL_SUBNET_PREFIX}.3/24:${HOST_VIRTUAL_SUBNET_PREFIX}.1 \
		--no-qcow2 \
		--newer-qemu
}

# Params: $1: name of the disk image to generate
ikvswitch_customize_images () {
	local NAME IP_ADDRESS RESULT_KPARTX MOUNT_DIR LOOP_DEVICE LOOPRAW_DEVICE DO_VLAN_SETUP REDISTRIBUTE_VLAN_SUBNETS SPEAKER_PEERS

	DHCP_SERVER_SUBNET=""
	DO_VLAN_SETUP="no"
	for i in $@ ; do
	case "${1}" in
	"--hostname")
		NAME=${2}
		shift
		shift
	;;
	"--network-style")
		# This can be "SPINE", "INTERNET" or "LEAF"
		NETWORK_STYLE=${2}
		shift
		shift
	;;
	"--spine-loopback-ip")
		SPINE_LOOPBACK_IP=${2}
		shift
		shift
	;;
	"--ip-address")
		IP_ADDRESS=${2}
		shift
		shift
	;;
	"--gw")
		GATEWAY=${2}
		shift
		shift
	;;
	"--as")
		AS=${2}
		shift
		shift
	;;
	"--dhcpd")
		DHCP_SERVER_SUBNET=${2}
		shift
		shift
	;;
	"--vlan-setup")
		DO_VLAN_SETUP="yes"
		shift
	;;
	"--map-redistribute-vlan-subnets")
		REDISTRIBUTE_VLAN_SUBNETS="${2}"
		shift
		shift
	;;
	esac
	done

	# Copy the VM template
	echo "===> Customizing template for virtual switch: ${NAME}"
	echo "cp /var/lib/ikvswitch/templates/ikvswitch-template.raw ./${NAME}.raw"
	cp /var/lib/ikvswitch/templates/ikvswitch-template.raw ./${NAME}.raw
	cd ../runtime
	# Mount the drive
	RESULT_KPARTX=$(kpartx -asv ${NAME}.raw 2>&1)
	if echo "${RESULT_KPARTX}" | grep "^add map" ; then
		LOOP_DEVICE=$(echo ${RESULT_KPARTX} | cut -d" " -f3)
		LOOPRAW_DEVICE=${LOOP_DEVICE%p*}
		echo "kpartx mounted using: ${LOOP_DEVICE} via ${LOOPRAW_DEVICE}"
	else
		echo "It seems kpartx didn't mount the image correctly: exiting."
		exit 1
	fi
	MOUNT_DIR=$(mktemp -d -t ${ME}-switch-mounted-partition.XXXXXX)
	mount /dev/mapper/${LOOP_DEVICE} ${MOUNT_DIR}
	# Start customizing
	echo "${NAME}" >${MOUNT_DIR}/etc/hostname
	sed -i s/bgpd=no/bgpd=yes/ ${MOUNT_DIR}/etc/frr/daemons

	# make sure we're ip forwarding
        echo "net.ipv4.ip_forward=1" >${MOUNT_DIR}/etc/sysctl.d/00-forward-internet.conf
        echo "net.ipv6.conf.all.forwarding=1" >>${MOUNT_DIR}/etc/sysctl.d/00-forward-internet.conf

	# Disable reverse path filtering on every switch VM, as neutron's
	# own BGP CI does (tempest-multinode-with-bgp, roles/leaf and
	# roles/spine): the kernel's source validation (rp_filter >= 1,
	# loose mode included) drops the asymmetric return traffic of
	# BGP-2-the-host floating IPs whenever the chassis advertising
	# the /32 host route differs from the one egressing the reply.
	# Named 99- so that it applies after the stock Debian files
	# setting it to 2: 10-network-security.conf
	# (default.rp_filter=2) and, since systemd 257, 50-default.conf
	# which also carries "net.ipv4.conf.*.rp_filter=2" - a glob that
	# is re-expanded at EVERY sysctl application (the boot one or a
	# manual "sysctl --system") and re-writes 2 into every interface
	# existing at that moment, behind the back of anything applied
	# earlier. sysctl.d precedence is lexicographic and the latest
	# file wins a key, so this file's keys win: all/default at 0 for
	# the interfaces created after the sysctl run, and the same
	# per-interface glob at 0 so any glob application ends at 0
	# instead of 2 regardless of the ordering race between
	# systemd-sysctl.service and networking.service (proven on the
	# PoC: leaves booted with the correct stanza hook still carried
	# rp_filter=2 on VLAN subifs three days later). Each iface stanza
	# of the /etc/network/interfaces files generated below therefore
	# also carries an "ip-rp-filter" option, honored by the ifupdown
	# package's /etc/network/if-up.d/ip hook at the exact moment the
	# interface is brought up, as a second belt (an interface raised
	# after the sysctl run inherits default=0 and is re-written to
	# the knob value). The value defaults to 0 (the asymmetric
	# return traffic of BGP-2-the-host floating IPs requires it); set
	# IFACE_RP_FILTER to 1 or 2 for strict/loose, or to an empty value
	# to not emit the option at all (the pre-knob behavior, which
	# leaves the switch interfaces at their boot-time rp_filter=2).
	# The rp_filter sysctls are IPv4-only by design: Linux has no
	# net.ipv6.*.rp_filter at all (IPv6 uRPF was never implemented
	# in the kernel, and nothing on these systems ships "ip6tables
	# -m rpfilter" rules either), so the asymmetric-return trap
	# described below can never happen for IPv6: there is nothing
	# to tweak on the v6 side. This is written into the file
	# itself, so that nobody tries to "fix" it later.
	echo "# The rp_filter sysctls are IPv4-only by design: Linux has no
# net.ipv6.*.rp_filter (IPv6 uRPF was never implemented in the
# kernel, and nothing ships ip6tables -m rpfilter rules either), so
# the asymmetric-return traffic of BGP-2-the-host can never be
# dropped for IPv6: there is nothing to tweak on the v6 side.
net.ipv4.conf.all.rp_filter=0" >${MOUNT_DIR}/etc/sysctl.d/99-rp-filter.conf
	echo "net.ipv4.conf.default.rp_filter=0" >>${MOUNT_DIR}/etc/sysctl.d/99-rp-filter.conf
	echo "net.ipv4.conf.*.rp_filter=0" >>${MOUNT_DIR}/etc/sysctl.d/99-rp-filter.conf

	IFACE_RP_FILTER=${IFACE_RP_FILTER-0}
	if [ -n "${IFACE_RP_FILTER}" ] ; then
		IP_RP_FILTER_LINE="	ip-rp-filter ${IFACE_RP_FILTER}
"
	else
		IP_RP_FILTER_LINE=""
	fi

	# Configure FRR and /etc/network/interfaces
	###############################
	### SPINE OR INTERNET NODES ###
	###############################
	if [ "${NETWORK_STYLE}" = "SPINE" ] || [ "${NETWORK_STYLE}" = "INTERNET" ] ; then
		if [ "${NETWORK_STYLE}" = "INTERNET" ] ; then
			# The link to the host server is dual-stack: the IPv6
			# side carries HOST_VIRTUAL_SUBNET_NET1_V6_INTERNET,
			# with HOST_VIRTUAL_SUBNET_NET1_V6_GW (the host
			# bridge) as its default gateway, so the internet
			# switch holds a v6 default route exactly like its v4
			# one, redistributed below into the whole fabric
			# through BGP. Both addresses are defined in
			# ikvswitch.conf, never composed here.
			IP_STUFF="auto enp0s4
iface enp0s4 inet static
${IP_RP_FILTER_LINE}	address ${IP_ADDRESS}
	netmask 255.255.255.0
	gateway ${GATEWAY}

iface enp0s4 inet6 static
${IP_RP_FILTER_LINE}	address ${HOST_VIRTUAL_SUBNET_NET1_V6_INTERNET}/64
	gateway ${HOST_VIRTUAL_SUBNET_NET1_V6_GW}
"
		else
			IP_STUFF="auto enp0s4
iface enp0s4 inet manual
${IP_RP_FILTER_LINE}	metric 4278198272
"
		fi

		echo "# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback
${IP_RP_FILTER_LINE}
${IP_STUFF}

auto enp0s5
iface enp0s5 inet manual
${IP_RP_FILTER_LINE}	metric 4278198272

auto enp0s6
iface enp0s6 inet manual
${IP_RP_FILTER_LINE}	metric 4278198272
" >${MOUNT_DIR}/etc/network/interfaces
		if [ "${NETWORK_STYLE}" = "SPINE" ] ; then
			echo "auto enp0s7
iface enp0s7 inet manual
${IP_RP_FILTER_LINE}	metric 4278198272

auto enp0s8
iface enp0s8 inet manual
${IP_RP_FILTER_LINE}	metric 4278198272

auto enp0s9
iface enp0s9 inet manual
${IP_RP_FILTER_LINE}	metric 4278198272

auto enp0s10
iface enp0s10 inet manual
${IP_RP_FILTER_LINE}	metric 4278198272

# Set this one last, so that cloud-init or user can
# override defaults.
source /etc/network/interfaces.d/*
" >>${MOUNT_DIR}/etc/network/interfaces
		fi
		echo "log syslog informational

!
int lo
 ip address ${SPINE_LOOPBACK_IP}/32
!
router bgp ${AS}
 bgp router-id ${IP_ADDRESS}
 no bgp default ipv4-unicast
 bgp bestpath as-path multipath-relax
 bgp bestpath compare-routerid
 neighbor leaf peer-group
 neighbor leaf remote-as external
 neighbor leaf capability extended-nexthop
 neighbor spine peer-group
 neighbor spine remote-as external
 neighbor spine capability extended-nexthop
 neighbor enp0s4 interface peer-group spine
 neighbor enp0s5 interface peer-group leaf
 neighbor enp0s6 interface peer-group leaf" >${MOUNT_DIR}/etc/frr/frr.conf
		if [ "${NETWORK_STYLE}" = "SPINE" ] ; then
			echo " neighbor enp0s7 interface peer-group leaf
 neighbor enp0s8 interface peer-group leaf
 neighbor enp0s9 interface peer-group leaf
 neighbor enp0s10 interface peer-group leaf" >>${MOUNT_DIR}/etc/frr/frr.conf
 		fi
echo "!
 address-family ipv4 unicast
  redistribute kernel route-map map-redistribute
  redistribute connected route-map map-redistribute
  redistribute static route-map map-redistribute
  neighbor leaf activate
  neighbor leaf soft-reconfiguration inbound
  neighbor leaf route-map map-leaf-in in
  neighbor leaf route-map map-leaf-out out
  neighbor spine activate
  neighbor spine soft-reconfiguration inbound
  neighbor spine route-map map-spine-in in
  neighbor spine route-map map-spine-out out
 exit-address-family
 !
 address-family ipv6 unicast
  # Export the kernel default route too: the permit 30 clause of
  # map-redistribute matches it, and without it the guest machines
  # would never receive a usable ::/0 from the fabric.
  redistribute kernel route-map map-redistribute
  redistribute connected route-map map-redistribute
  redistribute static route-map map-redistribute
  neighbor leaf activate
  neighbor leaf soft-reconfiguration inbound
  neighbor leaf route-map map-leaf-in in
  neighbor leaf route-map map-leaf-out out
  neighbor spine activate
  neighbor spine soft-reconfiguration inbound
  neighbor spine route-map map-spine-in in
  neighbor spine route-map map-spine-out out
 exit-address-family
exit
!
ip prefix-list pl-default seq 5 permit 0.0.0.0/0
!
ipv6 prefix-list pl-default6 seq 5 permit ::/0
!
ipv6 prefix-list pl-delegated6 seq 5 permit ${HOST_VIRTUAL_SUBNET_NET1_V6}::/59
!
route-map map-spine-out permit 10
exit
!
route-map map-spine-in permit 10
exit
!
route-map map-leaf-out permit 10
exit
!
route-map map-redistribute permit 10
 match interface lo
exit
!
route-map map-redistribute permit 20
 match ip address prefix-list pl-default
exit
!
# The v6 mirror of permit 20: only the internet switch holds a static
# ::/0 (via the host server), and redistributing it into the ipv6
# unicast address family gives a default route to every spine, leaf
# and BGP-2-the-host node of the fabric.
route-map map-redistribute permit 30
 match ipv6 address prefix-list pl-default6
exit
!
# The delegated host block, held as a static route by the internet
# switch only (see the ipv6 route below): redistributing it gives the
# whole fabric a specific route for every address owned by the host
# server, so traffic to the gateway's own space (the rack management
# networks' gateways, the headroom) is routed via the internet
# switch's host link instead of bouncing on the ::/0 relay.
route-map map-redistribute permit 40
 match ipv6 address prefix-list pl-delegated6
exit
!
route-map map-leaf-in permit 10
exit
!
" >>${MOUNT_DIR}/etc/frr/frr.conf
	if [ "${NETWORK_STYLE}" = "INTERNET" ] ; then
		# The static route for the delegated host block, pointing at
		# the host bridge's gateway address. Being an FRR static, the
		# "redistribute static" above picks it up and exports it into
		# the fabric, so the leafs and spines learn the whole block
		# rather than reaching the gateway's space through the ::/0.
		# It is anchored on HOST_VIRTUAL_SUBNET_NET1_V6, which carries
		# the trailing nibble of the block: a /59 built from the bare
		# base would not be canonical, and the kernel would silently
		# mask it down to the wrong block.
		echo "ipv6 route ${HOST_VIRTUAL_SUBNET_NET1_V6}::/59 ${HOST_VIRTUAL_SUBNET_NET1_V6_GW}" >>${MOUNT_DIR}/etc/frr/frr.conf
	fi
	##################
	### LEAF NODES ###
	##################
	else
		# The 2 interfaces connected to the spines switches
		echo "# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback
${IP_RP_FILTER_LINE}

auto enp0s4
iface enp0s4 inet manual
${IP_RP_FILTER_LINE}

auto enp0s5
iface enp0s5 inet manual
${IP_RP_FILTER_LINE}
" >${MOUNT_DIR}/etc/network/interfaces

		# N interfaces for connecting servers
		ENS_END=$(( 6 + ${NUM_U} - 3 ))
		for i in $(seq 6 ${ENS_END}) ; do
			echo "auto enp0s${i}
iface enp0s${i} inet manual
${IP_RP_FILTER_LINE}	mtu 9000
	metric 4278198272

auto enp0s${i}.${LEAF_TO_VM_VLAN_NUM}
iface enp0s${i}.${LEAF_TO_VM_VLAN_NUM} inet manual
${IP_RP_FILTER_LINE}	mtu 9000

" >>${MOUNT_DIR}/etc/network/interfaces
			if [ -n "${LEAF_TO_VM_CHASSIS_VLAN_NUM:-}" ] ; then
				echo "auto enp0s${i}.${LEAF_TO_VM_CHASSIS_VLAN_NUM}
iface enp0s${i}.${LEAF_TO_VM_CHASSIS_VLAN_NUM} inet manual
${IP_RP_FILTER_LINE}	mtu 9000

" >>${MOUNT_DIR}/etc/network/interfaces
			fi
			if [ "${DO_VLAN_SETUP}" = "yes" ] ; then
				VLAN_END=$(( ${VLAN_START} + ${VLAN_SUBNETS_NUM} - 1 ))
				for j in $(seq ${VLAN_START} ${VLAN_END}) ; do
					echo "auto enp0s${i}.${j}
iface enp0s${i}.${j} inet manual
${IP_RP_FILTER_LINE}	mtu 9000

" >>${MOUNT_DIR}/etc/network/interfaces
				done
			fi
		done
		####################################################
		### Setup the bridges for the VLANs (leaf1 only) ###
		####################################################
		if [ "${DO_VLAN_SETUP}" = "yes" ] ; then
			VLAN_END=$(( ${VLAN_START} + ${VLAN_SUBNETS_NUM} - 1 ))
			SUBNET_SUFFIX=${VLAN_SBUNETS_START_IP}
			for j in $(seq ${VLAN_START} ${VLAN_END}) ; do
				MY_VLAN_SUBNET=${HOST_VIRTUAL_SUBNET_START_PREFIX}.${SUBNET_SUFFIX}
				IFACE_LIST=""
				ENS_END=$(( 6 + ${NUM_U} - 3 ))
				for IF in $(seq 6 ${ENS_END}) ; do
					IFACE_LIST="${IFACE_LIST} enp0s${IF}.${j}"
				done
				echo "auto brvlan${j}
iface brvlan${j} inet static
${IP_RP_FILTER_LINE}        bridge_ports ${IFACE_LIST}
        address ${MY_VLAN_SUBNET}.1
        broadcast ${MY_VLAN_SUBNET}.255
        netmask 255.255.255.0
" >>${MOUNT_DIR}/etc/network/interfaces

				SUBNET_SUFFIX=$(( ${SUBNET_SUFFIX} + 1 ))
			done
		fi
		# end of /etc/network/interfaces
		echo "# Set this one last, so that cloud-init or user can
# override defaults.
source /etc/network/interfaces.d/*
" >>${MOUNT_DIR}/etc/network/interfaces

		echo "log syslog informational

!
interface lo
 ip address ${IP_ADDRESS}/32
!" >${MOUNT_DIR}/etc/frr/frr.conf

		# The blackhole statics backing the "network" statements of the
		# BGP block must be written BEFORE it: bgpd stops reading
		# frr.conf when it meets, inside the "router bgp" context, a
		# command it doesn't own (ip route belongs to staticd), which
		# silently drops everything that follows: the leaf peer-group
		# members, both address-families, the network statements and
		# the route-maps, leaving BGP unable to peer with the spines.
		for MGMT_SUBNET in ${CLUSTER_MGMT_SUBNETS} ; do
			echo "ip route ${MGMT_SUBNET} blackhole
!" >>${MOUNT_DIR}/etc/frr/frr.conf
		done

		echo "router bgp ${AS}
 bgp router-id ${IP_ADDRESS}
 no bgp default ipv4-unicast
 bgp bestpath as-path multipath-relax
 bgp bestpath compare-routerid
 neighbor leaf peer-group
 neighbor leaf remote-as external
 neighbor leaf capability extended-nexthop
 neighbor spine peer-group
 neighbor spine remote-as external
 neighbor spine capability extended-nexthop
 neighbor enp0s4 interface peer-group spine
 neighbor enp0s5 interface peer-group spine" >>${MOUNT_DIR}/etc/frr/frr.conf

		ENS_END=$(( 6 + ${NUM_U} - 3 ))
		for IF in $(seq 6 ${ENS_END}) ; do
			echo " neighbor enp0s${IF}.${LEAF_TO_VM_VLAN_NUM} interface peer-group leaf" >>${MOUNT_DIR}/etc/frr/frr.conf
			if [ -n "${LEAF_TO_VM_CHASSIS_VLAN_NUM:-}" ] ; then
				echo " neighbor enp0s${IF}.${LEAF_TO_VM_CHASSIS_VLAN_NUM} interface peer-group leaf" >>${MOUNT_DIR}/etc/frr/frr.conf
			fi
		done

		for VLAN_NEIGH in $(seq ${VLAN_START} $(( ${VLAN_START} + ${VLAN_SUBNETS_NUM} - 1 )) ) ; do
			echo " neighbor brvlan${VLAN_NEIGH} interface peer-group leaf" >>${MOUNT_DIR}/etc/frr/frr.conf
		done


		# The speaker peer prefixes may be missing from configuration
		# files predating them: guard each group, so that an undefined
		# VLAN_SBUNETn_PREFIX can't emit malformed "neighbor .200"
		# statements into the FRR configuration.
		SPEAKER_PEERS=""
		if [ -n "${VLAN_SBUNET1_PREFIX:-}" ] ; then
			SPEAKER_PEERS="${VLAN_SBUNET1_PREFIX}.200 ${VLAN_SBUNET1_PREFIX}.201"
		fi
		if [ -n "${VLAN_SBUNET6_PREFIX:-}" ] ; then
			SPEAKER_PEERS="${SPEAKER_PEERS} ${VLAN_SBUNET6_PREFIX}.200 ${VLAN_SBUNET6_PREFIX}.201 ${VLAN_SBUNET6_PREFIX}.202"
		fi
		if [ "${DO_VLAN_SETUP}" = "yes" ] ; then
			# Allow the OVS-mode BGP speaker hosts to connect and
			# advertize BGP: cl1's two network nodes on the legacy
			# speaker VLAN, and cl3's three controllers on its
			# transport VLAN.
			for SPEAKER_PEER_IP in ${SPEAKER_PEERS} ; do
				echo " neighbor ${SPEAKER_PEER_IP} remote-as external" >>${MOUNT_DIR}/etc/frr/frr.conf
			done
		fi
		echo " !
 address-family ipv4 unicast
  redistribute connected route-map map-redistribute
  redistribute static route-map map-redistribute
  neighbor leaf activate
  neighbor leaf soft-reconfiguration inbound
  neighbor leaf route-map map-leaf-in in
  neighbor leaf route-map map-leaf-out out
  neighbor spine activate
  neighbor spine soft-reconfiguration inbound
  neighbor spine route-map map-spine-in in
  neighbor spine route-map map-spine-out out" >>${MOUNT_DIR}/etc/frr/frr.conf
		if [ "${DO_VLAN_SETUP}" = "yes" ] ; then
			# Allow the OVS-mode BGP speaker hosts to connect and
			# advertize BGP: cl1's two network nodes on the legacy
			# speaker VLAN, and cl3's three controllers on its
			# transport VLAN. The peers are activated under the
			# ipv4 family only: the dragent OPEN offers both
			# families, and an ipv6 activation makes FRR reject the
			# whole session while the peering SVI carries no global
			# IPv6 address - re-enable it only once the brvlan SVIs
			# carry global IPv6.
			for SPEAKER_PEER_IP in ${SPEAKER_PEERS} ; do
				echo "  neighbor ${SPEAKER_PEER_IP} activate
  neighbor ${SPEAKER_PEER_IP} route-map vm-in in
  neighbor ${SPEAKER_PEER_IP} route-map vm-out out" >>${MOUNT_DIR}/etc/frr/frr.conf
			done
		fi
		for MGMT_SUBNET in ${CLUSTER_MGMT_SUBNETS} ; do
			echo "  network ${MGMT_SUBNET}" >>${MOUNT_DIR}/etc/frr/frr.conf
		done
		echo " exit-address-family
 !
 address-family ipv6 unicast
  redistribute connected route-map map-redistribute
  redistribute static route-map map-redistribute
  neighbor leaf activate
  neighbor leaf soft-reconfiguration inbound
  neighbor leaf route-map map-leaf-in in
  neighbor leaf route-map map-leaf-out out
  neighbor spine activate
  neighbor spine soft-reconfiguration inbound
  neighbor spine route-map map-spine-in in
  neighbor spine route-map map-spine-out out" >>${MOUNT_DIR}/etc/frr/frr.conf
		echo " exit-address-family
exit
!
route-map map-spine-out permit 10
exit
!
route-map map-spine-in permit 10
exit
!
route-map map-leaf-out permit 10
exit
!
route-map map-redistribute permit 10
 match interface lo
exit
!
ip prefix-list pl-default seq 5 permit 0.0.0.0/0
!
ipv6 prefix-list pl-default6 seq 5 permit ::/0
!
ipv6 prefix-list pl-delegated6 seq 5 permit ${HOST_VIRTUAL_SUBNET_NET1_V6}::/59
!
# The machine-facing import guard: a BGP-2-the-host machine must never
# be trusted with the fabric's default routes, nor with the delegated
# host block. A machine re-exporting what it merely learned (for
# example an OVN chassis VRF instance redistributing the kernel table
# into which a router advertisement default landed) announces a 1-AS
# path that beats the spines' 2-AS path on every leaf: the leaf's
# kernel default then points back at the machine, which loops the
# ::/0 forwarding path and kills the IPv6 egress of the whole rack.
# The v4 mirror guards the same class for machines redistributing
# their kernel table. Machines keep announcing their own /32s and
# /128s, and the leaves still relay the spines' defaults outbound
# (map-leaf-out): only the exact aggregates are denied inbound.
route-map map-leaf-in deny 10
 match ip address prefix-list pl-default
exit
!
route-map map-leaf-in deny 20
 match ipv6 address prefix-list pl-default6
exit
!
route-map map-leaf-in deny 30
 match ipv6 address prefix-list pl-delegated6
exit
!
route-map map-leaf-in permit 100
exit
!
" >>${MOUNT_DIR}/etc/frr/frr.conf
		if [ "${DO_VLAN_SETUP}" = "yes" ] ; then
			echo "route-map vm-in permit 10
exit
!
route-map vm-out permit 10
exit
!" >>${MOUNT_DIR}/etc/frr/frr.conf
		fi
		if [ -n "${DHCP_SERVER_SUBNET}" ] ; then
			SUBNET_DASH=$(echo ${DHCP_SERVER_SUBNET} | sed -e 's/\./-/g')
			echo "route-map map-redistribute permit 30
 match ip address prefix-list pl-${SUBNET_DASH}
exit
!
ip prefix-list pl-${SUBNET_DASH} seq 10 permit ${DHCP_SERVER_SUBNET}.0/24
!
" >>${MOUNT_DIR}/etc/frr/frr.conf
			if [ "${DHCP_SERVER_DAEMON}" = "dnsmasq" ] ; then
				DHCP_SERVER_PKG=dnsmasq
			else
				DHCP_SERVER_PKG=isc-dhcp-server
			fi
			echo "DEBIAN_FRONTEND=noninteractive chroot ${MOUNT_DIR} apt-get install -y -o Dpkg::Options::=--force-confold ${DHCP_SERVER_PKG}"
			DEBIAN_FRONTEND=noninteractive chroot ${MOUNT_DIR} apt-get install -y -o Dpkg::Options::=--force-confold ${DHCP_SERVER_PKG}
			if [ "${DHCP_SERVER_DAEMON}" = "dnsmasq" ] ; then
				# DHCP only, as dnsmasq would also start a
				# DNS resolver on the switch, and bind only
				# on the dhcp0 bridge.
				#
				# Note that dnsmasq matches dhcp-boot
				# entries in the reverse order of the
				# configuration file, and an iPXE client
				# also sends a value in option 93
				# (client-arch): all of the client-arch
				# boot entries are therefore guarded with
				# tag:!ipxe, so a single entry can ever
				# match a given client.
				if [ -n "${DHCP_HTTP_BOOT_URL}" ] ; then
					DHCP_HTTP_BOOT_DNSMASQ="dhcp-match=set:httpboot,option:client-arch,16
dhcp-boot=tag:!ipxe,tag:httpboot,${DHCP_HTTP_BOOT_URL}
"
				else
					DHCP_HTTP_BOOT_DNSMASQ=""
				fi
				echo "port=0
interface=dhcp0
bind-dynamic
dhcp-authoritative
dhcp-range=${DHCP_SERVER_SUBNET}.${DHCP_POOL_START},${DHCP_SERVER_SUBNET}.${DHCP_POOL_END},255.255.255.0,${DHCP_DEFAULT_LEASE_TIME}
dhcp-option=3,${DHCP_SERVER_SUBNET}.1
dhcp-option=6,${DHCP_DNS_SERVERS}
dhcp-option=15,${DHCP_DOMAIN_NAME}
dhcp-option=28,${DHCP_SERVER_SUBNET}.255
dhcp-userclass=set:ipxe,iPXE
dhcp-match=set:bios,option:client-arch,0
dhcp-match=set:efi64,option:client-arch,7
dhcp-match=set:efi64_9,option:client-arch,9
${DHCP_HTTP_BOOT_DNSMASQ}dhcp-boot=tag:!ipxe,tag:bios,${DHCP_PXE_BOOTFILE_BIOS},,${DHCP_RELAY_DESTINATION}
dhcp-boot=tag:!ipxe,tag:efi64,${DHCP_PXE_BOOTFILE_EFI},,${DHCP_RELAY_DESTINATION}
dhcp-boot=tag:!ipxe,tag:efi64_9,${DHCP_PXE_BOOTFILE_EFI},,${DHCP_RELAY_DESTINATION}
dhcp-boot=tag:ipxe,${DHCP_IPXE_BOOT_URL}
dhcp-boot=${DHCP_PXE_BOOTFILE_FALLBACK},,${DHCP_RELAY_DESTINATION}
">${MOUNT_DIR}/etc/dnsmasq.d/ikvswitch.conf
			else
				if [ -n "${DHCP_HTTP_BOOT_URL}" ] ; then
					DHCP_HTTP_BOOT_ISC="                } elsif option pxe-system-type = 00:10 {
                        filename \"${DHCP_HTTP_BOOT_URL}\";
"
				else
					DHCP_HTTP_BOOT_ISC=""
				fi
				echo "allow booting;
allow bootp;
default-lease-time ${DHCP_DEFAULT_LEASE_TIME};
max-lease-time ${DHCP_MAX_LEASE_TIME};
ddns-update-style none;
authoritative;
ignore-client-uids On;

subnet ${DHCP_SERVER_SUBNET}.0 netmask 255.255.255.0 {
        range ${DHCP_SERVER_SUBNET}.${DHCP_POOL_START} ${DHCP_SERVER_SUBNET}.${DHCP_POOL_END};
        option domain-name \"${DHCP_DOMAIN_NAME}\";
        option domain-name-servers ${DHCP_DNS_SERVERS};
        option routers ${DHCP_SERVER_SUBNET}.1;
        option subnet-mask 255.255.255.0;
        option broadcast-address ${DHCP_SERVER_SUBNET}.255;
        next-server ${DHCP_RELAY_DESTINATION};
        if exists user-class and option user-class = \"iPXE\" {
                filename \"${DHCP_IPXE_BOOT_URL}\";
        } elsif exists pxe-system-type {
                if option pxe-system-type = 00:00 {
                        filename \"${DHCP_PXE_BOOTFILE_BIOS}\";
                } elsif option pxe-system-type = 00:07 {
                        filename \"${DHCP_PXE_BOOTFILE_EFI}\";
                } elsif option pxe-system-type = 00:09 {
                        filename \"${DHCP_PXE_BOOTFILE_EFI}\";
${DHCP_HTTP_BOOT_ISC}                }
        } else {
                filename \"${DHCP_PXE_BOOTFILE_FALLBACK}\";
        }
}
">${MOUNT_DIR}/etc/dhcp/dhcpd.conf
			fi
			IFACE_LIST=""
			ENS_END=$(( 6 + ${NUM_U} - 3 ))
			for IF in $(seq 6 ${ENS_END}) ; do
				IFACE_LIST="${IFACE_LIST} enp0s${IF}"
			done
			echo "auto dhcp0
iface dhcp0 inet static
${IP_RP_FILTER_LINE}	bridge_ports ${IFACE_LIST}
	address ${DHCP_SERVER_SUBNET}.1
	broadcast ${DHCP_SERVER_SUBNET}.255
	netmask 255.255.255.0
" >>${MOUNT_DIR}/etc/network/interfaces
#			sed -i 's/INTERFACESv4=.*/INTERFACESv4="enp0s6 enp0s7 enp0s8 enp0s9 enp0s10 enp0s11 enp0s12 enp0s13 enp0s14 enp0s15 enp0s17 enp0s18"/' ${MOUNT_DIR}/etc/default/isc-dhcp-server
		fi
		if [ "${DO_VLAN_SETUP}" = "yes" ] && [ "${REDISTRIBUTE_VLAN_SUBNETS}" != "none" ] ; then
			VLAN_END=$(( ${VLAN_START} + ${VLAN_SUBNETS_NUM} - 1 ))
			SUBNET_DASH=$(echo ${HOST_VIRTUAL_SUBNET_START_PREFIX}.${VLAN_SBUNETS_START_IP} | sed -e 's/\./-/g')
			echo "route-map map-redistribute permit 31
 match ip address prefix-list pl-${SUBNET_DASH}
exit
!
" >>${MOUNT_DIR}/etc/frr/frr.conf
			SEQ=10
			if [ "${REDISTRIBUTE_VLAN_SUBNETS}" = "all" ] ; then
				SUBNET_SUFFIX=${VLAN_SBUNETS_START_IP}
				for j in $(seq ${VLAN_START} ${VLAN_END}) ; do
					MY_VLAN_SUBNET=${HOST_VIRTUAL_SUBNET_START_PREFIX}.${SUBNET_SUFFIX}
					echo " ip prefix-list pl-${SUBNET_DASH} seq ${SEQ} permit ${MY_VLAN_SUBNET}.0/24" >>${MOUNT_DIR}/etc/frr/frr.conf
					SEQ=$(( $SEQ + 1 ))
					SUBNET_SUFFIX=$(( ${SUBNET_SUFFIX} + 1 ))
				done
			else
				for VLAN_OCTET in $(echo ${REDISTRIBUTE_VLAN_SUBNETS} | tr ',' ' ') ; do
					echo " ip prefix-list pl-${SUBNET_DASH} seq ${SEQ} permit ${HOST_VIRTUAL_SUBNET_START_PREFIX}.${VLAN_OCTET}.0/24" >>${MOUNT_DIR}/etc/frr/frr.conf
					SEQ=$(( $SEQ + 1 ))
				done
			fi
			echo "!" >>${MOUNT_DIR}/etc/frr/frr.conf
		fi
	fi
	# Customize /etc/hosts, so we get all switches to resolve
	echo "127.0.0.1	localhost
${HOST_VIRTUAL_SUBNET_NET3}.1    internet
${HOST_VIRTUAL_SUBNET_NET3}.2    spine1
${HOST_VIRTUAL_SUBNET_NET3}.3    spine2
${HOST_VIRTUAL_SUBNET_NET3}.4    leaf1
${HOST_VIRTUAL_SUBNET_NET3}.5    leaf2
${HOST_VIRTUAL_SUBNET_NET3}.6    leaf3
${HOST_VIRTUAL_SUBNET_NET3}.7    leaf4
${HOST_VIRTUAL_SUBNET_NET3}.8    leaf5
${HOST_VIRTUAL_SUBNET_NET3}.9    leaf6

::1	localhost ip6-localhost ip6-loopback
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters
" >${MOUNT_DIR}/etc/hosts
	# Unmount
	umount ${MOUNT_DIR}
	rm -r ${MOUNT_DIR}
	kpartx -d ${NAME}.raw
	# The EXIT trap takes care of this as well, but with the values
	# reset here, it becomes a no-op after a successful customization.
	MOUNT_DIR=""
	LOOP_DEVICE=""
	LOOPRAW_DEVICE=""
}

ikv_spine_switch_write_ipmisim () {
	local NAME VNC_PORT HOST_IPMI_PORT GUEST_IPMI_CHART_PORT SD_PATH VM_PID_FILE FIRST_DRIVE MONITOR_PORT TAP_IFNAME MAC_END
	local OTHER_IFS_NUMS OTHER_IFS_TAP_PREFIX OTHER_IFS_TAP_SUFIX_START OTHER_IFS_MAC_END IFS_DEF OTHER_IFS_PCI_ADDR
	for i in $@ ; do
	case "${1}" in
	"--name")
		NAME=${2}
		shift
		shift
	;;
	"--vnc-port")
		VNC_PORT=${2}
		shift
		shift
	;;
	"--tap-ifname")
		TAP_IFNAME=${2}
		shift
		shift
	;;
	"--mac-end")
		MAC_END=${2}
		shift
		shift
	;;
	"--ip-address")
		IP_ADDRESS=${2}
		shift
		shift
	;;
	"--other-ifs-num")
		OTHER_IFS_NUMS=${2}
		shift
		shift
	;;
	"--other-ifs-tap-prefix")
		OTHER_IFS_TAP_PREFIX=${2}
		shift
		shift
	;;
	"--other-ifs-tap-sufix-start")
		OTHER_IFS_TAP_SUFIX_START=${2}
		shift
		shift
	;;
	"--other-ifs-mac-end")
		OTHER_IFS_MAC_END=${2}
		shift
		shift
	;;
	esac
	done

	echo "===> Spinning up ${NAME}"
	SD_PATH=/var/lib/ikvswitch/runtime/${NAME}.raw
	VM_PID_FILE=/var/lib/ikvswitch/runtime/${NAME}.pid
	FIRST_DRIVE="-device virtio-scsi-pci,id=scsi0,addr=0x3 -drive if=none,file=${SD_PATH},index=0,media=disk,format=raw,cache=none,discard=unmap,aio=native,id=drive-scsi0-0-0-0 -device scsi-hd,drive=drive-scsi0-0-0-0,bus=scsi0.0"

	HOST_IPMI_PORT=$(( ${VNC_PORT} + 9000 ))
	GUEST_IPMI_CHART_PORT=$(( ${VNC_PORT} + 9100 ))
	MONITOR_PORT=$(( 55000 + ${VNC_PORT}))

  # Generate network qemu parameters
	IFS_DEF="-device virtio-net-pci,netdev=net0,mac=${GUEST_MAC_ADDRESS_PREFIX}${MAC_END},addr=0x4 -netdev tap,id=net0,ifname=${TAP_IFNAME}"

	# Pin the PCI address of each NIC, so that the guest names its
	# interfaces enp0s4, enp0s5, ... regardless of the machine type.
	OTHER_IFS_PCI_ADDR=5
	if [ -n "${OTHER_IFS_NUMS}" ] ; then
		for i in $(seq 1 ${OTHER_IFS_NUMS}) ; do
			IFS_DEF="${IFS_DEF} -device virtio-net-pci,netdev=net${OTHER_IFS_TAP_SUFIX_START},mac=${GUEST_MAC_ADDRESS_PREFIX}${OTHER_IFS_MAC_END},addr=0x$(printf %X ${OTHER_IFS_PCI_ADDR}) -netdev tap,id=net${OTHER_IFS_TAP_SUFIX_START},ifname=${OTHER_IFS_TAP_PREFIX}${OTHER_IFS_TAP_SUFIX_START}"
			OTHER_IFS_MAC_END=$(printf "%X\n" $((0x$OTHER_IFS_MAC_END + 1)))
			OTHER_IFS_TAP_SUFIX_START=$(( $OTHER_IFS_TAP_SUFIX_START + 1 ))
			OTHER_IFS_PCI_ADDR=$(( ${OTHER_IFS_PCI_ADDR} + 1 ))
		done
	fi

        echo "name \"ipmisim1\"
set_working_mc 0x20
  startlan 1
    addr 0.0.0.0 ${HOST_IPMI_PORT}
    priv_limit admin
    allowed_auths_callback none md2 md5 straight
    allowed_auths_user none md2 md5 straight
    allowed_auths_operator none md2 md5 straight
    allowed_auths_admin none md2 md5 straight
    guid a123456789abcdefa123456789abcdef
  endlan
  serial 15 localhost ${GUEST_IPMI_CHART_PORT} codec VM
  startcmd \"qemu-system-x86_64 -enable-kvm -machine q35 -m size=512M -smp cpus=2 -cpu host,+spec-ctrl -vnc :${VNC_PORT} -monitor tcp:127.0.0.1:${MONITOR_PORT},server,nowait -pidfile ${VM_PID_FILE} -daemonize ${FIRST_DRIVE} -boot c ${IFS_DEF} -smbios type=1,manufacturer=LinuxKVM,product=qemu-oci,serial=${NAME} -smbios type=3,manufacturer=LinuxKVM,serial=${NAME} -chardev socket,id=ipmi0,host=localhost,port=${GUEST_IPMI_CHART_PORT},reconnect=10 -device ipmi-bmc-extern,chardev=ipmi0,id=bmc0 -device isa-ipmi-kcs,bmc=bmc0,irq=5\"
  startnow true
  user 1 true  \"\"        \"test\" user     10       none md2 md5 straight
  user 2 true  \"ipmiusr\" \"test\" admin    10       none md2 md5 straight
" >/var/lib/ikvswitch/runtime/${NAME}.ipmisim.conf

	start-stop-daemon \
		--start \
		--quiet \
		--background \
		--pidfile ${VM_PID_FILE}.ipmisim.pid \
		--make-pidfile \
		--startas /usr/bin/ipmi_sim \
		--      -n \
			-c /var/lib/ikvswitch/runtime/${NAME}.ipmisim.conf \
			-f /etc/ikvswitch/ipmisim1.emu \
		|| return 2

}

ikv_leaf_switch_write_ipmisim () {
	local NAME VNC_PORT HOST_IPMI_PORT GUEST_IPMI_CHART_PORT SD_PATH VM_PID_FILE FIRST_DRIVE MONITOR_PORT TAP_IF1_NAME TAP_IF2_NAME MAC1_END MAC2_END
	local OTHER_IFS_NUMS OTHER_IFS_TAP_PREFIX OTHER_IFS_TAP_SUFIX_START OTHER_IFS_MAC_END IFS_DEF OTHER_IFS_PCI_ADDR
	for i in $@ ; do
	case "${1}" in
	"--name")
		NAME=${2}
		shift
		shift
	;;
	"--vnc-port")
		VNC_PORT=${2}
		shift
		shift
	;;
	"--tap-if1-name")
		TAP_IF1_NAME=${2}
		shift
		shift
	;;
	"--mac1-end")
		MAC1_END=${2}
		shift
		shift
	;;
	"--tap-if2-name")
		TAP_IF2_NAME=${2}
		shift
		shift
	;;
	"--mac2-end")
		MAC2_END=${2}
		shift
		shift
	;;
	"--ip-address")
		IP_ADDRESS=${2}
		shift
		shift
	;;
	"--other-ifs-num")
		OTHER_IFS_NUMS=${2}
		shift
		shift
	;;
	"--other-ifs-tap-prefix")
		OTHER_IFS_TAP_PREFIX=${2}
		shift
		shift
	;;
	"--other-ifs-tap-sufix-start")
		OTHER_IFS_TAP_SUFIX_START=${2}
		shift
		shift
	;;
	"--other-ifs-mac-end")
		OTHER_IFS_MAC_END=${2}
		shift
		shift
	;;
	esac
	done

	echo "===> Spinning up ${NAME}"
	SD_PATH=/var/lib/ikvswitch/runtime/${NAME}.raw
	VM_PID_FILE=/var/lib/ikvswitch/runtime/${NAME}.pid
	FIRST_DRIVE="-device virtio-scsi-pci,id=scsi0,addr=0x3 -drive if=none,file=${SD_PATH},index=0,media=disk,format=raw,cache=none,discard=unmap,aio=native,id=drive-scsi0-0-0-0 -device scsi-hd,drive=drive-scsi0-0-0-0,bus=scsi0.0"

	HOST_IPMI_PORT=$(( ${VNC_PORT} + 9000 ))
	GUEST_IPMI_CHART_PORT=$(( ${VNC_PORT} + 9100 ))
	MONITOR_PORT=$(( 55000 + ${VNC_PORT}))

  # Define connection the to current rack leafs
	IFS_DEF="-device virtio-net-pci,netdev=net0,mac=${GUEST_MAC_ADDRESS_PREFIX}${MAC1_END},addr=0x4 -netdev tap,id=net0,ifname=${TAP_IF1_NAME}"
	IFS_DEF="${IFS_DEF} -device virtio-net-pci,netdev=net1,mac=${GUEST_MAC_ADDRESS_PREFIX}${MAC2_END},addr=0x5 -netdev tap,id=net1,ifname=${TAP_IF2_NAME}"

	# Pin the PCI address of each NIC, so that the guest names its
	# interfaces enp0s4, enp0s5, ... regardless of the machine type.
	OTHER_IFS_PCI_ADDR=6
	if [ -n "${OTHER_IFS_NUMS}" ] ; then
		for i in $(seq 3 ${OTHER_IFS_NUMS}) ; do
			IFS_DEF="${IFS_DEF} -device virtio-net-pci,netdev=net${OTHER_IFS_TAP_SUFIX_START},mac=${GUEST_MAC_ADDRESS_PREFIX}${OTHER_IFS_MAC_END},addr=0x$(printf %X ${OTHER_IFS_PCI_ADDR}) -netdev tap,id=net${OTHER_IFS_TAP_SUFIX_START},ifname=${OTHER_IFS_TAP_PREFIX}${OTHER_IFS_TAP_SUFIX_START}"
			OTHER_IFS_MAC_END="$(mac_add ${OTHER_IFS_MAC_END} 1)"
			OTHER_IFS_TAP_SUFIX_START=$(( $OTHER_IFS_TAP_SUFIX_START + 1 ))
			OTHER_IFS_PCI_ADDR=$(( ${OTHER_IFS_PCI_ADDR} + 1 ))
		done
	fi

        echo "name \"ipmisim1\"
set_working_mc 0x20
  startlan 1
    addr 0.0.0.0 ${HOST_IPMI_PORT}
    priv_limit admin
    allowed_auths_callback none md2 md5 straight
    allowed_auths_user none md2 md5 straight
    allowed_auths_operator none md2 md5 straight
    allowed_auths_admin none md2 md5 straight
    guid a123456789abcdefa123456789abcdef
  endlan
  serial 15 localhost ${GUEST_IPMI_CHART_PORT} codec VM
  startcmd \"qemu-system-x86_64 -enable-kvm -machine q35 -m size=512M -smp cpus=2 -cpu host,+spec-ctrl -vnc :${VNC_PORT} -monitor tcp:127.0.0.1:${MONITOR_PORT},server,nowait -pidfile ${VM_PID_FILE} -daemonize ${FIRST_DRIVE} -boot c ${IFS_DEF} -smbios type=1,manufacturer=LinuxKVM,product=qemu-oci,serial=${NAME} -smbios type=3,manufacturer=LinuxKVM,serial=${NAME} -chardev socket,id=ipmi0,host=localhost,port=${GUEST_IPMI_CHART_PORT},reconnect=10 -device ipmi-bmc-extern,chardev=ipmi0,id=bmc0 -device isa-ipmi-kcs,bmc=bmc0,irq=5\"
  startnow true
  user 1 true  \"\"        \"test\" user     10       none md2 md5 straight
  user 2 true  \"ipmiusr\" \"test\" admin    10       none md2 md5 straight
" >/var/lib/ikvswitch/runtime/${NAME}.ipmisim.conf

	start-stop-daemon \
		--start \
		--quiet \
		--background \
		--pidfile ${VM_PID_FILE}.ipmisim.pid \
		--make-pidfile \
		--startas /usr/bin/ipmi_sim \
		--      -n \
			-c /var/lib/ikvswitch/runtime/${NAME}.ipmisim.conf \
			-f /etc/ikvswitch/ipmisim1.emu \
		|| return 2

}

MYDIR=$(pwd)

if ! [ -r /var/lib/ikvswitch/templates/ikvswitch-template.raw ] ; then
	cd /var/lib/ikvswitch/templates
	ikvswitch_build_template
fi
cd /var/lib/ikvswitch/runtime

usage () {
	echo "usage: $ME <start/stop>"
	exit 1
}

case "${1}" in
"start")
	# Parse the optional arguments following the "start" command.
	# The default value of "all" keeps the historical behavior of
	# redistributing every VLAN subnet into BGP.
	MAP_REDISTRIBUTE_VLAN_SUBNETS="all"
	shift
	while [ -n "${1}" ] ; do
		case "${1}" in
		"--map-redistribute-vlan-subnets")
			MAP_REDISTRIBUTE_VLAN_SUBNETS="${2}"
			shift 2
		;;
		*)
			echo "Unknown option: ${1}"
			echo "Usage: ikvswitch-setup start [--map-redistribute-vlan-subnets all|none|113,114,119]"
			exit 1
		;;
		esac
	done
	# Validate the value of --map-redistribute-vlan-subnets: it must
	# be "all", "none", or a comma separated list of /24 last octets
	# within the range of the VLAN subnets, as defined by
	# VLAN_SBUNETS_START_IP and VLAN_SUBNETS_NUM.
	case "${MAP_REDISTRIBUTE_VLAN_SUBNETS}" in
	"all"|"none")
	;;
	*)
		for VLAN_OCTET in $(echo ${MAP_REDISTRIBUTE_VLAN_SUBNETS} | tr ',' ' ') ; do
			case "${VLAN_OCTET}" in
			""|*[!0-9]*)
				echo "Invalid VLAN subnet octet \"${VLAN_OCTET}\" in --map-redistribute-vlan-subnets"
				exit 1
			;;
			esac
			if [ "${VLAN_OCTET}" -lt "${VLAN_SBUNETS_START_IP}" ] || [ "${VLAN_OCTET}" -gt "$(( ${VLAN_SBUNETS_START_IP} + ${VLAN_SUBNETS_NUM} - 1 ))" ] ; then
				echo "VLAN subnet octet ${VLAN_OCTET} out of range ($(( ${VLAN_SBUNETS_START_IP} )) to $(( ${VLAN_SBUNETS_START_IP} + ${VLAN_SUBNETS_NUM} - 1 ))) in --map-redistribute-vlan-subnets"
				exit 1
			fi
		done
	;;
	esac
	# Validate the DHCP setup, as all of its values must come
	# from the configuration file (no hardcoded value in this
	# script). Only DHCP_SERVER_DAEMON has a fallback, so that
	# configuration files predating that option keep working.
	# This isn't done for "stop", as tearing down the lab must
	# always work, whatever the configuration file says.
	DHCP_SERVER_DAEMON=${DHCP_SERVER_DAEMON:-dnsmasq}
	DHCP_SERVER_VARS="DHCP_DEFAULT_LEASE_TIME DHCP_MAX_LEASE_TIME
		DHCP_POOL_START DHCP_POOL_END DHCP_DOMAIN_NAME DHCP_DNS_SERVERS
		DHCP_PXE_BOOTFILE_BIOS DHCP_PXE_BOOTFILE_EFI
		DHCP_PXE_BOOTFILE_FALLBACK DHCP_IPXE_BOOT_URL"
	for DHCP_VAR in ${DHCP_SERVER_VARS} ; do
		if [ -z "$(eval echo "\${${DHCP_VAR}}")" ] ; then
			echo "Please set ${DHCP_VAR} in /etc/ikvswitch/ikvswitch.conf"
			exit 1
		fi
	done
	case "${DHCP_SERVER_DAEMON}" in
	"dnsmasq"|"isc-dhcp")
	;;
	*)
		echo "DHCP_SERVER_DAEMON must be either dnsmasq or isc-dhcp"
		exit 1
	;;
	esac
	if [ -n "${DHCP_HTTP_BOOT_URL}" ] ; then
		case "${DHCP_HTTP_BOOT_URL}" in
		http://*|https://*)
		;;
		*)
			echo "DHCP_HTTP_BOOT_URL must start with http:// or https://"
			exit 1
		;;
		esac
	fi
	rm /var/lib/ikvswitch/runtime/* || true
	# The "Internet" VM
	ikvswitch_customize_images --spine-loopback-ip ${HOST_VIRTUAL_SUBNET_NET3}.1 --as ${AS_START} --hostname internet --network-style INTERNET --ip-address ${HOST_VIRTUAL_SUBNET_NET1}.2 --gw ${HOST_VIRTUAL_SUBNET_NET1}.1
	MY_AS=$(( ${AS_START} + 1 ))

	# The 2x "Spine" VMs
	ikvswitch_customize_images --spine-loopback-ip ${HOST_VIRTUAL_SUBNET_NET3}.2 --as ${MY_AS} --hostname spine1 --network-style SPINE --ip-address ${HOST_VIRTUAL_SUBNET_NET3}.2
	MY_AS=$(( ${MY_AS} + 1 ))
	ikvswitch_customize_images --spine-loopback-ip ${HOST_VIRTUAL_SUBNET_NET3}.3 --as ${MY_AS} --hostname spine2 --network-style SPINE --ip-address ${HOST_VIRTUAL_SUBNET_NET3}.3

	# The 6x "Leaf" VMs, with 1, 3 and 5 having DHCPd
	for i in $(seq 1 6) ; do
		MY_AS=$(( ${MY_AS} + 1 ))
		# We install a DHCP server only on left switch rack
		# ie: one switch per rack.
		case "${i}" in
		"1")	DHCP_SERVER="--dhcpd ${DHCPD_SUBNET_1}"	;;
		"3")	DHCP_SERVER="--dhcpd ${DHCPD_SUBNET_2}"	;;
		"5")	DHCP_SERVER="--dhcpd ${DHCPD_SUBNET_3}"	;;
		*)	DHCP_SERVER=""	;;
		esac
		LEAF_LOOPBACK_IP=$(( $i + 3 ))
		if [ "${i}" = 2 ] ; then
			VLAN_OPT="--vlan-setup --map-redistribute-vlan-subnets ${MAP_REDISTRIBUTE_VLAN_SUBNETS}"
		else
			VLAN_OPT=""
		fi
		ikvswitch_customize_images ${VLAN_OPT} --spine-loopback-ip ${HOST_VIRTUAL_SUBNET_NET3}.${LEAF_LOOPBACK_IP} --as ${MY_AS} --hostname leaf${i} --network-style LEAF --ip-address ${HOST_VIRTUAL_SUBNET_NET3}.${LEAF_LOOPBACK_IP} ${DHCP_SERVER}
	done

	# The "Internet" VM
	ikv_spine_switch_write_ipmisim --name internet --vnc-port 80 --tap-ifname ${HOST_VM_TAP_IFNAME_PREFIX} --mac-end 10 --other-ifs-num 2 --other-ifs-tap-prefix ${INTERNET_TO_SPINE_TAP_IFNAME_PREFIX} --other-ifs-tap-sufix-start 1 --other-ifs-mac-end 12

	# Both 2x "Spine" VMs
	ikv_spine_switch_write_ipmisim --name spine1 --vnc-port 81 --tap-ifname ${SPINE_TO_INTERNET_TAP_PREFIX}1 --mac-end 21 --other-ifs-num 6 --other-ifs-tap-prefix ${SPINE_VM_TAP_IFNAME_PREFIX}1 --other-ifs-tap-sufix-start 2 --other-ifs-mac-end 22
	ikv_spine_switch_write_ipmisim --name spine2 --vnc-port 82 --tap-ifname ${SPINE_TO_INTERNET_TAP_PREFIX}2 --mac-end 31 --other-ifs-num 6 --other-ifs-tap-prefix ${SPINE_VM_TAP_IFNAME_PREFIX}2 --other-ifs-tap-sufix-start 2 --other-ifs-mac-end 32

	MAC_END=41 # Used to compute mac addresses automatically according to the number of VM per rack

	# The 6x "Leaf" VMs
	ikv_leaf_switch_write_ipmisim --name leaf1 --vnc-port 83 --tap-if1-name ${LEAF_TO_SPINE_TAP_PREFIX}11 --mac1-end "${MAC_END}" --tap-if2-name ${LEAF_TO_SPINE_TAP_PREFIX}12 --mac2-end "$(mac_add ${MAC_END} 1)" \
		--other-ifs-num ${NUM_U} --other-ifs-tap-prefix ${LEAF_TO_SERVERS_TAP_PREFIX}1- --other-ifs-tap-sufix-start 3 --other-ifs-mac-end "$(mac_add ${MAC_END} 2)" --ip-address ${HOST_VIRTUAL_SUBNET_NET3}.4

	# Compute the next macaddress available
	MAC_END="$(mac_add ${MAC_END} $((NUM_U + 1)))"

	ikv_leaf_switch_write_ipmisim --name leaf2 --vnc-port 84 --tap-if1-name ${LEAF_TO_SPINE_TAP_PREFIX}21 --mac1-end ${MAC_END} --tap-if2-name ${LEAF_TO_SPINE_TAP_PREFIX}22 --mac2-end "$(mac_add ${MAC_END} 1)" \
		--other-ifs-num ${NUM_U} --other-ifs-tap-prefix ${LEAF_TO_SERVERS_TAP_PREFIX}2- --other-ifs-tap-sufix-start 3 --other-ifs-mac-end "$(mac_add ${MAC_END} 2)" --ip-address ${HOST_VIRTUAL_SUBNET_NET3}.5

	# Compute the next macaddress available
	MAC_END="$(mac_add ${MAC_END} $((NUM_U + 1)))"

	ikv_leaf_switch_write_ipmisim --name leaf3 --vnc-port 85 --tap-if1-name ${LEAF_TO_SPINE_TAP_PREFIX}31 --mac1-end ${MAC_END} --tap-if2-name ${LEAF_TO_SPINE_TAP_PREFIX}32 --mac2-end "$(mac_add ${MAC_END} 1)" \
		--other-ifs-num ${NUM_U} --other-ifs-tap-prefix ${LEAF_TO_SERVERS_TAP_PREFIX}3- --other-ifs-tap-sufix-start 3 --other-ifs-mac-end "$(mac_add ${MAC_END} 2)" --ip-address ${HOST_VIRTUAL_SUBNET_NET3}.6

	# Compute the next macaddress available
	MAC_END="$(mac_add ${MAC_END} $((NUM_U + 1)))"

	ikv_leaf_switch_write_ipmisim --name leaf4 --vnc-port 86 --tap-if1-name ${LEAF_TO_SPINE_TAP_PREFIX}41 --mac1-end ${MAC_END} --tap-if2-name ${LEAF_TO_SPINE_TAP_PREFIX}42 --mac2-end "$(mac_add ${MAC_END} 1)" \
		--other-ifs-num ${NUM_U} --other-ifs-tap-prefix ${LEAF_TO_SERVERS_TAP_PREFIX}4- --other-ifs-tap-sufix-start 3 --other-ifs-mac-end "$(mac_add ${MAC_END} 2)" --ip-address ${HOST_VIRTUAL_SUBNET_NET3}.7

	# Compute the next macaddress available
	MAC_END="$(mac_add ${MAC_END} $((NUM_U + 1)))"

	ikv_leaf_switch_write_ipmisim --name leaf5 --vnc-port 87 --tap-if1-name ${LEAF_TO_SPINE_TAP_PREFIX}51 --mac1-end ${MAC_END} --tap-if2-name ${LEAF_TO_SPINE_TAP_PREFIX}52 --mac2-end "$(mac_add ${MAC_END} 1)" \
		--other-ifs-num ${NUM_U} --other-ifs-tap-prefix ${LEAF_TO_SERVERS_TAP_PREFIX}5- --other-ifs-tap-sufix-start 3 --other-ifs-mac-end "$(mac_add ${MAC_END} 2)" --ip-address ${HOST_VIRTUAL_SUBNET_NET3}.8

	# Compute the next macaddress available
	MAC_END="$(mac_add ${MAC_END} $((NUM_U + 1)))"

	ikv_leaf_switch_write_ipmisim --name leaf6 --vnc-port 88 --tap-if1-name ${LEAF_TO_SPINE_TAP_PREFIX}61 --mac1-end ${MAC_END} --tap-if2-name ${LEAF_TO_SPINE_TAP_PREFIX}62 --mac2-end "$(mac_add ${MAC_END} 1)" \
		--other-ifs-num ${NUM_U} --other-ifs-tap-prefix ${LEAF_TO_SERVERS_TAP_PREFIX}6- --other-ifs-tap-sufix-start 3 --other-ifs-mac-end "$(mac_add ${MAC_END} 2)" --ip-address ${HOST_VIRTUAL_SUBNET_NET3}.9

;;
"stop")
	echo "===> Killing all VMs and IPMI SIM:"
	for i in $(ls /var/lib/ikvswitch/runtime/*.pid) ; do
		PID=$(cat $i)
		echo " $PID"
		kill -9 ${PID} || true
	done
;;
*)
	usage
;;
esac

cd ${MYDIR}

echo "===> All done <==="
