ia64/xen-unstable

changeset 1550:f9d7fa707de4

bitkeeper revision 1.1003 (40d8b8dc_YbEBn1ToXKOIMOG5-RsRw)

Merge labyrinth.cl.cam.ac.uk:/auto/groups/xeno/BK/xeno.bk
into labyrinth.cl.cam.ac.uk:/auto/anfs/scratch/labyrinth/iap10/xeno-clone/xeno.bk
author iap10@labyrinth.cl.cam.ac.uk
date Tue Jun 22 22:55:24 2004 +0000 (2004-06-22)
parents c4b72645fb6b 7c46576a93c1
children f023d40722e4 8d6a0fcaa924
files .rootkeys linux-2.4.26-xen-sparse/arch/xen/drivers/netif/backend/interface.c tools/xenmgr/lib/XendBridge.py tools/xenmgr/lib/XendDomain.py tools/xenmgr/lib/XendDomainInfo.py tools/xenmgr/lib/server/SrvConsoleServer.py tools/xenmgr/lib/server/SrvServer.py tools/xenmgr/lib/server/blkif.py tools/xenmgr/lib/server/netif.py tools/xenmgr/netfix
line diff
     1.1 --- a/.rootkeys	Tue Jun 22 18:59:03 2004 +0000
     1.2 +++ b/.rootkeys	Tue Jun 22 22:55:24 2004 +0000
     1.3 @@ -231,6 +231,7 @@ 40c9c468SNuObE_YWARyS0hzTPSzKg tools/xen
     1.4  40c9c468Um_qc66OQeLEceIz1pgD5g tools/xenmgr/lib/EventServer.py
     1.5  40c9c468U8EVl0d3G--8YXVg6VJD3g tools/xenmgr/lib/EventTypes.py
     1.6  40c9c468QJTEuk9g4qHxGpmIi70PEQ tools/xenmgr/lib/PrettyPrint.py
     1.7 +40d8915cyoVA0hJxiBFNymL7YvDaRg tools/xenmgr/lib/XendBridge.py
     1.8  40c9c4688m3eqnC8fhLu1APm36VOVA tools/xenmgr/lib/XendClient.py
     1.9  40c9c468t6iIKTjwuYoe-UMCikDcOQ tools/xenmgr/lib/XendConsole.py
    1.10  40c9c468WnXs6eOUSff23IIGI4kMfQ tools/xenmgr/lib/XendDB.py
     2.1 --- a/linux-2.4.26-xen-sparse/arch/xen/drivers/netif/backend/interface.c	Tue Jun 22 18:59:03 2004 +0000
     2.2 +++ b/linux-2.4.26-xen-sparse/arch/xen/drivers/netif/backend/interface.c	Tue Jun 22 22:55:24 2004 +0000
     2.3 @@ -13,8 +13,10 @@
     2.4  #define NETIF_HASH(_d,_h) (((int)(_d)^(int)(_h))&(NETIF_HASHSZ-1))
     2.5  
     2.6  static netif_t *netif_hash[NETIF_HASHSZ];
     2.7 +#ifdef XEN_BRIDGE
     2.8  static struct net_device *bridge_dev;
     2.9  static struct net_bridge *bridge_br;
    2.10 +#endif
    2.11  
    2.12  netif_t *netif_find_by_handle(domid_t domid, unsigned int handle)
    2.13  {
    2.14 @@ -38,7 +40,9 @@ void __netif_disconnect_complete(netif_t
    2.15      unbind_evtchn_from_irq(netif->evtchn);
    2.16      vfree(netif->tx); /* Frees netif->rx as well. */
    2.17      rtnl_lock();
    2.18 +#ifdef XEN_BRIDGE
    2.19      (void)br_del_if(bridge_br, netif->dev);
    2.20 +#endif
    2.21      (void)dev_close(netif->dev);
    2.22      rtnl_unlock();
    2.23  
    2.24 @@ -240,6 +244,7 @@ void netif_connect(netif_be_connect_t *c
    2.25      rtnl_lock();
    2.26  
    2.27      (void)dev_open(netif->dev);
    2.28 +#ifdef XEN_BRIDGE
    2.29      (void)br_add_if(bridge_br, netif->dev);
    2.30  
    2.31      /*
    2.32 @@ -254,6 +259,7 @@ void netif_connect(netif_be_connect_t *c
    2.33          (void)dev_open(eth0_dev);
    2.34          (void)br_add_if(bridge_br, eth0_dev);
    2.35      }
    2.36 +#endif
    2.37  
    2.38      rtnl_unlock();
    2.39  
    2.40 @@ -295,6 +301,7 @@ int netif_disconnect(netif_be_disconnect
    2.41  void netif_interface_init(void)
    2.42  {
    2.43      memset(netif_hash, 0, sizeof(netif_hash));
    2.44 +#ifdef XEN_BRIDGE
    2.45      if ( br_add_bridge("nbe-br") != 0 )
    2.46          BUG();
    2.47      bridge_dev = __dev_get_by_name("nbe-br");
    2.48 @@ -302,14 +309,17 @@ void netif_interface_init(void)
    2.49      bridge_br->bridge_hello_time = bridge_br->hello_time = 0;
    2.50      bridge_br->bridge_forward_delay = bridge_br->forward_delay = 0;
    2.51      bridge_br->stp_enabled = 0;
    2.52 +#endif
    2.53  }
    2.54  
    2.55  #ifndef CONFIG_BRIDGE
    2.56  #error Must configure Ethernet bridging in Network Options
    2.57  #endif
    2.58 +#ifndef XEN_BRIDGE
    2.59  EXPORT_SYMBOL(br_add_bridge);
    2.60  EXPORT_SYMBOL(br_del_bridge);
    2.61  EXPORT_SYMBOL(br_add_if);
    2.62  EXPORT_SYMBOL(br_del_if);
    2.63  EXPORT_SYMBOL(br_get_bridge_ifindices);
    2.64  EXPORT_SYMBOL(br_get_port_ifindices);
    2.65 +#endif
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/tools/xenmgr/lib/XendBridge.py	Tue Jun 22 22:55:24 2004 +0000
     3.3 @@ -0,0 +1,182 @@
     3.4 +"""Bridge control utilities.
     3.5 +"""
     3.6 +import os
     3.7 +import os.path
     3.8 +import re
     3.9 +import sys
    3.10 +
    3.11 +from xenmgr import XendRoot
    3.12 +xroot = XendRoot.instance()
    3.13 +
    3.14 +os.defpath = os.defpath + ':/sbin:/usr/sbin:/usr/local/sbin'
    3.15 +CMD_IFCONFIG = 'ifconfig'
    3.16 +CMD_ROUTE    = 'route'
    3.17 +CMD_BRCTL    = 'brctl'
    3.18 +
    3.19 +DEFAULT_BRIDGE = 'nbe-br'
    3.20 +DEFAULT_INTERFACE = 'eth0'
    3.21 +
    3.22 +opts = None
    3.23 +
    3.24 +class Opts:
    3.25 +
    3.26 +    def __init__(self, defaults):
    3.27 +        for (k, v) in defaults.items():
    3.28 +            setattr(self, k, v)
    3.29 +        pass
    3.30 +
    3.31 +def cmd(p, s):
    3.32 +    """Print and execute command 'p' with args 's'.
    3.33 +    """
    3.34 +    global opts
    3.35 +    c = p + ' ' + s
    3.36 +    if opts.verbose: print c
    3.37 +    if not opts.dryrun:
    3.38 +        os.system(c)
    3.39 +
    3.40 +def default_bridge():
    3.41 +    return xroot.get_config_value('bridge', DEFAULT_BRIDGE)
    3.42 +
    3.43 +def default_interface():
    3.44 +    return xroot.get_config_value('interface', DEFAULT_INTERFACE)
    3.45 +
    3.46 +def vif_dev(dom, vif):
    3.47 +    """Return the name of the network interface for vif on domain dom.
    3.48 +    """
    3.49 +    return "vif%d.%d" % (dom, vif)
    3.50 +
    3.51 +def vif_bridge_add(dom, vif, bridge=None):
    3.52 +    """Add the network interface for vif on dom to a bridge.
    3.53 +    """
    3.54 +    if not bridge: bridge = default_bridge()
    3.55 +    d = { 'bridge': bridge, 'vif': vif_dev(dom, vif) }
    3.56 +    cmd(CMD_BRCTL, 'addif %(bridge)s %(vif)s' % d)
    3.57 +    return bridge
    3.58 +
    3.59 +def vif_bridge_rem(dom, vif, bridge=None):
    3.60 +    """Remove the network interface for vif on dom from a bridge.
    3.61 +    """
    3.62 +    if not bridge: bridge = default_bridge()
    3.63 +    print 'vif_bridge_rem>', dom, vif, bridge
    3.64 +    d = { 'bridge': bridge, 'vif': vif_dev(dom, vif) }
    3.65 +    cmd(CMD_BRCTL, 'delif %(bridge)s %(vif)s' % d)
    3.66 +
    3.67 +def bridge_create(bridge=None, **kwd):
    3.68 +    """Create a bridge.
    3.69 +    Defaults hello time to 0, forward delay to 0 and stp off.
    3.70 +    """
    3.71 +    if not bridge: bridge = default_bridge()
    3.72 +    cmd(CMD_BRCTL, 'addbr %s' % bridge)
    3.73 +    if kwd.get('hello', None) is None:
    3.74 +        kwd['hello'] = 0
    3.75 +    if kwd.get('fd', None) is None:
    3.76 +        kwd['fd'] = 0
    3.77 +    if kwd.get('stp', None) is None:
    3.78 +        kwd['stp'] = 'off'
    3.79 +    bridge_set(bridge, **kwd)
    3.80 +
    3.81 +def bridge_set(bridge, hello=None, fd=None, stp=None):
    3.82 +    """Set bridge parameters.
    3.83 +    """
    3.84 +    if hello is not None:
    3.85 +        cmd(CMD_BRCTL, 'sethello %s %d' % (bridge, hello))
    3.86 +    if fd is not None:
    3.87 +        cmd(CMD_BRCTL, 'setfd %s %d' % (bridge, fd))
    3.88 +    if stp is not None:
    3.89 +        cmd(CMD_BRCTL, 'stp %s %s' % (bridge, stp))
    3.90 +
    3.91 +def bridge_del(bridge=None):
    3.92 +    """Delete a bridge.
    3.93 +    """
    3.94 +    if not bridge: bridge = default_bridge()
    3.95 +    cmd(CMD_BRCTL, 'delbr %s' % bridge)
    3.96 +
    3.97 +def routes():
    3.98 +    """Return a list of the routes.
    3.99 +    """
   3.100 +    fin = os.popen(CMD_ROUTE + ' -n', 'r')
   3.101 +    routes = []
   3.102 +    for x in fin:
   3.103 +        if x.startswith('Kernel'): continue
   3.104 +        if x.startswith('Destination'): continue
   3.105 +        x = x.strip()
   3.106 +        y = x.split()
   3.107 +        z = { 'destination': y[0],
   3.108 +              'gateway'    : y[1],
   3.109 +              'mask'       : y[2],
   3.110 +              'flags'      : y[3],
   3.111 +              'metric'     : y[4],
   3.112 +              'ref'        : y[5],
   3.113 +              'use'        : y[6],
   3.114 +              'interface'  : y[7] }
   3.115 +        routes.append(z)
   3.116 +    return routes
   3.117 +
   3.118 +def ifconfig(interface):
   3.119 +    """Return the ip config for an interface,
   3.120 +    """
   3.121 +    fin = os.popen(CMD_IFCONFIG + ' %s' % interface, 'r')
   3.122 +    inetre = re.compile('\s*inet\s*addr:(?P<address>\S*)\s*Bcast:(?P<broadcast>\S*)\s*Mask:(?P<mask>\S*)')
   3.123 +    info = None
   3.124 +    for x in fin:
   3.125 +        m = inetre.match(x)
   3.126 +        if not m: continue
   3.127 +        info = m.groupdict()
   3.128 +        info['interface'] = interface
   3.129 +        break
   3.130 +    return info
   3.131 +
   3.132 +def reconfigure(interface=None, bridge=None):
   3.133 +    """Reconfigure an interface to be attached to a bridge, and give the bridge
   3.134 +    the IP address etc. from interface. Move the default route to the interface
   3.135 +    to the bridge.
   3.136 +
   3.137 +    If opts.create is true, creates the bridge.
   3.138 +    """
   3.139 +    global opts
   3.140 +    if not interface: interface = default_interface()
   3.141 +    if not bridge: bridge = default_bridge()
   3.142 +    intf_info = ifconfig(interface)
   3.143 +    if not intf_info:
   3.144 +        print 'Interface not found:', interface
   3.145 +        return
   3.146 +    if opts.create:
   3.147 +        bridge_create(bridge)
   3.148 +    #bridge_info = ifconfig(bridge)
   3.149 +    #if not bridge_info:
   3.150 +    #    print 'Bridge not found:', bridge
   3.151 +    #    return
   3.152 +    route_info = routes()
   3.153 +    intf_info['bridge'] = bridge
   3.154 +    intf_info['gateway'] = None
   3.155 +    for r in route_info:
   3.156 +        if (r['destination'] == '0.0.0.0' and
   3.157 +            'G' in r['flags'] and
   3.158 +            r['interface'] == interface):
   3.159 +            intf_info['gateway'] = r['gateway']
   3.160 +    if not intf_info['gateway']:
   3.161 +        print 'Gateway not found: ', interface
   3.162 +        return
   3.163 +    cmd(CMD_IFCONFIG,
   3.164 +        '%(bridge)s %(address)s netmask %(mask)s broadcast %(broadcast)s up'
   3.165 +        % intf_info)
   3.166 +    cmd(CMD_ROUTE,
   3.167 +        'add default gateway %(gateway)s dev %(bridge)s'
   3.168 +        % intf_info)
   3.169 +    cmd(CMD_BRCTL, 'addif %(bridge)s %(interface)s' % intf_info)
   3.170 +    cmd(CMD_IFCONFIG, '%(interface)s 0.0.0.0' % intf_info)
   3.171 +
   3.172 +defaults = {
   3.173 +    'interface': default_interface(),
   3.174 +    'bridge'   : default_bridge(),
   3.175 +    'verbose'  : 1,
   3.176 +    'dryrun'   : 0,
   3.177 +    'create'   : 0,
   3.178 +    }
   3.179 +
   3.180 +opts = Opts(defaults)
   3.181 +
   3.182 +def set_opts(val):
   3.183 +    global opts
   3.184 +    opts = val
   3.185 +    return opts
     4.1 --- a/tools/xenmgr/lib/XendDomain.py	Tue Jun 22 18:59:03 2004 +0000
     4.2 +++ b/tools/xenmgr/lib/XendDomain.py	Tue Jun 22 22:55:24 2004 +0000
     4.3 @@ -8,7 +8,6 @@ import sys
     4.4  
     4.5  import Xc; xc = Xc.new()
     4.6  import xenctl.ip
     4.7 -import xenctl.vdisk
     4.8  
     4.9  import sxp
    4.10  import XendRoot
    4.11 @@ -125,6 +124,7 @@ class XendDomain:
    4.12  
    4.13      def _delete_domain(self, id, notify=1):
    4.14          if id in self.domain:
    4.15 +            self.domain[id].died()
    4.16              if notify: eserver.inject('xend.domain.died', id)
    4.17              del self.domain[id]
    4.18          if id in self.domain_db:
     5.1 --- a/tools/xenmgr/lib/XendDomainInfo.py	Tue Jun 22 18:59:03 2004 +0000
     5.2 +++ b/tools/xenmgr/lib/XendDomainInfo.py	Tue Jun 22 22:55:24 2004 +0000
     5.3 @@ -17,7 +17,6 @@ import os
     5.4  from twisted.internet import defer
     5.5  
     5.6  import Xc; xc = Xc.new()
     5.7 -
     5.8  import xenctl.ip
     5.9  
    5.10  import sxp
    5.11 @@ -25,6 +24,8 @@ import sxp
    5.12  import XendConsole
    5.13  xendConsole = XendConsole.instance()
    5.14  
    5.15 +import XendBridge
    5.16 +
    5.17  import server.SrvConsoleServer
    5.18  xend = server.SrvConsoleServer.instance()
    5.19  
    5.20 @@ -125,7 +126,7 @@ class XendDomainInfo:
    5.21              sxpr.append(['state', state])
    5.22              if self.info['shutdown']:
    5.23                  reasons = ["poweroff", "reboot", "suspend"]
    5.24 -                reason = reasons[info['shutdown_reason']]
    5.25 +                reason = reasons[self.info['shutdown_reason']]
    5.26                  sxpr.append(['shutdown_reason', reason])
    5.27              sxpr.append(['cpu', self.info['cpu']])
    5.28              sxpr.append(['cpu_time', self.info['cpu_time']/1e9])
    5.29 @@ -183,6 +184,18 @@ class XendDomainInfo:
    5.30              return 0
    5.31          return xc.domain_destroy(dom=self.dom)
    5.32  
    5.33 +    def died(self):
    5.34 +        print 'died>', self.dom
    5.35 +        self.release_vifs()
    5.36 +
    5.37 +    def release_vifs(self):
    5.38 +        print 'release_vifs>', self.dom
    5.39 +        vifs = self.get_devices('vif')
    5.40 +        for v in vifs:
    5.41 +            vif = sxp.child_value(v, 'vif')
    5.42 +            bridge = sxp.child_value(v, 'bridge')
    5.43 +            XendBridge.vif_bridge_rem(self.dom, vif, bridge)
    5.44 +
    5.45      def show(self):
    5.46          """Print virtual machine info.
    5.47          """
    5.48 @@ -264,7 +277,7 @@ def lookup_disk_uname( uname ):
    5.49          segments = None
    5.50      return segments
    5.51  
    5.52 -def make_disk(dom, uname, dev, mode, sharing):
    5.53 +def make_disk(dom, uname, dev, mode):
    5.54      """Create a virtual disk device for a domain.
    5.55  
    5.56      @returns Deferred
    5.57 @@ -689,10 +702,14 @@ def vm_dev_vif(vm, val, index):
    5.58          raise VmError('vif: vif in control domain')
    5.59      vif = index #todo
    5.60      vmac = sxp.child_value(val, "mac")
    5.61 -    bridge = sxp.child_value(val, "bridge") # todo
    5.62      defer = make_vif(vm.dom, vif, vmac)
    5.63      def fn(id):
    5.64 -        dev = val + ['vif', vif]
    5.65 +        bridge = sxp.child_value(val, "bridge")
    5.66 +        bridge = XendBridge.vif_bridge_add(vm.dom, vif, bridge)
    5.67 +        dev = ['vif', ['vif', vif], ['bridge', bridge] ]
    5.68 +        netdev = xend.netif_dev(vm.dom, vif)
    5.69 +        if netdev and netdev.mac:
    5.70 +            dev += [ 'mac', netdev.mac ]
    5.71          vm.add_device('vif', dev)
    5.72          print 'vm_dev_vif> created', dev
    5.73          return id
    5.74 @@ -715,8 +732,7 @@ def vm_dev_vbd(vm, val, index):
    5.75      if not dev:
    5.76          raise VMError('vbd: Missing dev')
    5.77      mode = sxp.child_value(val, 'mode', 'r')
    5.78 -    sharing = sxp.child_value(val, 'sharing', 'rr')
    5.79 -    defer = make_disk(vm.dom, uname, dev, mode, sharing)
    5.80 +    defer = make_disk(vm.dom, uname, dev, mode)
    5.81      def fn(vbd):
    5.82          vm.add_device('vbd', val)
    5.83          return vbd
     6.1 --- a/tools/xenmgr/lib/server/SrvConsoleServer.py	Tue Jun 22 18:59:03 2004 +0000
     6.2 +++ b/tools/xenmgr/lib/server/SrvConsoleServer.py	Tue Jun 22 22:55:24 2004 +0000
     6.3 @@ -549,7 +549,7 @@ class Daemon:
     6.4          self.listenEvent()
     6.5          self.listenNotifier()
     6.6          self.listenVirq()
     6.7 -        SrvServer.create()
     6.8 +        SrvServer.create(bridge=1)
     6.9          reactor.run()
    6.10  
    6.11      def createFactories(self):
    6.12 @@ -602,6 +602,9 @@ class Daemon:
    6.13          d = self.blkifCF.createInstance(dom)
    6.14          return d
    6.15  
    6.16 +    def blkif_dev(self, dom, vdev):
    6.17 +        return self.blkifCF.getDomainDevice(dom, vdev)
    6.18 +
    6.19      def blkif_dev_create(self, dom, vdev, mode, segment):
    6.20          """Create a block device.
    6.21          
    6.22 @@ -641,6 +644,9 @@ class Daemon:
    6.23          d = ctrl.attach_device(vif, vmac)
    6.24          return d
    6.25  
    6.26 +    def netif_dev(self, dom, vif):
    6.27 +        return self.netifCF.getDomainDevice(dom, vif)
    6.28 +
    6.29      def console_create(self, dom, console_port=None):
    6.30          """Create a console for a domain.
    6.31          """
     7.1 --- a/tools/xenmgr/lib/server/SrvServer.py	Tue Jun 22 18:59:03 2004 +0000
     7.2 +++ b/tools/xenmgr/lib/server/SrvServer.py	Tue Jun 22 22:55:24 2004 +0000
     7.3 @@ -32,17 +32,24 @@ from twisted.internet import reactor
     7.4  from xenmgr import XendRoot
     7.5  xroot = XendRoot.instance()
     7.6  
     7.7 +from xenmgr import XendBridge
     7.8 +
     7.9  from SrvRoot import SrvRoot
    7.10  
    7.11 -def create(port=None, interface=None):
    7.12 +def create(port=None, interface=None, bridge=0):
    7.13      if port is None: port = 8000
    7.14      if interface is None: interface = ''
    7.15 +    if bridge or xroot.rebooted:
    7.16 +        init_bridge()
    7.17      root = resource.Resource()
    7.18      xend = SrvRoot()
    7.19      root.putChild('xend', xend)
    7.20      site = server.Site(root)
    7.21      reactor.listenTCP(port, site, interface=interface)
    7.22 -    
    7.23 +
    7.24 +def init_bridge():
    7.25 +    XendBridge.bridge_create()
    7.26 +    XendBridge.reconfigure()
    7.27  
    7.28  def main(port=None, interface=None):
    7.29      create(port, interface)
     8.1 --- a/tools/xenmgr/lib/server/blkif.py	Tue Jun 22 18:59:03 2004 +0000
     8.2 +++ b/tools/xenmgr/lib/server/blkif.py	Tue Jun 22 22:55:24 2004 +0000
     8.3 @@ -33,6 +33,14 @@ class BlkifControllerFactory(controller.
     8.4              blkif.send_be_create()
     8.5          return d
     8.6  
     8.7 +    def getDomainDevices(self, dom):
     8.8 +        blkif = self.getInstanceByDom(dom)
     8.9 +        return (blkif and blkif.getDevices()) or []
    8.10 +
    8.11 +    def getDomainDevice(self, dom, vdev):
    8.12 +        blkif = self.getInstanceByDom(dom)
    8.13 +        return (blkif and blkif.getDevice(vdev)) or None
    8.14 +
    8.15      def setControlDomain(self, dom):
    8.16          if self.dom == dom: return
    8.17          self.deregisterChannel()
    8.18 @@ -146,6 +154,12 @@ class BlkifController(controller.Control
    8.19          self.registerChannel()
    8.20          #print 'BlkifController<', 'dom=', self.dom, 'idx=', self.idx
    8.21  
    8.22 +    def getDevices(self):
    8.23 +        return self.devices.values()
    8.24 +
    8.25 +    def getDevice(self, vdev):
    8.26 +        return self.devices.get(vdev)
    8.27 +
    8.28      def attach_device(self, vdev, mode, segment):
    8.29          """Attach a device to the specified interface.
    8.30          """
     9.1 --- a/tools/xenmgr/lib/server/netif.py	Tue Jun 22 18:59:03 2004 +0000
     9.2 +++ b/tools/xenmgr/lib/server/netif.py	Tue Jun 22 22:55:24 2004 +0000
     9.3 @@ -31,6 +31,14 @@ class NetifControllerFactory(controller.
     9.4              netif = NetifController(self, dom)
     9.5              self.addInstance(netif)
     9.6          return netif
     9.7 +
     9.8 +    def getDomainDevices(self, dom):
     9.9 +        netif = self.getInstanceByDom(dom)
    9.10 +        return (netif and netif.getDevices()) or []
    9.11 +
    9.12 +    def getDomainDevice(self, dom, vif):
    9.13 +        netif = self.getInstanceByDom(dom)
    9.14 +        return (netif and netif.getDevice(vif)) or None
    9.15          
    9.16      def setControlDomain(self, dom):
    9.17          """Set the 'back-end' device driver domain.
    9.18 @@ -98,6 +106,11 @@ class NetDev:
    9.19          self.vif = vif
    9.20          self.mac = mac
    9.21          self.evtchn = None
    9.22 +
    9.23 +    def sxpr(self):
    9.24 +        vif = str(self.vif)
    9.25 +        mac = ':'.join(map(lambda x: "%x" % x, self.mac))
    9.26 +        return ['netif', ['vif', vif], ['mac', mac]]
    9.27      
    9.28  class NetifController(controller.Controller):
    9.29      """Network interface controller. Handles all network devices for a domain.
    9.30 @@ -135,6 +148,29 @@ class NetifController(controller.Control
    9.31                  random.randint(0x00, 0xff) ]
    9.32          return mac
    9.33  
    9.34 +    def lostChannel(self):
    9.35 +        print 'NetifController>lostChannel>', 'dom=', self.dom
    9.36 +        #for vif in self.devices:
    9.37 +        #    self.send_be_destroy(vif)
    9.38 +        controller.Controller.lostChannel(self)
    9.39 +
    9.40 +    def getDevices(self):
    9.41 +        return self.devices.values()
    9.42 +
    9.43 +    def getDevice(self, vif):
    9.44 +        return self.devices.get(vif)
    9.45 +
    9.46 +    def addDevice(self, vif, vmac):
    9.47 +        if vmac is None:
    9.48 +            mac = self.randomMAC()
    9.49 +        else:
    9.50 +            mac = [ int(x, 16) for x in vmac.split(':') ]
    9.51 +        if len(mac) != 6: raise ValueError("invalid mac")
    9.52 +        #print "attach_device>", "vif=", vif, "mac=", mac
    9.53 +        dev = NetDev(vif, mac)
    9.54 +        self.devices[vif] = dev
    9.55 +        return dev
    9.56 +
    9.57      def attach_device(self, vif, vmac):
    9.58          """Attach a network device.
    9.59          If vmac is None a random mac address is assigned.
    9.60 @@ -142,13 +178,7 @@ class NetifController(controller.Control
    9.61          @param vif interface index
    9.62          @param vmac mac address (string)
    9.63          """
    9.64 -        if vmac is None:
    9.65 -            mac = self.randomMAC()
    9.66 -        else:
    9.67 -            mac = [ int(x, 16) for x in vmac.split(':') ]
    9.68 -        if len(mac) != 6: raise ValueError("invalid mac")
    9.69 -        #print "attach_device>", "vif=", vif, "mac=", mac
    9.70 -        self.devices[vif] = NetDev(vif, mac)
    9.71 +        self.addDevice(vif, vmac)
    9.72          d = self.factory.addDeferred()
    9.73          self.send_be_create(vif)
    9.74          return d
    10.1 --- a/tools/xenmgr/netfix	Tue Jun 22 18:59:03 2004 +0000
    10.2 +++ b/tools/xenmgr/netfix	Tue Jun 22 22:55:24 2004 +0000
    10.3 @@ -3,102 +3,15 @@
    10.4  # Copyright (C) 2004 Mike Wray <mike.wray@hp.com>
    10.5  #============================================================================
    10.6  # Move the IP address from eth0 onto the Xen bridge (nbe-br).
    10.7 -# Works best if the bridge control utils (brctl) have been installed.
    10.8 +# Only works if the bridge control utils (brctl) have been installed.
    10.9  #============================================================================
   10.10 -import os
   10.11 -import os.path
   10.12 -import re
   10.13 -import sys
   10.14  
   10.15  from getopt import getopt
   10.16 -
   10.17 -os.defpath = os.defpath+':/sbin:/usr/sbin:/usr/local/sbin'
   10.18 -CMD_IFCONFIG = 'ifconfig'
   10.19 -CMD_ROUTE    = 'route'
   10.20 -CMD_BRCTL    = 'brctl'
   10.21 -
   10.22 -def routes():
   10.23 -    """Return a list of the routes.
   10.24 -    """
   10.25 -    fin = os.popen(CMD_ROUTE + ' -n', 'r')
   10.26 -    routes = []
   10.27 -    for x in fin:
   10.28 -        if x.startswith('Kernel'): continue
   10.29 -        if x.startswith('Destination'): continue
   10.30 -        x = x.strip()
   10.31 -        y = x.split()
   10.32 -        z = { 'destination': y[0],
   10.33 -              'gateway'    : y[1],
   10.34 -              'mask'       : y[2],
   10.35 -              'flags'      : y[3],
   10.36 -              'metric'     : y[4],
   10.37 -              'ref'        : y[5],
   10.38 -              'use'        : y[6],
   10.39 -              'interface'  : y[7] }
   10.40 -        routes.append(z)
   10.41 -    return routes
   10.42 -
   10.43 -def cmd(p, s):
   10.44 -    """Print and execute command 'p' with args 's'.
   10.45 -    """
   10.46 -    global opts
   10.47 -    c = p + ' ' + s
   10.48 -    if opts.verbose: print c
   10.49 -    if not opts.dryrun:
   10.50 -        os.system(c)
   10.51 +from xenmgr.XendBridge import *
   10.52  
   10.53 -def ifconfig(interface):
   10.54 -    """Return the ip config for an interface,
   10.55 -    """
   10.56 -    fin = os.popen(CMD_IFCONFIG + ' %s' % interface, 'r')
   10.57 -    inetre = re.compile('\s*inet\s*addr:(?P<address>\S*)\s*Bcast:(?P<broadcast>\S*)\s*Mask:(?P<mask>\S*)')
   10.58 -    info = None
   10.59 -    for x in fin:
   10.60 -        m = inetre.match(x)
   10.61 -        if not m: continue
   10.62 -        info = m.groupdict()
   10.63 -        info['interface'] = interface
   10.64 -        break
   10.65 -    return info
   10.66 -
   10.67 -def reconfigure(interface, bridge):
   10.68 -    """Reconfigure an interface to be attached to a bridge, and give the bridge
   10.69 -    the IP address etc. from interface. Move the default route to the interface
   10.70 -    to the bridge.
   10.71 -    """
   10.72 -    intf_info = ifconfig(interface)
   10.73 -    if not intf_info:
   10.74 -        print 'Interface not found:', interface
   10.75 -        return
   10.76 -    #bridge_info = ifconfig(bridge)
   10.77 -    #if not bridge_info:
   10.78 -    #    print 'Bridge not found:', bridge
   10.79 -    #    return
   10.80 -    route_info = routes()
   10.81 -    intf_info['bridge'] = bridge
   10.82 -    intf_info['gateway'] = None
   10.83 -    for r in route_info:
   10.84 -        if (r['destination'] == '0.0.0.0' and
   10.85 -            'G' in r['flags'] and
   10.86 -            r['interface'] == interface):
   10.87 -            intf_info['gateway'] = r['gateway']
   10.88 -    if not intf_info['gateway']:
   10.89 -        print 'Gateway not found: ', interface
   10.90 -        return
   10.91 -    cmd(CMD_IFCONFIG, '%(bridge)s %(address)s netmask %(mask)s broadcast %(broadcast)s up' % intf_info)
   10.92 -    cmd(CMD_ROUTE, 'add default gateway %(gateway)s dev %(bridge)s' % intf_info)
   10.93 -    cmd(CMD_BRCTL, 'addif %(bridge)s %(interface)s' % intf_info)
   10.94 -    cmd(CMD_IFCONFIG, '%(interface)s 0.0.0.0' % intf_info)
   10.95 -
   10.96 -defaults = {
   10.97 -    'interface': 'eth0',
   10.98 -    'bridge'   : 'nbe-br',
   10.99 -    'verbose'  : 1,
  10.100 -    'dryrun'   : 0,
  10.101 -    }
  10.102 -
  10.103 -short_options = 'hvqni:b:'
  10.104 -long_options  = ['help', 'verbose', 'quiet', 'interface=', 'bridge=']
  10.105 +short_options = 'hvqni:b:c'
  10.106 +long_options  = ['help', 'verbose', 'quiet',
  10.107 +                 'interface=', 'bridge=', 'create']
  10.108  
  10.109  def usage():
  10.110      print """Usage:
  10.111 @@ -107,12 +20,13 @@ def usage():
  10.112      Reconfigure routing so that <bridge> has the IP address from
  10.113      <interface>. This lets IP carry on working when <interface>
  10.114      is attached to <bridge> for virtual networking.
  10.115 -    If brctl is available, <interface> is added to <bridge>,
  10.116 +    Uses brctl to add <interface> to <bridge>,
  10.117      so this can be run before any domains have been created.
  10.118      """ % sys.argv[0]
  10.119      print """
  10.120      -i, --interface <interface>    interface, default %(interface)s.
  10.121      -b, --bridge <bridge>          bridge, default %(bridge)s.
  10.122 +    -c, --create                   create the bridge.
  10.123      -v, --verbose                  Print commands.
  10.124      -q, --quiet                    Don't print commands.
  10.125      -n, --dry-run                  Don't execute commands.
  10.126 @@ -120,32 +34,28 @@ def usage():
  10.127      """ % defaults
  10.128      sys.exit(1)
  10.129  
  10.130 -class Opts:
  10.131 -
  10.132 -    def __init__(self, defaults):
  10.133 -        for (k, v) in defaults.items():
  10.134 -            setattr(self, k, v)
  10.135 -        pass
  10.136  
  10.137  def main():
  10.138 -    global opts
  10.139 -    opts = Opts(defaults)
  10.140 +    lopts = set_opts(Opts(defaults))
  10.141 +    lopts.dryrun = 0
  10.142      (options, args) = getopt(sys.argv[1:], short_options, long_options)
  10.143      if args: usage()
  10.144      for k, v in options:
  10.145          if k in ['-h', '--help']:
  10.146              usage()
  10.147 +        elif k in ['-c', '--create']:
  10.148 +            lopts.create = 1
  10.149          elif k in ['-i', '--interface']:
  10.150 -            opts.interface = v
  10.151 +            lopts.interface = v
  10.152          elif k in ['-b', '--bridge']:
  10.153 -            opts.bridge = v
  10.154 +            lopts.bridge = v
  10.155          elif k in ['-q', '--quiet']:
  10.156 -            opts.verbose = 0
  10.157 +            lopts.verbose = 0
  10.158          elif k in ['-v', '--verbose']:
  10.159 -            opts.verbose = 1
  10.160 +            lopts.verbose = 1
  10.161          elif k in ['-n', '--dry-run']:
  10.162 -            opts.dryrun = 1
  10.163 -    reconfigure(opts.interface, opts.bridge)
  10.164 +            lopts.dryrun = 1
  10.165 +    reconfigure(lopts.interface, lopts.bridge)
  10.166  
  10.167  if __name__ == '__main__':
  10.168      main()