#!/bin/bash
#============================================================================
# ${XEN_SCRIPT_DIR}/vif-vtrill
# Based on ${XEN_SCRIPT_DIR}/vif-openvswitch
#
# Script for configuring a vif in routed + bridged mode.
# The hotplugging system will call this script if it is specified either in
# the device configuration given to Xend, or the default Xend configuration
# in ${XEN_CONFIG_DIR}/xend-config.sxp.  If the script is specified in
# neither of those places, then this script is the default.
#
# Usage:
# vif-openvswitch (add|remove|online|offline)
#
# Environment vars:
# vif         vif interface name (required).
# XENBUS_PATH path to this device's details in the XenStore (required).
#
# Read from the store:
# ip      list of IP networks for the vif, space-separated (optional).
#
# up:
# Enslaves the vif interface to a VM-specific bridge and adds iptables rules
# for its ip addresses (if any).
#
# down:
# Removes the vif interface from the VM-bridge bridge and removes the iptables
# rules for its ip addresses (if any).
#============================================================================

dir=$(dirname "$0")
. "${dir}/vif-common.sh"

main_ip=$(dom0_ip)
main_ip6=$(dom0_ip6)
gateway_ip6=$(dom0_ip6_gateway)
vlanname=$(echo ${dev} | cut -d'.' -f1)

case "${command}" in
    online)
        create_bridge ${vlanname}
        add_to_bridge ${vlanname} ${dev}
        ifconfig ${vlanname} ${main_ip} netmask 255.255.255.255 up
        if [ ! -z "${main_ip6}" ]
        then
                ip -6 addr add ${main_ip6} dev ${vlanname}
                ip -6 neighbor add proxy ${gateway_ip6} dev ${vlanname}
                echo 1 >/proc/sys/net/ipv6/conf/${vlanname}/proxy_ndp
        fi
        echo 1 >/proc/sys/net/ipv4/conf/${vlanname}/proxy_arp
        ipcmd='add'
        cmdprefix=''
        ;;
    offline)
        ifconfig ${vlanname} down
        brctl delbr ${vlanname}
        ipcmd='del'
        cmdprefix='do_without_error'
        ;;
    add)
        create_bridge ${vlanname}
        add_to_bridge ${vlanname} ${dev}
        ;;
    remove)
        ;;
esac

if [ "${ip}" -a "$type_if" != "tap" ] ; then
    # If we've been given a list of IP addresses, then add routes from dom0 to
    # the guest using those addresses.
    for addr in ${ip} ; do
      do_without_error ip route del ${addr}
      if [ "$(is_ipv6 ${addr})" = "ipv6" ]; then
        ${cmdprefix} ip -6 route ${ipcmd} ${addr} dev ${vlanname} src ${main_ip6}
        ${cmdprefix} ip -6 neighbor ${ipcmd} proxy ${addr} dev ${netdev:-eth0}
      else
        do_without_error ip route ${ipcmd} ${addr} dev ${vlanname} src ${main_ip}
      fi
    done
fi

log "iptable"
handle_iptable

call_hooks vif post

log debug "Successful vif-vtrill ${command} for ${dev}."
if [ "${command}" = "online" ]
then
  success
fi
