#!/bin/bash

# Copyright (C) 2008,2009 Citrix Systems, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation; version 2.1 only. with the special
# exception on linking described in file LICENSE.
#
# 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 Lesser General Public License for more details.

# 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


if [ $# -lt 4 ]; then
    echo "Usage: $0 <type> <domid> <devid> <action>"
    echo "where type=[vif|tap]"
    echo "domid is the domain id of the frontend domain"
    echo "devid is the device id e.g. 0,1,2,etc"
    echo "action is [online|add|move|remove]"
    exit 1
fi

TYPE=$1
DOMID=$2
DEVID=$3
ACTION=$4

if [ -z $5 ]; then
    DUMMY=""
else
    DUMMY="echo "

fi

logger -t vif-real "Called as: $0 $TYPE $DOMID $DEVID $ACTION"

dev=${TYPE}${DOMID}.${DEVID}

# Find some utils that we'll need:
BRCTL="/usr/sbin/brctl"
if [ ! -e $BRCTL ] ; then
	BRCTL="/sbin/brctl"
fi
IP="/sbin/ip"
vsctl="/usr/bin/ovs-vsctl"

# Lets have a bunch of helper functions
handle_promiscuous()
{
    # other-config keys are optional
    local arg=$(xenstore-read "${PRIVATE}/other-config/promiscuous" 2>/dev/null)
    if [ $? -eq 0 -a -n "${arg}" ] ; then
        case $NETWORK_MODE in
            bridge)
                case "${arg}" in 
                    true|on) echo 1 > /sys/class/net/${dev}/brport/promisc ;;
                    *) echo 0 > /sys/class/net/${dev}/brport/promisc ;;
                esac
                ;;
            openvswitch)
                logger -t script-vif "${dev}: Promiscuous ports are not supported via Open vSwitch."
                ;;
        esac
    fi
}

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

handle_mtu()
{
    local mtu=$(xenstore-read "${PRIVATE}/MTU" 2>/dev/null)
    if [ $? -eq 0 -a -n "${mtu}" ]; then
        logger -t scripts-vif "Setting ${dev} MTU ${mtu}"
        ${IP} link set "${dev}" mtu ${mtu} || logger -t scripts-vif "Failed to ip link set ${dev} mtu ${mtu}. Error code $?"
    else
        logger -t scripts-vif "Failed to read ${PRIVATE}/MTU"
        xenstore-write "${HOTPLUG_ERROR}" "Failed to read ${PRIVATE}/MTU"
        exit 1
    fi
}

set_vif_external_id()
{
    local key=$1
    local value=$2

    logger -t scripts-vif "vif${DOMID}.${DEVID} external-ids:\"${key}\"=\"${value}\""

    echo "-- set interface vif${DOMID}.${DEVID} external-ids:\"${key}\"=\"${value}\""
}

