[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-changelog] New network-bridge script and associated gubbins.



# HG changeset patch
# User emellor@xxxxxxxxxxxxxxxxxxxxxx
# Node ID 18eb059ae47159b8c89eae880ed939245934c751
# Parent  b0f61c2c07462185096aa1708f0b49c2399304fe
New network-bridge script and associated gubbins.

This is Kurt Garloff's reworked network-bridge script:

* we got rid of ifconfig
* it works for netdev != eth0
* arp on and off are symmetric as are ifdown and ifup
* ifup will be passed the ifcfg config file name if needed
  (the ifup may otherwise figure that the veth0 hardware is
   NOT the same as the original ${netdev} and not use the same
   config -- this happens on SUSE. Charles Coffing tracked this
   one down.)

Plus Kurt's avoid-dash patch:

the network setup scripts on SUSE have trouble with the bridge
name xen-br0; they don't expect the '-'.
Arguably this should be fixed.
But I assume there's more scripts out there which may not like it,
so I suggest the following patch to rename xen-br0 to xenbr0.

Plus Charles Duffy's patch to support multiple bridges:

The attached patch allows the network-bridge script to be used to
generate multiple bridges corresponding to different physical
interfaces. It adds a new parameter, "vifnum", used to refer both to
the loopback interface to be used and to set defaults regarding the
physical interface and bridge name.

Thus, if one wishes to start xenbr0 on eth0 and xenbr1 on eth1, one
need only call:

network-bridge start ## vifnum is 0 by default
network-bridge start vifnum=1

...well, that and set loopback.nloopbacks=2 in the Dom0 kernel
parameters.

Plus renaming of virtnum to vifnum in Charles' patch, as requested by Ian Pratt.

Plus a fix to DevController to allocate vif IDs starting from 0 (i.e. vif2.0
is now domain 2's first vif, as opposed to vif2.1 in the recent past).

Plus tidying up inside network-bridge using some helper variables.

Signed-off-by: Ewan Mellor <ewan@xxxxxxxxxxxxx>

diff -r b0f61c2c0746 -r 18eb059ae471 tools/examples/network-bridge
--- a/tools/examples/network-bridge     Wed Oct 19 13:00:37 2005
+++ b/tools/examples/network-bridge     Wed Oct 19 15:24:54 2005
@@ -5,8 +5,8 @@
 # The script name to use is defined in /etc/xen/xend-config.sxp
 # in the network-script field.
 #
-# This script creates a bridge (default xen-br0), adds a device
-# (default eth0) to it, copies the IP addresses from the device
+# This script creates a bridge (default xenbr${vifnum}), adds a device
+# (default eth${vifnum}) to it, copies the IP addresses from the device
 # to the bridge and adjusts the routes accordingly.
 #
 # If all goes well, this should ensure that networking stays up.
@@ -20,9 +20,12 @@
 #
 # Vars:
 #
-# bridge     The bridge to use (default xen-br0).
-# netdev     The interface to add to the bridge (default eth0).
-# antispoof  Whether to use iptables to prevent spoofing (default yes).
+# vifnum     Virtual device number to use (default 0). Numbers >=1
+#            require the netback driver to have nloopbacks set to a
+#            higher value than its default of 1.
+# bridge     The bridge to use (default xenbr${vifnum}).
+# netdev     The interface to add to the bridge (default eth${vifnum}).
+# antispoof  Whether to use iptables to prevent spoofing (default no).
 #
 # start:
 # Creates the bridge and enslaves netdev to it.
@@ -60,11 +63,37 @@
 # Pull variables in args in to environment.
 for arg ; do export "${arg}" ; done
 
-bridge=${bridge:-xen-br0}
-netdev=${netdev:-eth0}
+vifnum=${vifnum:-0}
+bridge=${bridge:-xenbr${vifnum}}
+netdev=${netdev:-eth${vifnum}}
 antispoof=${antispoof:-no}
 
-echo "*network $OP bridge=$bridge netdev=$netdev antispoof=$antispoof" >&2
+pdev="p${netdev}"
+vdev="veth${vifnum}"
+vif0="vif0.${vifnum}"
+
+echo "*network $OP bridge=$bridge netdev=$netdev antispoof=$antispoof 
vifnum=$vifnum" >&2
+
+legacy_mask_to_prefix() {
+    mask=$1
+    first=${mask%%.*}
+    second=${mask#*.}
+    third=${second#*.}
+    fourth=${third#*.}
+    second=${second%%.*}
+    third=${third%%.*}
+    declare -i INT FULLMASK BIT
+    INT=$((((($first*256)+$second)*256+$third)*256+$fourth))
+    FULLMASK=4294967295
+    BIT=1
+    for bit in `seq 32 -1 0`; do
+       if test $FULLMASK -eq $INT; then PREFIX=$bit; return; fi
+       FULLMASK=$(($FULLMASK-$BIT))
+       BIT=$((BIT*2))
+    done
+    echo "ERROR converting netmask $mask to prefix"
+    exit 1
+}
 
 # Usage: transfer_addrs src dst
 # Copy all IP addresses (including aliases) from device $src to device $dst.
@@ -99,6 +128,7 @@
 s@\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\)/[0-9]\+@\1@
 s/${src}/dev ${src}/
 " | sh -e
+    ip link set dev ${dst} up
 }
 
 # Usage: transfer_routes src dst
@@ -131,11 +161,11 @@
 
     # Don't create the bridge if it already exists.
     if ! brctl show | grep -q ${bridge} ; then
-        brctl addbr ${bridge}
-        brctl stp ${bridge} off
-        brctl setfd ${bridge} 0
-    fi
-    ifconfig ${bridge} up
+       brctl addbr ${bridge}
+       brctl stp ${bridge} off
+       brctl setfd ${bridge} 0
+    fi
+    ip link set ${bridge} up
 }
 
 # Usage: add_to_bridge bridge dev
@@ -144,7 +174,7 @@
     local dev=$2
     # Don't add $dev to $bridge if it's already on a bridge.
     if ! brctl show | grep -q ${dev} ; then
-        brctl addif ${bridge} ${dev}
+       brctl addif ${bridge} ${dev}
     fi
 }
 
