debian: Fix references to OpenFlow in ovs-switch-config template
[openvswitch] / xenserver / etc_xensource_scripts_vif
index 1fbf155b628593d7c8b656d4c5e3d9d10780e5a4..240cb7098728241a9f52bd1ff77dde7e07341c28 100755 (executable)
@@ -24,7 +24,16 @@ BRCTL="/usr/sbin/brctl"
 IP="/sbin/ip"
 
 vsctl="/usr/bin/ovs-vsctl"
-dump_vif_details="/usr/share/vswitch/scripts/dump-vif-details"
+
+# XAPI before build 29381 (approximately) did not provide some of the
+# data in XenStore that we rely on.
+. /etc/xensource-inventory
+if test "$PRODUCT_VERSION" = "5.5.0" || test "${BUILD_NUMBER%[a-z]}" -le 26131
+then
+    xs550=true
+else
+    xs550=false
+fi
 
 handle_promiscuous()
 {
@@ -38,7 +47,7 @@ handle_promiscuous()
                esac
                ;;
            vswitch)
-               logger -t script-vif "${dev}: Promiscuous ports are not supported via vSwitch."
+               logger -t script-vif "${dev}: Promiscuous ports are not supported via Open vSwitch."
                ;;
        esac
     fi
@@ -61,7 +70,72 @@ handle_mtu()
 {
     local mtu=$(xenstore-read "${PRIVATE}/MTU" 2>/dev/null)
     if [ $? -eq 0 -a -n "${mtu}" ]; then
-       echo "${mtu}" > /sys/class/net/${dev}/mtu
+       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 $?"
+    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)
+    fi
+    if [ -n "${vm_uuid}" ] ; then
+       set_vif_external_id "vm-uuid" "${vm_uuid}"
+    fi
+
+    local vif_uuid=$(xenstore-read "${PRIVATE}/vif-uuid" 2>/dev/null)
+    if $xs550 && [ -z "${vif_uuid}" ] && [ -n "${vm_uuid}" ]; then
+        vif_uuid=$(xe vif-list --minimal vm-uuid="${vm_uuid}" device=$DEVID)
+    fi
+    if [ -n "${vif_uuid}" ] ; then
+       set_vif_external_id "vif-uuid" "${vif_uuid}"
+    fi
+
+    local vif_details=
+    local net_uuid=$(xenstore-read "${PRIVATE}/network-uuid" 2>/dev/null)
+    if $xs550 && [ -z "${net_uuid}" ] && [ -n "${vif_uuid}" ]; then
+        net_uuid=$(xe vif-param-get uuid="${vif_uuid}" param-name=network-uuid)
+    fi
+    if [ -n "${net_uuid}" ] ; then
+       set_vif_external_id "network-uuid" "${net_uuid}"
+    fi
+
+    local address=$(xenstore-read "/local/domain/$DOMID/device/vif/$DEVID/mac" 2>/dev/null)
+    if [ -n "${address}" ] ; then
+       set_vif_external_id "vif-mac" "${address}"
+    fi
+}
+
+xs550_set_internal_network_uuid()
+{
+    if $xs550; then
+        # vNetManager needs to know the network UUID(s) associated with each
+        # datapath.  Normally interface-reconfigure adds them, but XAPI does
+        # not use interface-reconfigure for internal networks. Instead, XAPI
+        # calls the addbr ioctl internally, so we have to do it here instead
+        # for internal networks.  This is only acceptable because xapi is lazy
+        # about creating internal networks: it only creates one just before it
+        # adds the first vif to it.  There may still be a brief delay between
+        # the initial ovs-vswitchd connection to vNetManager and setting this
+        # configuration variable, but vNetManager can tolerate that.
+        local bridge=$1
+        local net_uuid=$(xenstore-read "${PRIVATE}/network-uuid" 2>/dev/null)
+        if [ -n "${net_uuid}" ] ; then
+            logger -t scripts-vif "${bridge} network-uuids ${net_uuid}"
+            echo "-- br-set-external-id $bridge network-uuids ${net_uuid}"
+        fi
     fi
 }
 
@@ -91,12 +165,11 @@ add_to_bridge()
            ${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
+           if [ "$TYPE" = "vif" ] ; then
+               local vif_details=$(handle_vswitch_vif_details)
+           fi
 
-               $vsctl add-port $bridge $dev $vif_details
+           $vsctl -- --if-exists del-port $dev -- add-port $bridge $dev $vif_details $(xs550_set_internal_network_uuid $bridge)
            ;;
     esac
            
@@ -110,7 +183,10 @@ remove_from_bridge()
            # Nothing to do
            ;;
        vswitch)
-        $vsctl del-port $bridge $dev
+            # 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 -- --if-exists del-port $dev
            ;;
     esac
 }