handle_vswitch_vif_details()
{
    local vm=$(xenstore-read "/local/domain/$DOMID/vm" 2>/dev/null)
    if [ $? -eq 0 -a -n "${vm}" ] ; then
        local vm_uuid=$(xenstore-read "$vm/uuid" 2>/dev/null)
    else
        logger -t scripts-vif "Failed to read /local/domain/$DOMID/vm"
        xenstore-write "${HOTPLUG_ERROR}" "Failed to read /local/domain/$DOMID/vm"
        exit 1
    fi
    if [ -n "${vm_uuid}" ] ; then
        set_vif_external_id "xs-vm-uuid" "${vm_uuid}"
    else
        logger -t scripts-vif "Failed to read $vm/uuid"
        xenstore-write "${HOTPLUG_ERROR}" "Failed to read $vm/uuid"
        exit 1
    fi

    local vif_uuid=$(xenstore-read "${PRIVATE}/vif-uuid" 2>/dev/null)
    if [ -n "${vif_uuid}" ] ; then
	set_vif_external_id "xs-vif-uuid" "${vif_uuid}"
    else
        logger -t scripts-vif "Failed to read ${PRIVATE}/vif-uuid"
        xenstore-write "${HOTPLUG_ERROR}" "Failed to read ${PRIVATE}/vif-uuid"
        exit 1
    fi

    local vif_details=
    local net_uuid=$(xenstore-read "${PRIVATE}/network-uuid" 2>/dev/null)
    if [ -n "${net_uuid}" ] ; then
	set_vif_external_id "xs-network-uuid" "${net_uuid}"
    else
        logger -t scripts-vif "Failed to read ${PRIVATE}/network-uuid"
        xenstore-write "${HOTPLUG_ERROR}" "Failed to read ${PRIVATE}/network-uuid"
        exit 1
    fi
    local address=$(xenstore-read "/local/domain/$DOMID/device/vif/$DEVID/mac" 2>/dev/null)
    if [ -n "${address}" ] ; then
        set_vif_external_id "attached-mac" "${address}"
    else
        logger -t scripts-vif "Failed to read /local/domain/$DOMID/device/vif/$DEVID/mac"
        xenstore-write "${HOTPLUG_ERROR}" "Failed to read /local/domain/$DOMID/device/vif/$DEVID/mac"
        exit 1
    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"
        xenstore-write "${HOTPLUG_ERROR}" "Failed to read ${PRIVATE}/bridge-MAC from xenstore"
        exit 1
    fi

    local bridge=$(xenstore-read "backend/vif/$DOMID/$DEVID/bridge")
    if [ $? -ne 0 -o -z "${bridge}" ]; then
        logger -t scripts-vif "Failed to read backend/vif/$DOMID/$DEVID/bridge from xenstore. Trying ${PRIVATE}/bridge"
        local bridge=$(xenstore-read "${PRIVATE}/bridge")
        if [ $? -ne 0 -o -z "${bridge}" ]; then
            logger -t scripts-vif "Failed to read ${PRIVATE}/bridge from xenstore"
            xenstore-write "${HOTPLUG_ERROR}" "Failed to read ${PRIVATE}/bridge from xenstore"
            exit 1
        fi
    fi

    logger -t scripts-vif "Adding ${dev} to ${bridge} with address ${address}"

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

    case $NETWORK_MODE in
    bridge)
        ${BRCTL} setfd "${bridge}" 0                    || logger -t scripts-vif "Failed to brctl setfd ${bridge} 0"
        ${BRCTL} addif "${bridge}" "${dev}"             || logger -t scripts-vif "Failed to brctl addif ${bridge} ${dev}"
        ;;
    openvswitch)
        if [ "$TYPE" = "vif" ] ; then
            local vif_details=$(handle_vswitch_vif_details $bridge)
        fi

        $vsctl --timeout=30 -- --if-exists del-port $dev -- add-port $bridge $dev $vif_details
        ;;
    esac

    local setup_vif_rules=$(xenstore-read "${PRIVATE}/setup-vif-rules" 2>/dev/null)
    $setup_vif_rules $XENOPSD_BACKEND ${dev} $DOMUUID $DEVID filter         || logger -t scripts-vif "Failed to setup-vif-rules $XENOPSD_BACKEND ${dev} $DOMUUID $DEVID filter"
}

remove_from_bridge()
{
    local setup_vif_rules=$(xenstore-read "${PRIVATE}/setup-vif-rules" 2>/dev/null)
    $setup_vif_rules $XENOPSD_BACKEND ${dev} $DOMUUID $DEVID clear          || logger -t scripts-vif "Failed to setup-vif-rules $XENOPSD_BACKEND ${dev} $DOMUUID $DEVID clear"

    case $NETWORK_MODE in
    bridge)
        # Nothing to do
        ;;
    openvswitch)
        # If ovs-brcompatd is running, it might already have deleted the
        # port.  Use --if-exists to suppress the error that would otherwise
        # arise in that case.
        $vsctl --timeout=30 -- --if-exists del-port $dev
        ;;
    esac
}