@@ -166,8 +196,10 @@
     local bridge=$2
     
     echo '============================================================'
-    ifconfig ${dev}
-    ifconfig ${bridge}
+    ip addr show ${dev}
+    ip addr show ${bridge}
+    echo ' '
+    brctl show ${bridge}
     echo ' '
     ip route list
     echo ' '
@@ -177,100 +209,113 @@
 
 op_start () {
     if [ "${bridge}" == "null" ] ; then
-        return
+       return
     fi
 
     create_bridge ${bridge}
 
-    if ifconfig 2>/dev/null | grep -q veth0 ; then
-        return
-    fi
-
-    if ifconfig veth0 2>/dev/null | grep -q veth0 ; then
-       mac=`ifconfig ${netdev} | grep HWadd | sed -e 
's/.*\(..:..:..:..:..:..\).*/\1/'`
-       if ! ifdown ${netdev} ; then
-               # if ifup didn't work, see if we have an ip= on cmd line
-               if egrep 'ip=[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:' /proc/cmdline ; 
-               then
-                        kip=`sed -e 
's!.*ip=\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\):.*!\1!' /proc/cmdline`
-                        kmask=`sed -e 
's!.*ip=[^:]*:[^:]*:[^:]*:\([^:]*\):.*!\1!' /proc/cmdline` 
-                        kgate=`sed -e 's!.*ip=[^:]*:[^:]*:\([^:]*\):.*!\1!' 
/proc/cmdline`
-                       ifconfig ${netdev} 0.0.0.0 down
+    if ! ip link show 2>/dev/null | grep -q "^[0-9]*: ${vdev}"; then
+       return
+    fi
+
+    if ip link show ${vdev} 2>/dev/null >/dev/null; then
+       mac=`ip link show ${netdev} | grep 'link\/ether' | sed -e 's/.*ether 
\(..:..:..:..:..:..\).*/\1/'`
+       eval `/sbin/getcfg -d /etc/sysconfig/network/ -f ifcfg- -- ${netdev}`
+       transfer_addrs ${netdev} ${vdev}
+       if ! ifdown ${netdev}; then
+           # if ifup didn't work, see if we have an ip= on cmd line
+           if egrep 'ip=[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:' /proc/cmdline; then
+               kip=`sed -e 
's!.*ip=\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\):.*!\1!' /proc/cmdline`
+               kmask=`sed -e 's!.*ip=[^:]*:[^:]*:[^:]*:\([^:]*\):.*!\1!' 
/proc/cmdline` 
+               kgate=`sed -e 's!.*ip=[^:]*:[^:]*:\([^:]*\):.*!\1!' 
/proc/cmdline`
+               ip link set ${netdev} down
+               ip addr flush ${netdev}
+           fi
+       fi
+       ip link set ${netdev} name ${pdev}
+       ip link set ${vdev} name ${netdev}
+       ip link set ${pdev} down arp off
+       ip link set ${pdev} addr fe:ff:ff:ff:ff:ff
+       ip addr flush ${pdev}
+       ip link set ${netdev} addr ${mac} arp on
+       add_to_bridge ${bridge} ${vif0}
+       add_to_bridge ${bridge} ${pdev}
+       ip link set ${bridge} up
+       ip link set ${vif0} up
+       ip link set ${pdev} up 
+       if ! ifup ${HWD_CONFIG_0} ${netdev} ; then
+           if [ ${kip} ] ; then
+               # use the addresses we grocked from /proc/cmdline
+               if [ -z "${kmask}" ]; then 
+                   PREFIX=32; 
+               else 
+                   legacy_to_prefix ${kmask};
                fi
+               ip addr add ${kip}/${PREFIX} dev ${netdev}
+               ip link set dev ${netdev} up
+               [ ${kgate} ] && ip route add default via ${kgate}
+           fi
        fi
-       ip link set ${netdev} name p${netdev}
-       ip link set veth0 name ${netdev}
-       ifconfig p${netdev} 0.0.0.0 -arp down
-       ifconfig p${netdev} hw ether fe:ff:ff:ff:ff:ff
-       ifconfig ${netdev} hw ether ${mac}
-       add_to_bridge ${bridge} vif0.0
-       add_to_bridge ${bridge} p${netdev}
-       ip link set ${bridge} up
-       ip link set vif0.0 up
-       ip link set p${netdev} up
-       if ! ifup ${netdev} ; then
-               if [ ${kip} ] ; then
-                       # use the addresses we grocked from /proc/cmdline       
-                       ifconfig ${netdev} ${kip} 
-                       [ ${kmask} ] && ifconfig ${netdev} netmask ${kmask} 
-                       ifconfig ${netdev} up
-                       [ ${kgate} ] && ip route add default via ${kgate}       
-               fi
-        fi
     else
-       # old style without veth0
-       transfer_addrs ${netdev} ${bridge}
-        transfer_routes ${netdev} ${bridge}
-    fi
-    
+       # old style without ${vdev}
+       transfer_addrs  ${netdev} ${bridge}
+       transfer_routes ${netdev} ${bridge}
+    fi
+
     if [ ${antispoof} == 'yes' ] ; then
-        antispoofing ${netdev} ${bridge}
+       antispoofing ${netdev} ${bridge}
     fi
 }
 
 op_stop () {
-    if [ "${bridge}" == "null" ] ; then
-        return
-    fi
-
-    if ifconfig peth0 2>/dev/null | grep -q peth0 ; then
-
-        ifconfig vif0.0 down
-        mac=`ifconfig eth0 | grep HWadd | \
-            sed -e 's/.*\(..:..:..:..:..:..\).*/\1/'`
-        ifconfig ${netdev} 0.0.0.0 down
-        ifconfig ${netdev} hw ether fe:ff:ff:ff:ff:ff
-
-        ifconfig p${netdev} down
-        ifconfig p${netdev} hw ether ${mac} arp 
-        brctl delif ${bridge} p${netdev}
-
-        ip link set eth0 name veth0
-        ip link set peth0 name eth0
-        ifconfig ${bridge} down
-        brctl delbr ${bridge}
-        ifup eth0
+    if [ "${bridge}" == "null" ]; then
+       return
+    fi
+    if ! ip link show ${bridge} >/dev/null 2>&1; then
+       return
+    fi
+
+    if ip link show ${pdev} 2>/dev/null >/dev/null; then
+       ip link set dev ${vif0} down
+       mac=`ip link show ${netdev} | grep 'link\/ether' | sed -e 's/.*ether 
\(..:..:..:..:..:..\).*/\1/'`
+       transfer_addrs ${netdev} ${pdev}
+       ifdown ${netdev}
+       ip link set ${netdev} down arp off
+       ip link set ${netdev} addr fe:ff:ff:ff:ff:ff
+       ip link set ${pdev} down
+       ip addr flush ${netdev}
+       ip link set ${pdev} addr ${mac} arp on
+
+       brctl delif ${bridge} ${pdev}
+       brctl delif ${bridge} ${vif0}
+       ip link set ${bridge} down
+
+       ip link set ${netdev} name ${vdev}
+       ip link set ${pdev} name ${netdev}
+       ifup ${netdev}
 
     else
-        transfer_routes ${bridge} ${netdev}
-    fi
+       transfer_routes ${bridge} ${netdev}
+       ip link set ${bridge} down
+    fi
+    brctl delbr ${bridge}
 }
 
 case ${OP} in
     start)
