#!/bin/sh

set -e

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 () {
	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 \
		--static-iface type=normal,iface0=ens4,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

	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
	;;
	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

	# Configure FRR and /etc/network/interfaces
	###############################
	### SPINE OR INTERNET NODES ###
	###############################
	if [ "${NETWORK_STYLE}" = "SPINE" ] || [ "${NETWORK_STYLE}" = "INTERNET" ] ; then
		if [ "${NETWORK_STYLE}" = "INTERNET" ] ; then
			IP_STUFF="auto ens4
iface ens4 inet static
	address ${IP_ADDRESS}
	netmask 255.255.255.0
	gateway ${GATEWAY}
"
		else
			IP_STUFF="auto ens4
iface ens4 inet manual
	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_STUFF}

auto ens5
iface ens5 inet manual
	metric 4278198272

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

auto ens8
iface ens8 inet manual
	metric 4278198272

auto ens9
iface ens9 inet manual
	metric 4278198272

auto ens10
iface ens10 inet manual
	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 ens4 interface peer-group spine
 neighbor ens5 interface peer-group leaf
 neighbor ens6 interface peer-group leaf" >${MOUNT_DIR}/etc/frr/frr.conf
		if [ "${NETWORK_STYLE}" = "SPINE" ] ; then
			echo " neighbor ens7 interface peer-group leaf
 neighbor ens8 interface peer-group leaf
 neighbor ens9 interface peer-group leaf
 neighbor ens10 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
  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
!
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
!
route-map map-leaf-in permit 10
exit
!
" >>${MOUNT_DIR}/etc/frr/frr.conf
	##################
	### 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

auto ens4
iface ens4 inet manual

auto ens5
iface ens5 inet manual
" >${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 ens${i}
iface ens${i} inet manual
	mtu 9000
	metric 4278198272

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

" >>${MOUNT_DIR}/etc/network/interfaces
			if [ "${DO_VLAN_SETUP}" = "yes" ] ; then
				VLAN_END=$(( ${VLAN_START} + ${VLAN_SUBNETS_NUM} ))
				for j in $(seq ${VLAN_START} ${VLAN_END}) ; do
					echo "auto ens${i}.${j}
iface ens${i}.${j} inet manual
	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} ))
			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} ens${IF}.${j}"
				done
				echo "auto brvlan${j}
iface brvlan${j} inet static
        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
!
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 ens4 interface peer-group spine
 neighbor ens5 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 ens${IF}.${LEAF_TO_VM_VLAN_NUM} interface peer-group leaf" >>${MOUNT_DIR}/etc/frr/frr.conf
		done

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


		if [ "${DO_VLAN_SETUP}" = "yes" ] ; then
			# Allow both cl1-network-1 and cl2-network-2 to connect and advertize BGP.
			echo " neighbor ${VLAN_SBUNET1_PREFIX}.200 remote-as external" >>${MOUNT_DIR}/etc/frr/frr.conf
			echo " neighbor ${VLAN_SBUNET1_PREFIX}.201 remote-as external" >>${MOUNT_DIR}/etc/frr/frr.conf
		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 both cl1-network-1 and cl2-network-2 to connect and advertize BGP.
			echo "  neighbor ${VLAN_SBUNET1_PREFIX}.200 activate
  neighbor ${VLAN_SBUNET1_PREFIX}.200 route-map vm-in in
  neighbor ${VLAN_SBUNET1_PREFIX}.200 route-map vm-out out" >>${MOUNT_DIR}/etc/frr/frr.conf
			echo "  neighbor ${HOST_VIRTUAL_SUBNET_START_PREFIX}.113.201 activate
  neighbor ${VLAN_SBUNET1_PREFIX}.201 route-map vm-in in
  neighbor ${VLAN_SBUNET1_PREFIX}.201 route-map vm-out out" >>${MOUNT_DIR}/etc/frr/frr.conf
		fi
		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
 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
!
route-map map-leaf-in permit 10
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} ens${IF}"
			done
			echo "auto dhcp0
iface dhcp0 inet static
	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="ens6 ens7 ens8 ens9 ens10 ens11 ens12 ens13 ens14 ens15 ens17 ens18"/' ${MOUNT_DIR}/etc/default/isc-dhcp-server
		fi
		if [ "${DO_VLAN_SETUP}" = "yes" ] ; then
			VLAN_END=$(( ${VLAN_START} + ${VLAN_SUBNETS_NUM} ))
			SUBNET_SUFFIX=${VLAN_SBUNETS_START_IP}
			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
			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
			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
	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 -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} -netdev tap,id=net0,ifname=${TAP_IFNAME}"

	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} -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 ))
		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 -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
	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 -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} -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} -netdev tap,id=net1,ifname=${TAP_IF2_NAME}"

  # Connection to the other VMs
	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} -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 ))
		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 -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")
	# 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"
		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 <==="
