Merge branch 'master' into next
[sliver-openvswitch.git] / xenserver / etc_xensource_scripts_vif
index 572f5af..4e24d83 100755 (executable)
@@ -1,7 +1,16 @@
 #!/bin/sh
 
-# Copyright (C) 2008,2009 Citrix Systems, Inc. All rights reserved.
-# Copyright (C) 2009 Nicira Networks, Inc.
+# 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.
 
 # Keep other-config/ keys in sync with device.ml:vif_udev_keys
 
-cfg_mod="/root/vswitch/bin/ovs-cfg-mod"
-dump_vif_details="/root/vswitch/scripts/dump-vif-details"
-service="/sbin/service"
-
-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
+BRCTL="/usr/sbin/brctl"
+IP="/sbin/ip"
 
+vsctl="/usr/bin/ovs-vsctl"
+dump_vif_details="/usr/share/vswitch/scripts/dump-vif-details"
 
 handle_promiscuous()
 {
-    local arg=$(xenstore-read "${PRIVATE}/other-config/promiscuous")
+    local arg=$(xenstore-read "${PRIVATE}/other-config/promiscuous" 2>/dev/null)
     if [ $? -eq 0 -a -n "${arg}" ] ; then
-        case "${arg}" in 
-            true|on) logger -t script-vif "${vif}: Promiscuous ports are not supported via vSwitch." ;;
-            *) ;;
-        esac
+       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
+               ;;
+           vswitch)
+               logger -t script-vif "${dev}: Promiscuous ports are not supported via vSwitch."
+               ;;
+       esac
     fi
 }
 
 handle_ethtool()
 {
     local opt=$1
-    local arg=$(xenstore-read "${PRIVATE}/other-config/ethtool-${opt}")
+    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 "${vif}" "${opt}" on ;;
-            false|off) /sbin/ethtool -K "${vif}" "${opt}" off ;;
-            *) logger -t scripts-vif "Unknown ethtool argument ${opt}=${arg} on ${vif}/${VIFUUID}" ;;
+            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")
+    local mtu=$(xenstore-read "${PRIVATE}/MTU" 2>/dev/null)
     if [ $? -eq 0 -a -n "${mtu}" ]; then
-       echo "${mtu}" > /sys/class/net/${vif}/mtu
+       echo "${mtu}" > /sys/class/net/${dev}/mtu
     fi
 }
 
@@ -63,75 +70,118 @@ 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"
+       exit 1
     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"
+       exit 1
     fi
-    logger -t scripts-vif "Adding ${vif} to ${bridge} with address ${address}"
-
-    vid=
-    if [ -e "/var/lib/openvswitch/br-$bridge" ]; then
-       . "/var/lib/openvswitch/br-$bridge"
-       if [ -n "$VLAN_SLAVE" -a -n "$VLAN_VID" ]; then
-           bridge=$VLAN_SLAVE
-           vid="--add=vlan.$vif.tag=$VLAN_VID"
-       fi
-    fi
-
-    ${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}"
-
-    local vif_details=$($dump_vif_details $DOMID $DEVID)
-    if [ $? -ne 0 -o -z "${vif_details}" ]; then
-           logger -t scripts-vif "Failed to retrieve vif details for vswitch"
-    fi
-
-    $cfg_mod -F /etc/ovs-vswitchd.conf \
-        --del-match="bridge.*.port=$vif" \
-        --del-match="vlan.$vif.trunks=*" \
-        --del-match="vlan.$vif.tag=*" \
-        --del-match="port.$vif.[!0-9]*" \
-        --add="bridge.$bridge.port=$vif" \
-        $vid $vif_details -c 
-    $service vswitch reload
+    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}"
+           ;;
+       vswitch)
+               local vif_details=$($dump_vif_details $DOMID $DEVID)
+               if [ $? -ne 0 -o -z "${vif_details}" ]; then
+                       logger -t scripts-vif "Failed to retrieve vif details for vswitch"
+               fi
+
+               $vsctl add-port $bridge $dev $vif_details
+           ;;
+    esac
+           
+    ${IP} link set "${dev}" up                          || logger -t scripts-vif "Failed to ip link set ${dev} up"
+}
 
-    ${IP} link set "${vif}" up                          || logger -t scripts-vif "Failed to ip link set ${vif} up"
+remove_from_bridge()
+{
+    case $NETWORK_MODE in
+       bridge)
+           # Nothing to do
+           ;;
+       vswitch)
+        $vsctl del-port $bridge $dev
+           ;;
+    esac
 }
 
-echo Called as "$@" "$TYPE" "$DOMID" "$DEVID" | logger -t scripts-vif
-case "$1" in
-online)
-       handle_ethtool rx
-       handle_ethtool tx
-       handle_ethtool sg
-       handle_ethtool tso
-       handle_ethtool ufo
-       handle_ethtool gso
+NETWORK_MODE=$(cat /etc/xensource/network.conf)
+ACTION=$1
+TYPE=$2
 
-       handle_mtu
-       add_to_bridge
-       handle_promiscuous
+case $NETWORK_MODE in
+    bridge|vswitch) ;;
+    *)
+       logger -t scripts-vif "Unknown network mode $NETWORK_MODE"
+       exit 1
+       ;;
+esac
+
+case ${TYPE} in
+    vif)
+       DOMID=`echo ${XENBUS_PATH} | cut -f 3 -d '/'`
+       DEVID=`echo ${XENBUS_PATH} | cut -f 4 -d '/'`
+       dev=vif${DOMID}.${DEVID}
+       ;;
+    tap)
+       dev=$INTERFACE
+       DOMID=`echo ${dev#tap} | cut -f 1 -d '.'`
+       DEVID=`echo ${dev#tap} | cut -f 2 -d '.'`
+       ;;
+    *)  
+       logger -t scripts-vif "unknown interface type ${TYPE}"
+       exit 1
+       ;;
+esac
 
-       xenstore-write "${HOTPLUG}/vif" "${vif}"
-       xenstore-write "${HOTPLUG}/hotplug" "online"
+XAPI=/xapi/${DOMID}/hotplug/vif/${DEVID}
+HOTPLUG=/xapi/${DOMID}/hotplug/vif/${DEVID}
+PRIVATE=/xapi/${DOMID}/private/vif/${DEVID}
 
-       # xs-xen.pq.hq:91e986b8e49f netback-wait-for-hotplug
-       xenstore-write "/local/domain/0/backend/vif/${DOMID}/${DEVID}/hotplug-status" "connected"
+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
+
+           xenstore-write "${HOTPLUG}/vif" "${dev}"
+           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"
+       fi
+       ;;
 
+add)
+       if [ "${TYPE}" = "tap" ] ; then
+           add_to_bridge
+       fi
        ;;
+
 remove)
-       xenstore-rm "${HOTPLUG}/hotplug"
-       vif=vif${DOMID}.${DEVID}
-       logger -t scripts-vif "${vif} has been removed"
-       $cfg_mod -vANY:console:emer -F /etc/ovs-vswitchd.conf \
-           --del-match="bridge.*.port=${vif}" \
-           --del-match="vlan.${vif}.trunks=*" \
-           --del-match="vlan.${vif}.tag=*" \
-           --del-match="port.${vif}.[!0-9]*" -c
-       $service vswitch reload
+       if [ "${TYPE}" = "vif" ] ;then
+           xenstore-rm "${HOTPLUG}/hotplug"
+       fi
+       logger -t scripts-vif "${dev} has been removed"
+       remove_from_bridge
        ;;
 esac