-        op_start
-        ;;
+       op_start
+       ;;
     
     stop)
-        op_stop
-        ;;
+       op_stop
+       ;;
 
     status)
-        show_status ${netdev} ${bridge}
-       ;;
+       show_status ${netdev} ${bridge}
+       ;;
 
     *)
-       echo 'Unknown command: ' ${OP} >&2
-       echo 'Valid commands are: start, stop, status' >&2
-       exit 1
+       echo 'Unknown command: ' ${OP} >&2
+       echo 'Valid commands are: start, stop, status' >&2
+       exit 1
 esac
diff -r b0f61c2c0746 -r 18eb059ae471 tools/examples/vif-bridge
--- a/tools/examples/vif-bridge Wed Oct 19 13:00:37 2005
+++ b/tools/examples/vif-bridge Wed Oct 19 15:24:54 2005
@@ -8,7 +8,7 @@
 #
 # Example invocation:
 #
-# vif-bridge up domain=VM1 vif=vif1.0 bridge=xen-br0 ip="128.232.38.45/28 
10.10.10.55/24"
+# vif-bridge up domain=VM1 vif=vif1.0 bridge=xenbr0 ip="128.232.38.45/28 
10.10.10.55/24"
 #
 #
 # Usage:
diff -r b0f61c2c0746 -r 18eb059ae471 tools/examples/xend-config.sxp
--- a/tools/examples/xend-config.sxp    Wed Oct 19 13:00:37 2005
+++ b/tools/examples/xend-config.sxp    Wed Oct 19 15:24:54 2005
@@ -33,7 +33,7 @@
 # The script used to start/stop networking for xend.
 (network-script    network-bridge)
 # The default bridge that virtual interfaces should be connected to.
