Don't try to add the same interface to the bridge twice, and clean up old routing...
[invirt/packages/invirt-xen-config.git] / vif-invirtroute
index 4663664..f473ac9 100755 (executable)
@@ -14,6 +14,7 @@
 #
 # Environment vars:
 # vif         vif interface name (required).
+# dev         qemu interface name (optional)
 # 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 (default given in
@@ -30,20 +31,29 @@ dir=$(dirname "$0")
 main_ip=$(dom0_ip)
 dev=${dev:-${vif}}
 
+brname=dom${vif#vif}
+
 case "$command" in
     online)
-        ifconfig ${dev} ${main_ip} netmask 255.255.255.255 up
-        echo 1 >/proc/sys/net/ipv4/conf/${dev}/proxy_arp
-       echo 1 >/proc/sys/net/ipv4/conf/${dev}/rp_filter 
-       xenstore-write "$XENBUS_PATH/feature-gso-tcpv4" 0
-        if [ x${qemu_online} != xyes ]; then
-          ethtool -K ${dev} tx off
+        create_bridge ${brname}
+        setup_bridge_port ${dev}
+        add_to_bridge ${brname} ${dev}
+        if [ "$dev" != "$vif" ]; then
+            setup_bridge_port ${vif}
+            add_to_bridge ${brname} ${vif}
         fi
+        ifconfig ${brname} ${main_ip} netmask 255.255.255.255 up
+        echo 1 >/proc/sys/net/ipv4/conf/${brname}/proxy_arp
+        echo 1 >/proc/sys/net/ipv4/conf/${brname}/rp_filter
+        xenstore-write "$XENBUS_PATH/feature-gso-tcpv4" 0
+        ethtool -K ${vif} tso off
         ipcmd='add'
         cmdprefix=''
         ;;
     offline)
-        do_without_error ifdown ${vif}
+        do_without_error brctl delif ${brname} ${vif}
+        do_without_error brctl delif ${brname} ${dev}
+        do_without_error ifconfig ${brname} down
         ipcmd='del'
         cmdprefix='do_without_error'
         ;;
@@ -55,7 +65,10 @@ if [  ${vif_type} != "ioemu"  -o  x${qemu_online} = xyes ] ; 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
-           ${cmdprefix} ip route ${ipcmd} ${addr} dev ${dev} src ${main_ip}
+           ${cmdprefix} ip route ${ipcmd} ${addr} dev ${brname} src ${main_ip}
+           if [ "$ipcmd" == "del" ]; then
+               do_without_error ip route del ${addr} dev ${dev} src ${main_ip}
+           fi
            if [ "$command" == "online" ]; then
                arpspoof -i $(invirt-getconf xen.iface) -t 18.181.0.1 ${addr}&
                sleep 5
@@ -65,6 +78,11 @@ if [  ${vif_type} != "ioemu"  -o  x${qemu_online} = xyes ] ; then
     fi
 fi
 
+if [ "$command" == "offline" ]
+then
+    do_without_error brctl delbr ${brname}
+fi
+
 log debug "Successful vif-route $command for $vif."
 if [ "$command" == "online" ]
 then