#!/bin/sh

# CA-23900: Warning: when VIFs are added to windows guests with PV drivers the backend vif device is registered,
# unregistered and then registered again. This causes the udev event to fire twice and this script runs twice.
# Since the first invocation of the script races with the device unregistration, spurious errors are possible
# which will be logged but are safe to ignore since the second script invocation should complete the operation.
# Note that each script invocation is run synchronously from udev and so the scripts don't race with each other.

# Keep other-config/ keys in sync with device.ml:vif_udev_keys

TYPE=`echo ${XENBUS_PATH} | cut -f 2 -d '/'`
DOMID=`echo ${XENBUS_PATH} | cut -f 3 -d '/'`
DEVID=`echo ${XENBUS_PATH} | cut -f 4 -d '/'`

XAPI=/xapi/${DOMID}/hotplug/${TYPE}/${DEVID}
HOTPLUG=/xapi/${DOMID}/hotplug/${TYPE}/${DEVID}
PRIVATE=/xapi/${DOMID}/private/${TYPE}/${DEVID}
BRCTL=/usr/sbin/brctl
IP=/sbin/ip


handle_promiscuous()
{
    local arg=$(xenstore-read "${PRIVATE}/other-config/promiscuous")
    if [ $? -eq 0 -a -n "${arg}" ] ; then
        case "${arg}" in 
            true|on) echo 1 > /sys/class/net/${vif}/brport/promisc ;;
            *) echo 0 > /sys/class/net/${vif}/brport/promisc ;;
        esac
    fi
}

handle_ethtool()
{
    local opt=$1
    local arg=$(xenstore-read "${PRIVATE}/other-config/ethtool-${opt}")
    if [ $? -eq 0 -a -n "${arg}" ] ; then
        case "${arg}" in
            true|on)   /sbin/ethtool -K "${vif}" "${opt}" on ;;
            false|off) /sbin/ethtool -K "${vif}" "${opt}" off ;;
            *) logger -t scripts-vif "Unknown ethtool argument ${opt}=${arg} on ${vif}/${VIFUUID}" ;;
        esac
    fi
}

handle_mtu()
{
    local mtu=$(xenstore-read "${PRIVATE}/MTU")
    if [ $? -eq 0 -a -n "${mtu}" ]; then
	echo "${mtu}" > /sys/class/net/${vif}/mtu
    fi
}

add_to_bridge()
{
    local address=$(xenstore-read "${PRIVATE}/bridge-MAC")
    if [ $? -ne 0 -o -z "${address}" ]; then
	logger -t scripts-vif "Failed to read ${PRIVATE}/bridge-MAC from xenstore"
    fi
    local bridge=$(xenstore-read "${PRIVATE}/bridge")
    if [ $? -ne 0 -o -z "${bridge}" ]; then
	logger -t scripts-vif "Failed to read ${PRIVATE}/bridge from xenstore"
    fi
    logger -t scripts-vif "Adding ${vif} to ${bridge} with address ${address}"

    ${IP} link set "${vif}" down                        || logger -t scripts-vif "Failed to ip link set ${vif} down"
    ${IP} link set "${vif}" arp off                     || logger -t scripts-vif "Failed to ip link set ${vif} arp off"
    ${IP} link set "${vif}" multicast off               || logger -t scripts-vif "Failed to ip link set ${vif} multicast off"
    ${IP} link set "${vif}" address "${address}"        || logger -t scripts-vif "Failed to ip link set ${vif} address ${address}"
    ${IP} addr flush "${vif}"                           || logger -t scripts-vif "Failed to ip addr flush ${vif}"


    ${BRCTL} setfd "${bridge}" 0                        || logger -t scripts-vif "Failed to brctl setfd ${bridge} 0"
    ${BRCTL} addif "${bridge}" "${vif}"                 || logger -t scripts-vif "Failed to brctl addif ${bridge} ${vif}"
    ${IP} link set "${vif}" up                          || logger -t scripts-vif "Failed to ip link set ${vif} up"
}

case "$1" in
online)
	handle_ethtool rx
	handle_ethtool tx
	handle_ethtool sg
	handle_ethtool tso
	handle_ethtool ufo
	handle_ethtool gso

	handle_mtu
	add_to_bridge
	handle_promiscuous

	xenstore-write "${HOTPLUG}/vif" "${vif}"
	xenstore-write "${HOTPLUG}/hotplug" "online"

	# xs-xen.pq.hq:91e986b8e49f netback-wait-for-hotplug
	xenstore-write "/local/domain/0/backend/vif/${DOMID}/${DEVID}/hotplug-status" "connected"

	;;
remove)
	xenstore-rm "${HOTPLUG}/hotplug"
	logger -t scripts-vif "vif${DOMID}.${DEVID} has been removed"
	;;
esac