-(vif-bridge        xen-br0)
+(vif-bridge        xenbr0)
 # The default script used to control virtual interfaces.
 (vif-script        vif-bridge)
 
diff -r b0f61c2c0746 -r 18eb059ae471 tools/examples/xmexample.vmx
--- a/tools/examples/xmexample.vmx      Wed Oct 19 13:00:37 2005
+++ b/tools/examples/xmexample.vmx      Wed Oct 19 15:24:54 2005
@@ -35,7 +35,7 @@
 
 # Optionally define mac and/or bridge for the network interfaces.
 # Random MACs are assigned if not given.
-#vif = [ 'mac=aa:00:00:00:00:11, bridge=xen-br0' ]
+#vif = [ 'mac=aa:00:00:00:00:11, bridge=xenbr0' ]
 
 #----------------------------------------------------------------------------
 # Define the disk devices you want the domain to have access to, and
diff -r b0f61c2c0746 -r 18eb059ae471 tools/examples/xmexample1
--- a/tools/examples/xmexample1 Wed Oct 19 13:00:37 2005
+++ b/tools/examples/xmexample1 Wed Oct 19 15:24:54 2005
@@ -36,7 +36,7 @@
 
 # Optionally define mac and/or bridge for the network interfaces.
 # Random MACs are assigned if not given.