call_hook_script() {
    local domid=$1
    local action=$2
    # Call the VIF hotplug hook if present
    if [ -x /etc/xapi.d/vif-hotplug ]; then
        local vm=$(xenstore-read "/local/domain/$domid/vm" 2>/dev/null)
        if [ $? -eq 0 -a -n "${vm}" ] ; then
            local vm_uuid=$(xenstore-read "$vm/uuid" 2>/dev/null)
        else
            logger -t scripts-vif "Failed to read /local/domain/$domid/vm"
            xenstore-write "${HOTPLUG_ERROR}" "Failed to read /local/domain/$domid/vm"
        fi
        if [ -n "${vm_uuid}" ] ; then
            logger -t scripts-vif "VM UUID ${vm_uuid}"
        else
            logger -t scripts-vif "Failed to read $vm/uuid"
            xenstore-write "${HOTPLUG_ERROR}" "Failed to read $vm/uuid"
        fi

        local vif_uuid=$(xenstore-read "${PRIVATE}/vif-uuid" 2>/dev/null)
        if [ -n "${vif_uuid}" ] ; then
            logger -t scripts-vif "VIF UUID ${vif_uuid}"
        else
            logger -t scripts-vif "Failed to read ${PRIVATE}/vif-uuid"
            xenstore-write "${HOTPLUG_ERROR}" "Failed to read ${PRIVATE}/vif-uuid"
        fi
        if [ -n "${vif_uuid}" -a -n "${vm_uuid}" ] ; then
            logger -t scripts-vif "Calling VIF hotplug hook for VM ${vm_uuid}, VIF ${vif_uuid}"
            /etc/xapi.d/vif-hotplug -action "${action}" -vifuuid "${vif_uuid}" -vmuuid "${vm_uuid}"
        fi
    fi
}





VM=$(xenstore-read "/local/domain/${DOMID}/vm" 2> /dev/null)
DOMUUID=$(xenstore-read "${VM}/uuid" 2> /dev/null)

HOTPLUG=/xapi/${DOMUUID}/hotplug/${DOMID}/vif/${DEVID}
PRIVATE=/xapi/${DOMUUID}/private/vif/${DEVID}
HOTPLUG_STATUS="${XENBUS_PATH}/hotplug-status"
HOTPLUG_ERROR="${XENBUS_PATH}/hotplug-error"

NETWORK_MODE=bridge 
if [ -e /sys/module/openvswitch ]; then
    NETWORK_MODE=openvswitch
fi
XENOPSD_BACKEND=$(xenstore-read "${PRIVATE}/xenopsd-backend" 2>/dev/null)

case $NETWORK_MODE in
    bridge|openvswitch) ;;
    vswitch) NETWORK_MODE=openvswitch ;;
    *)
        logger -t scripts-vif "Unknown network mode $NETWORK_MODE"
        exit 1
        ;;
esac

# Hack to prevent the execution of hotplug scripts from udev if the domain
# has been launched from libxl
if [ -n "${UDEV_CALL}" ] && \
   xenstore-read "libxl/disable_udev" >/dev/null 2>&1; then
    logger -t scripts-vif "called from udev and disable_udev set, skipping domid:$DOMID devid:$DEVID mode:$NETWORK_MODE uuid:$DOMUUID"
    exit 0
fi

logger -t scripts-vif "Called as \"$@\" domid:$DOMID devid:$DEVID mode:$NETWORK_MODE"
case "${ACTION}" in
online)
    if [ "${TYPE}" = "vif" ] ; then
        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

        # only for the benefit of xenrt test case, see CA-61528
        xenstore-write "${HOTPLUG}/vif" "${dev}"
        xenstore-write "${HOTPLUG}/hotplug" "online"

        logger -t script-vif "${dev}: writing ${HOTPLUG_STATUS}=connected"
        xenstore-write "${HOTPLUG_STATUS}" "connected"
        call_hook_script $DOMID "${ACTION}"
    fi
    ;;

add)
    if [ "${TYPE}" = "tap" ] ; then
        add_to_bridge
    fi
    ;;

remove)
    if [ "${TYPE}" = "vif" ] ;then
        call_hook_script $DOMID "${ACTION}"
		# Unclear whether this is necessary, since netback also does it:
        logger -t script-vif "${dev}: removing ${HOTPLUG_STATUS}"
        xenstore-rm "${HOTPLUG_STATUS}"
        logger -t script-vif "${dev}: removing ${HOTPLUG}/hotplug"
		# Trigger xapi to clean up:
        xenstore-rm "${HOTPLUG}/hotplug"
    fi
    logger -t scripts-vif "${dev} has been removed"
    remove_from_bridge
    ;;

move)
    if [ "${TYPE}" = "vif" ] ;then
        add_to_bridge
    fi
esac