-#vif = [ 'mac=aa:00:00:00:00:11, bridge=xen-br0' ]
+#vif = [ 'mac=aa:00:00:00:00:11, bridge=xenbr0' ]
 
 #----------------------------------------------------------------------------
 # Define the disk devices you want the domain to have access to, and
diff -r b0f61c2c0746 -r 18eb059ae471 tools/examples/xmexample2
--- a/tools/examples/xmexample2 Wed Oct 19 13:00:37 2005
+++ b/tools/examples/xmexample2 Wed Oct 19 15:24:54 2005
@@ -67,7 +67,7 @@
 
 # Optionally define mac and/or bridge for the network interfaces.
 # Random MACs are assigned if not given.
-#vif = [ 'mac=aa:00:00:00:00:11, bridge=xen-br0' ]
+#vif = [ 'mac=aa:00:00:00:00:11, bridge=xenbr0' ]
 
 #----------------------------------------------------------------------------
 # Define the disk devices you want the domain to have access to, and
diff -r b0f61c2c0746 -r 18eb059ae471 tools/ioemu/target-i386-dm/qemu-ifup
--- a/tools/ioemu/target-i386-dm/qemu-ifup      Wed Oct 19 13:00:37 2005
+++ b/tools/ioemu/target-i386-dm/qemu-ifup      Wed Oct 19 15:24:54 2005
@@ -7,4 +7,4 @@
 echo $*
 
 ifconfig $1 0.0.0.0 up
-brctl addif xen-br0 $1
+brctl addif xenbr0 $1
diff -r b0f61c2c0746 -r 18eb059ae471 tools/misc/netfix
--- a/tools/misc/netfix Wed Oct 19 13:00:37 2005
+++ b/tools/misc/netfix Wed Oct 19 15:24:54 2005
@@ -3,7 +3,7 @@
 #============================================================================
 # Copyright (C) 2004 Mike Wray <mike.wray@xxxxxx>
 #============================================================================
-# Move the IP address from eth0 onto the Xen bridge (xen-br0).
+# Move the IP address from eth0 onto the Xen bridge (xenbr0).
 # Only works if the bridge control utils (brctl) have been installed.
 #============================================================================
 
@@ -19,7 +19,7 @@
                  'interface=', 'bridge=', 'create']
 
 defaults['interface'] = 'eth0'
-defaults['bridge'] = 'xen-br0'
+defaults['bridge'] = 'xenbr0'
 
 def usage():
     print """Usage:
diff -r b0f61c2c0746 -r 18eb059ae471 tools/python/xen/xend/XendRoot.py
--- a/tools/python/xen/xend/XendRoot.py Wed Oct 19 13:00:37 2005
+++ b/tools/python/xen/xend/XendRoot.py Wed Oct 19 15:24:54 2005
@@ -253,7 +253,7 @@
         return self.get_config_bool('enable-dump', 'no')
 
     def get_vif_bridge(self):
-        return self.get_config_value('vif-bridge', 'xen-br0')
+        return self.get_config_value('vif-bridge', 'xenbr0')
 
     def get_vif_script(self):
         return self.get_config_value('vif-script', 'vif-bridge')
diff -r b0f61c2c0746 -r 18eb059ae471 
tools/python/xen/xend/server/DevController.py
--- a/tools/python/xen/xend/server/DevController.py     Wed Oct 19 13:00:37 2005
+++ b/tools/python/xen/xend/server/DevController.py     Wed Oct 19 15:24:54 2005
@@ -160,7 +160,7 @@
                 if result:
                     result = int(result)
                 else:
-                    result = 1
+                    result = 0
                 t.write("nextDeviceID", str(result + 1))
                 if t.commit():
                     return result
diff -r b0f61c2c0746 -r 18eb059ae471 tools/python/xen/xend/server/SrvDaemon.py
--- a/tools/python/xen/xend/server/SrvDaemon.py Wed Oct 19 13:00:37 2005
+++ b/tools/python/xen/xend/server/SrvDaemon.py Wed Oct 19 15:24:54 2005
@@ -32,34 +32,6 @@
         self.traceindent = 0
         self.child = 0 
         
-    def daemon_pids(self):
-        pids = []
-        pidex = '(?P<pid>\d+)'
-        pythonex = '(?P<python>\S*python\S*)'
-        cmdex = '(?P<cmd>.*)'
-        procre = re.compile('^\s*' + pidex + '\s*' + pythonex + '\s*' + cmdex 
+ '$')
-        xendre = re.compile('^\S+/xend\s*(start|restart)\s*.*$')
-        procs = os.popen('ps -e -o pid,args 2>/dev/null')
-        for proc in procs:
-            pm = procre.match(proc)
-            if not pm: continue
-            xm = xendre.match(pm.group('cmd'))
-            if not xm: continue
-            pids.append(int(pm.group('pid')))
-        return pids
-
-    def new_cleanup(self, kill=0):
-        err = 0
-        pids = self.daemon_pids()
-        if kill:
-            for pid in pids:
-                print "Killing daemon pid=%d" % pid
-                os.kill(pid, signal.SIGHUP)
-        elif pids:
-            err = 1
-            print "Daemon already running: ", pids
-        return err
-
     def read_pid(self, pidfile):
         """Read process id from a file.
 
@@ -115,11 +87,8 @@
             os.remove(pidfile)
         return running
 
-    def cleanup_xend(self, kill=False):
+    def cleanup_xend(self, kill):
         return self.cleanup_process(XEND_PID_FILE, "xend", kill)
-
-    def cleanup(self, kill=False):
-        self.cleanup_xend(kill=kill)
 
     def status(self):
         """Returns the status of the xend daemon.
@@ -156,16 +125,13 @@
         # Detach from TTY.
         os.setsid()
 
-        # Detach from standard file descriptors.
-        # I do this at the file-descriptor level: the overlying Python file
-        # objects also use fd's 0, 1 and 2.
+        # Detach from standard file descriptors, and redirect them to
+        # /dev/null or the log as appropriate.
         os.close(0)
         os.close(1)
         os.close(2)
         if XEND_DEBUG:
             os.open('/dev/null', os.O_RDONLY)
-            # XXX KAF: Why doesn't this capture output from C extensions that
-            # fprintf(stdout) or fprintf(stderr) ??
             os.open(XEND_DEBUG_LOG, os.O_WRONLY|os.O_CREAT)
             os.dup(1)
         else:
@@ -180,7 +146,7 @@
         0  Success
         4  Insufficient privileges
         """
-        xend_pid = self.cleanup_xend()
+        xend_pid = self.cleanup_xend(False)
 
         if self.set_user():
             return 4
@@ -294,7 +260,10 @@
             return 1
 
     def stop(self):
-        return self.cleanup(kill=True)
+        result = self.cleanup_xend(True)
+        from xen.xend import Vifctl
+        Vifctl.network("stop")
+        return result
 
     def run(self, status):
         try:
diff -r b0f61c2c0746 -r 18eb059ae471 tools/vnet/vnet-module/varp.c
--- a/tools/vnet/vnet-module/varp.c     Wed Oct 19 13:00:37 2005
+++ b/tools/vnet/vnet-module/varp.c     Wed Oct 19 15:24:54 2005
@@ -176,7 +176,7 @@
 /** UDP port (network order). */
 u16 varp_port = 0;
 
-char *varp_device = "xen-br0";
+char *varp_device = "xenbr0";
 
 #define VarpTable_read_lock(z, flags)    do{ (flags) = 0; down(&(z)->lock); } 
while(0)
 #define VarpTable_read_unlock(z, flags)  do{ (flags) = 0; up(&(z)->lock); } 
while(0)

_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.