ia64/xen-unstable

changeset 1612:353e11fcd243

bitkeeper revision 1.1023.1.6 (40e15b7edePZdBO_2yFHsmAtSxfw9g)

Change to using the vifctl script to init networking and
bring vifs up and down instead of doing it all ourselves.
author mjw@wray-m-3.hpl.hp.com
date Tue Jun 29 12:07:26 2004 +0000 (2004-06-29)
parents be911ab15381
children d881debab3eb
files .rootkeys tools/xen/Makefile tools/xen/lib/util/Brctl.py tools/xen/lib/xend/Vifctl.py tools/xen/lib/xend/XendBridge.py tools/xen/lib/xend/XendDomainInfo.py tools/xen/lib/xend/server/SrvServer.py tools/xen/lib/xend/server/netif.py tools/xen/vifctl
line diff
     1.1 --- a/.rootkeys	Mon Jun 28 16:45:49 2004 +0000
     1.2 +++ b/.rootkeys	Tue Jun 29 12:07:26 2004 +0000
     1.3 @@ -239,6 +239,7 @@ 3fbd0a40yT6G3M9hMpaz5xTUdl0E4g tools/xc/
     1.4  40c9c468icGyC5RAF1bRKsCXPDCvsA tools/xen/Makefile
     1.5  40dc4076hGpwa8-sWRN0jtXZeQJuKg tools/xen/lib/__init__.py
     1.6  40dfd40aMOhnw_cQLve9462UR5yYxQ tools/xen/lib/ext/__init__.py
     1.7 +40d8915cyoVA0hJxiBFNymL7YvDaRg tools/xen/lib/util/Brctl.py
     1.8  40dfd40aGqGkiopOOgJxSF4iCbHM0Q tools/xen/lib/util/__init__.py
     1.9  4055ee4dwy4l0MghZosxoiu6zmhc9Q tools/xen/lib/util/console_client.py
    1.10  40c9c468IienauFHQ_xJIcqnPJ8giQ tools/xen/lib/util/ip.py
    1.11 @@ -247,7 +248,7 @@ 40c9c468SNuObE_YWARyS0hzTPSzKg tools/xen
    1.12  40c9c468Um_qc66OQeLEceIz1pgD5g tools/xen/lib/xend/EventServer.py
    1.13  40c9c468U8EVl0d3G--8YXVg6VJD3g tools/xen/lib/xend/EventTypes.py
    1.14  40c9c468QJTEuk9g4qHxGpmIi70PEQ tools/xen/lib/xend/PrettyPrint.py
    1.15 -40d8915cyoVA0hJxiBFNymL7YvDaRg tools/xen/lib/xend/XendBridge.py
    1.16 +40e15b7eeQxWE_hUPB2YTgM9fsZ1PQ tools/xen/lib/xend/Vifctl.py
    1.17  40c9c4688m3eqnC8fhLu1APm36VOVA tools/xen/lib/xend/XendClient.py
    1.18  40c9c468t6iIKTjwuYoe-UMCikDcOQ tools/xen/lib/xend/XendConsole.py
    1.19  40c9c468WnXs6eOUSff23IIGI4kMfQ tools/xen/lib/xend/XendDB.py
    1.20 @@ -291,6 +292,7 @@ 40cf2937PSslwBliN1g7ofDy2H_RhA tools/xen
    1.21  40cf2937Z8WCNOnO2FcWdubvEAF9QQ tools/xen/lib/xm/shutdown.py
    1.22  40c9c469kT0H9COWzA4XzPBjWK0WsA tools/xen/netfix
    1.23  40c9c469n2RRwCmjWdjdyyVRWKmgWg tools/xen/setup.py
    1.24 +40e15b7edWEtBf_oe3eBwGKuh1dyzQ tools/xen/vifctl
    1.25  40c9c4697z76HDfkCLdMhmaEwzFoNQ tools/xen/xend
    1.26  40cf2937dqM1jWW87O5OoOYND8leuA tools/xen/xm
    1.27  40431ac64Hj4ixUnKmlugZKhXPFE_Q tools/xend-old/Makefile
     2.1 --- a/tools/xen/Makefile	Mon Jun 28 16:45:49 2004 +0000
     2.2 +++ b/tools/xen/Makefile	Tue Jun 29 12:07:26 2004 +0000
     2.3 @@ -14,6 +14,8 @@ install: all
     2.4  	install -m0755 xend $(prefix)/usr/sbin
     2.5  	install -m0755 netfix $(prefix)/usr/sbin
     2.6  	install -m0755 xm $(prefix)/usr/sbin
     2.7 +	mkdir -p $(prefix)/etc/xen/xend
     2.8 +	install -m0755 vifctl $(prefix)/etc/xen/xend
     2.9  
    2.10  clean:
    2.11  	rm -rf build *.pyc *.pyo *.o *.a *~
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/tools/xen/lib/util/Brctl.py	Tue Jun 29 12:07:26 2004 +0000
     3.3 @@ -0,0 +1,161 @@
     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 +os.defpath = os.defpath + ':/sbin:/usr/sbin:/usr/local/sbin'
    3.12 +CMD_IFCONFIG = 'ifconfig'
    3.13 +CMD_ROUTE    = 'route'
    3.14 +CMD_BRCTL    = 'brctl'
    3.15 +CMD_IPTABLES = "iptables"
    3.16 +
    3.17 +opts = None
    3.18 +
    3.19 +class Opts:
    3.20 +
    3.21 +    def __init__(self, defaults):
    3.22 +        for (k, v) in defaults.items():
    3.23 +            setattr(self, k, v)
    3.24 +        pass
    3.25 +
    3.26 +def cmd(p, s):
    3.27 +    """Print and execute command 'p' with args 's'.
    3.28 +    """
    3.29 +    global opts
    3.30 +    c = p + ' ' + s
    3.31 +    if opts.verbose: print c
    3.32 +    if not opts.dryrun:
    3.33 +        os.system(c)
    3.34 +
    3.35 +def vif_bridge_add(params):
    3.36 +    """Add the network interface for vif on dom to a bridge.
    3.37 +    """
    3.38 +    cmd(CMD_BRCTL, 'addif %(bridge)s %(vif)s' % params)
    3.39 +    return bridge
    3.40 +
    3.41 +def vif_bridge_rem(params):
    3.42 +    """Remove the network interface for vif on dom from a bridge.
    3.43 +    """
    3.44 +    cmd(CMD_BRCTL, 'delif %(bridge)s %(vif)s' % params)
    3.45 +
    3.46 +def vif_restrict_addr(vif, addr, delete=0):
    3.47 +    d = { 'vif': vif, 'addr': addr}
    3.48 +    if delete:
    3.49 +        d['flag'] = '-D'
    3.50 +    else:
    3.51 +        d['flag'] = '-A'
    3.52 +    cmd(CMD_IPTABLES, '-P FORWARD DROP')
    3.53 +    cmd(CMD_IPTABLES, '%(flag)s FORWARD -m physdev --physdev-in %(vif)s -s %(addr)s -j ACCEPT' % d)
    3.54 +    cmd(CMD_IPTABLES, '%(flag)s FORWARD -m physdev --physdev-out %(vif)s -d %(addr)s -j ACCEPT' % d)
    3.55 +
    3.56 +def bridge_create(bridge, **kwd):
    3.57 +    """Create a bridge.
    3.58 +    Defaults hello time to 0, forward delay to 0 and stp off.
    3.59 +    """
    3.60 +    cmd(CMD_BRCTL, 'addbr %s' % bridge)
    3.61 +    if kwd.get('hello', None) is None:
    3.62 +        kwd['hello'] = 0
    3.63 +    if kwd.get('fd', None) is None:
    3.64 +        kwd['fd'] = 0
    3.65 +    if kwd.get('stp', None) is None:
    3.66 +        kwd['stp'] = 'off'
    3.67 +    bridge_set(bridge, **kwd)
    3.68 +
    3.69 +def bridge_set(bridge, hello=None, fd=None, stp=None):
    3.70 +    """Set bridge parameters.
    3.71 +    """
    3.72 +    if hello is not None:
    3.73 +        cmd(CMD_BRCTL, 'sethello %s %d' % (bridge, hello))
    3.74 +    if fd is not None:
    3.75 +        cmd(CMD_BRCTL, 'setfd %s %d' % (bridge, fd))
    3.76 +    if stp is not None:
    3.77 +        cmd(CMD_BRCTL, 'stp %s %s' % (bridge, stp))
    3.78 +
    3.79 +def bridge_del(bridge):
    3.80 +    """Delete a bridge.
    3.81 +    """
    3.82 +    cmd(CMD_BRCTL, 'delbr %s' % bridge)
    3.83 +
    3.84 +def routes():
    3.85 +    """Return a list of the routes.
    3.86 +    """
    3.87 +    fin = os.popen(CMD_ROUTE + ' -n', 'r')
    3.88 +    routes = []
    3.89 +    for x in fin:
    3.90 +        if x.startswith('Kernel'): continue
    3.91 +        if x.startswith('Destination'): continue
    3.92 +        x = x.strip()
    3.93 +        y = x.split()
    3.94 +        z = { 'destination': y[0],
    3.95 +              'gateway'    : y[1],
    3.96 +              'mask'       : y[2],
    3.97 +              'flags'      : y[3],
    3.98 +              'metric'     : y[4],
    3.99 +              'ref'        : y[5],
   3.100 +              'use'        : y[6],
   3.101 +              'interface'  : y[7] }
   3.102 +        routes.append(z)
   3.103 +    return routes
   3.104 +
   3.105 +def ifconfig(interface):
   3.106 +    """Return the ip config for an interface,
   3.107 +    """
   3.108 +    fin = os.popen(CMD_IFCONFIG + ' %s' % interface, 'r')
   3.109 +    inetre = re.compile('\s*inet\s*addr:(?P<address>\S*)\s*Bcast:(?P<broadcast>\S*)\s*Mask:(?P<mask>\S*)')
   3.110 +    info = None
   3.111 +    for x in fin:
   3.112 +        m = inetre.match(x)
   3.113 +        if not m: continue
   3.114 +        info = m.groupdict()
   3.115 +        info['interface'] = interface
   3.116 +        break
   3.117 +    return info
   3.118 +
   3.119 +def reconfigure(interface, bridge):
   3.120 +    """Reconfigure an interface to be attached to a bridge, and give the bridge
   3.121 +    the IP address etc. from interface. Move the default route to the interface
   3.122 +    to the bridge.
   3.123 +
   3.124 +    """
   3.125 +    global opts
   3.126 +    intf_info = ifconfig(interface)
   3.127 +    if not intf_info:
   3.128 +        print 'Interface not found:', interface
   3.129 +        return
   3.130 +    #bridge_info = ifconfig(bridge)
   3.131 +    #if not bridge_info:
   3.132 +    #    print 'Bridge not found:', bridge
   3.133 +    #    return
   3.134 +    route_info = routes()
   3.135 +    intf_info['bridge'] = bridge
   3.136 +    intf_info['gateway'] = None
   3.137 +    for r in route_info:
   3.138 +        if (r['destination'] == '0.0.0.0' and
   3.139 +            'G' in r['flags'] and
   3.140 +            r['interface'] == interface):
   3.141 +            intf_info['gateway'] = r['gateway']
   3.142 +    if not intf_info['gateway']:
   3.143 +        print 'Gateway not found: ', interface
   3.144 +        return
   3.145 +    cmd(CMD_IFCONFIG,
   3.146 +        '%(bridge)s %(address)s netmask %(mask)s broadcast %(broadcast)s up'
   3.147 +        % intf_info)
   3.148 +    cmd(CMD_ROUTE,
   3.149 +        'add default gateway %(gateway)s dev %(bridge)s'
   3.150 +        % intf_info)
   3.151 +    cmd(CMD_BRCTL, 'addif %(bridge)s %(interface)s' % intf_info)
   3.152 +    cmd(CMD_IFCONFIG, '%(interface)s 0.0.0.0' % intf_info)
   3.153 +
   3.154 +defaults = {
   3.155 +    'verbose'  : 1,
   3.156 +    'dryrun'   : 0,
   3.157 +    }
   3.158 +
   3.159 +opts = Opts(defaults)
   3.160 +
   3.161 +def set_opts(val):
   3.162 +    global opts
   3.163 +    opts = val
   3.164 +    return opts
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/tools/xen/lib/xend/Vifctl.py	Tue Jun 29 12:07:26 2004 +0000
     4.3 @@ -0,0 +1,28 @@
     4.4 +import os
     4.5 +import os.path
     4.6 +import sys
     4.7 +
     4.8 +VIFCTL = '/etc/xen/xend/vifctl'
     4.9 +
    4.10 +def init():
    4.11 +    os.system(VIFCTL + ' init ')
    4.12 +
    4.13 +def up(vif, mac=None, bridge=None, ipaddr=[]):
    4.14 +    args = ['vif=%s' % vif]
    4.15 +    if mac:
    4.16 +        args.append('mac=%s' % mac)
    4.17 +    if bridge:
    4.18 +        args.append('bridge=%s' % bridge)
    4.19 +    if ipaddr:
    4.20 +        args.append('ipaddr=%s' % ','.join(ipaddr))
    4.21 +    os.system(VIFCTL + ' up ' + ' '.join(args))
    4.22 +
    4.23 +def down(vif, mac=None, bridge=None, ipaddr=[]):
    4.24 +    args = ['vif=%s' % vif]
    4.25 +    if mac:
    4.26 +        args.append('mac=%s' % mac)
    4.27 +    if bridge:
    4.28 +        args.append('bridge=%s' % bridge)
    4.29 +    if ipaddr:
    4.30 +        args.append('ipaddr=%s' % ','.join(ipaddr))
    4.31 +    os.system(VIFCTL + ' down ' + ' '.join(args))
     5.1 --- a/tools/xen/lib/xend/XendBridge.py	Mon Jun 28 16:45:49 2004 +0000
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,193 +0,0 @@
     5.4 -"""Bridge control utilities.
     5.5 -"""
     5.6 -import os
     5.7 -import os.path
     5.8 -import re
     5.9 -import sys
    5.10 -
    5.11 -from xen.xend import XendRoot
    5.12 -xroot = XendRoot.instance()
    5.13 -
    5.14 -os.defpath = os.defpath + ':/sbin:/usr/sbin:/usr/local/sbin'
    5.15 -CMD_IFCONFIG = 'ifconfig'
    5.16 -CMD_ROUTE    = 'route'
    5.17 -CMD_BRCTL    = 'brctl'
    5.18 -CMD_IPTABLES = "iptables"
    5.19 -
    5.20 -DEFAULT_BRIDGE = 'nbe-br'
    5.21 -DEFAULT_INTERFACE = 'eth0'
    5.22 -
    5.23 -opts = None
    5.24 -
    5.25 -class Opts:
    5.26 -
    5.27 -    def __init__(self, defaults):
    5.28 -        for (k, v) in defaults.items():
    5.29 -            setattr(self, k, v)
    5.30 -        pass
    5.31 -
    5.32 -def cmd(p, s):
    5.33 -    """Print and execute command 'p' with args 's'.
    5.34 -    """
    5.35 -    global opts
    5.36 -    c = p + ' ' + s
    5.37 -    if opts.verbose: print c
    5.38 -    if not opts.dryrun:
    5.39 -        os.system(c)
    5.40 -
    5.41 -def default_bridge():
    5.42 -    return xroot.get_config_value('bridge', DEFAULT_BRIDGE)
    5.43 -
    5.44 -def default_interface():
    5.45 -    return xroot.get_config_value('interface', DEFAULT_INTERFACE)
    5.46 -
    5.47 -def vif_dev(dom, vif):
    5.48 -    """Return the name of the network interface for vif on domain dom.
    5.49 -    """
    5.50 -    return "vif%d.%d" % (dom, vif)
    5.51 -
    5.52 -def vif_bridge_add(dom, vif, bridge=None):
    5.53 -    """Add the network interface for vif on dom to a bridge.
    5.54 -    """
    5.55 -    if not bridge: bridge = default_bridge()
    5.56 -    d = { 'bridge': bridge, 'vif': vif_dev(dom, vif) }
    5.57 -    cmd(CMD_BRCTL, 'addif %(bridge)s %(vif)s' % d)
    5.58 -    return bridge
    5.59 -
    5.60 -def vif_bridge_rem(dom, vif, bridge=None):
    5.61 -    """Remove the network interface for vif on dom from a bridge.
    5.62 -    """
    5.63 -    if not bridge: bridge = default_bridge()
    5.64 -    print 'vif_bridge_rem>', dom, vif, bridge
    5.65 -    d = { 'bridge': bridge, 'vif': vif_dev(dom, vif) }
    5.66 -    cmd(CMD_BRCTL, 'delif %(bridge)s %(vif)s' % d)
    5.67 -
    5.68 -def vif_restrict_addr(dom, vif, addr, delete=0):
    5.69 -    d = { 'vif': vif_dev(dom, vif), 'addr': addr}
    5.70 -    if delete:
    5.71 -        d['flag'] = '-D'
    5.72 -    else:
    5.73 -        d['flag'] = '-A'
    5.74 -    cmd(CMD_IPTABLES, '-P FORWARD DROP')
    5.75 -    cmd(CMD_IPTABLES, '%(flag)s FORWARD -m physdev --physdev-in %(vif)s -s %(addr)s -j ACCEPT' % d)
    5.76 -    cmd(CMD_IPTABLES, '%(flag)s FORWARD -m physdev --physdev-out %(vif)s -d %(addr)s -j ACCEPT' % d)
    5.77 -
    5.78 -def bridge_create(bridge=None, **kwd):
    5.79 -    """Create a bridge.
    5.80 -    Defaults hello time to 0, forward delay to 0 and stp off.
    5.81 -    """
    5.82 -    if not bridge: bridge = default_bridge()
    5.83 -    cmd(CMD_BRCTL, 'addbr %s' % bridge)
    5.84 -    if kwd.get('hello', None) is None:
    5.85 -        kwd['hello'] = 0
    5.86 -    if kwd.get('fd', None) is None:
    5.87 -        kwd['fd'] = 0
    5.88 -    if kwd.get('stp', None) is None:
    5.89 -        kwd['stp'] = 'off'
    5.90 -    bridge_set(bridge, **kwd)
    5.91 -
    5.92 -def bridge_set(bridge, hello=None, fd=None, stp=None):
    5.93 -    """Set bridge parameters.
    5.94 -    """
    5.95 -    if hello is not None:
    5.96 -        cmd(CMD_BRCTL, 'sethello %s %d' % (bridge, hello))
    5.97 -    if fd is not None:
    5.98 -        cmd(CMD_BRCTL, 'setfd %s %d' % (bridge, fd))
    5.99 -    if stp is not None:
   5.100 -        cmd(CMD_BRCTL, 'stp %s %s' % (bridge, stp))
   5.101 -
   5.102 -def bridge_del(bridge=None):
   5.103 -    """Delete a bridge.
   5.104 -    """
   5.105 -    if not bridge: bridge = default_bridge()
   5.106 -    cmd(CMD_BRCTL, 'delbr %s' % bridge)
   5.107 -
   5.108 -def routes():
   5.109 -    """Return a list of the routes.
   5.110 -    """
   5.111 -    fin = os.popen(CMD_ROUTE + ' -n', 'r')
   5.112 -    routes = []
   5.113 -    for x in fin:
   5.114 -        if x.startswith('Kernel'): continue
   5.115 -        if x.startswith('Destination'): continue
   5.116 -        x = x.strip()
   5.117 -        y = x.split()
   5.118 -        z = { 'destination': y[0],
   5.119 -              'gateway'    : y[1],
   5.120 -              'mask'       : y[2],
   5.121 -              'flags'      : y[3],
   5.122 -              'metric'     : y[4],
   5.123 -              'ref'        : y[5],
   5.124 -              'use'        : y[6],
   5.125 -              'interface'  : y[7] }
   5.126 -        routes.append(z)
   5.127 -    return routes
   5.128 -
   5.129 -def ifconfig(interface):
   5.130 -    """Return the ip config for an interface,
   5.131 -    """
   5.132 -    fin = os.popen(CMD_IFCONFIG + ' %s' % interface, 'r')
   5.133 -    inetre = re.compile('\s*inet\s*addr:(?P<address>\S*)\s*Bcast:(?P<broadcast>\S*)\s*Mask:(?P<mask>\S*)')
   5.134 -    info = None
   5.135 -    for x in fin:
   5.136 -        m = inetre.match(x)
   5.137 -        if not m: continue
   5.138 -        info = m.groupdict()
   5.139 -        info['interface'] = interface
   5.140 -        break
   5.141 -    return info
   5.142 -
   5.143 -def reconfigure(interface=None, bridge=None):
   5.144 -    """Reconfigure an interface to be attached to a bridge, and give the bridge
   5.145 -    the IP address etc. from interface. Move the default route to the interface
   5.146 -    to the bridge.
   5.147 -
   5.148 -    If opts.create is true, creates the bridge.
   5.149 -    """
   5.150 -    global opts
   5.151 -    if not interface: interface = default_interface()
   5.152 -    if not bridge: bridge = default_bridge()
   5.153 -    intf_info = ifconfig(interface)
   5.154 -    if not intf_info:
   5.155 -        print 'Interface not found:', interface
   5.156 -        return
   5.157 -    if opts.create:
   5.158 -        bridge_create(bridge)
   5.159 -    #bridge_info = ifconfig(bridge)
   5.160 -    #if not bridge_info:
   5.161 -    #    print 'Bridge not found:', bridge
   5.162 -    #    return
   5.163 -    route_info = routes()
   5.164 -    intf_info['bridge'] = bridge
   5.165 -    intf_info['gateway'] = None
   5.166 -    for r in route_info:
   5.167 -        if (r['destination'] == '0.0.0.0' and
   5.168 -            'G' in r['flags'] and
   5.169 -            r['interface'] == interface):
   5.170 -            intf_info['gateway'] = r['gateway']
   5.171 -    if not intf_info['gateway']:
   5.172 -        print 'Gateway not found: ', interface
   5.173 -        return
   5.174 -    cmd(CMD_IFCONFIG,
   5.175 -        '%(bridge)s %(address)s netmask %(mask)s broadcast %(broadcast)s up'
   5.176 -        % intf_info)
   5.177 -    cmd(CMD_ROUTE,
   5.178 -        'add default gateway %(gateway)s dev %(bridge)s'
   5.179 -        % intf_info)
   5.180 -    cmd(CMD_BRCTL, 'addif %(bridge)s %(interface)s' % intf_info)
   5.181 -    cmd(CMD_IFCONFIG, '%(interface)s 0.0.0.0' % intf_info)
   5.182 -
   5.183 -defaults = {
   5.184 -    'interface': default_interface(),
   5.185 -    'bridge'   : default_bridge(),
   5.186 -    'verbose'  : 1,
   5.187 -    'dryrun'   : 0,
   5.188 -    'create'   : 0,
   5.189 -    }
   5.190 -
   5.191 -opts = Opts(defaults)
   5.192 -
   5.193 -def set_opts(val):
   5.194 -    global opts
   5.195 -    opts = val
   5.196 -    return opts
     6.1 --- a/tools/xen/lib/xend/XendDomainInfo.py	Mon Jun 28 16:45:49 2004 +0000
     6.2 +++ b/tools/xen/lib/xend/XendDomainInfo.py	Tue Jun 29 12:07:26 2004 +0000
     6.3 @@ -24,8 +24,6 @@ import sxp
     6.4  import XendConsole
     6.5  xendConsole = XendConsole.instance()
     6.6  
     6.7 -import XendBridge
     6.8 -
     6.9  import server.SrvDaemon
    6.10  xend = server.SrvDaemon.instance()
    6.11  
    6.12 @@ -765,7 +763,7 @@ def vm_dev_vif(vm, val, index):
    6.13          if devid:
    6.14              dev.setprop('id', devid)
    6.15          bridge = sxp.child_value(val, "bridge")
    6.16 -        dev.bridge_add(bridge)
    6.17 +        dev.up(bridge)
    6.18          vm.add_device('vif', dev)
    6.19          print 'vm_dev_vif> created', dev
    6.20          return id
     7.1 --- a/tools/xen/lib/xend/server/SrvServer.py	Mon Jun 28 16:45:49 2004 +0000
     7.2 +++ b/tools/xen/lib/xend/server/SrvServer.py	Tue Jun 29 12:07:26 2004 +0000
     7.3 @@ -32,7 +32,7 @@ from twisted.internet import reactor
     7.4  from xen.xend import XendRoot
     7.5  xroot = XendRoot.instance()
     7.6  
     7.7 -from xen.xend import XendBridge
     7.8 +from xen.xend import Vifctl
     7.9  
    7.10  from SrvRoot import SrvRoot
    7.11  
    7.12 @@ -48,8 +48,7 @@ def create(port=None, interface=None, br
    7.13      reactor.listenTCP(port, site, interface=interface)
    7.14  
    7.15  def init_bridge():
    7.16 -    XendBridge.bridge_create()
    7.17 -    XendBridge.reconfigure()
    7.18 +    Vifctl.init()
    7.19  
    7.20  def main(port=None, interface=None):
    7.21      create(port, interface)
     8.1 --- a/tools/xen/lib/xend/server/netif.py	Mon Jun 28 16:45:49 2004 +0000
     8.2 +++ b/tools/xen/lib/xend/server/netif.py	Tue Jun 29 12:07:26 2004 +0000
     8.3 @@ -4,7 +4,7 @@ from twisted.internet import defer
     8.4  
     8.5  from xen.xend import sxp
     8.6  from xen.xend import PrettyPrint
     8.7 -from xen.xend import XendBridge
     8.8 +from xen.xend import Vifctl
     8.9  
    8.10  import channel
    8.11  import controller
    8.12 @@ -115,10 +115,11 @@ class NetDev(controller.Dev):
    8.13          self.mac = mac
    8.14          self.evtchn = None
    8.15          self.bridge = None
    8.16 +        self.ipaddr = []
    8.17  
    8.18      def sxpr(self):
    8.19          vif = str(self.vif)
    8.20 -        mac = ':'.join(map(lambda x: "%x" % x, self.mac))
    8.21 +        mac = self.get_mac()
    8.22          val = ['netdev', ['vif', vif], ['mac', mac]]
    8.23          if self.bridge:
    8.24              val.append(['bridge', self.bridge])
    8.25 @@ -128,20 +129,31 @@ class NetDev(controller.Dev):
    8.26                          self.evtchn['port2']])
    8.27          return val
    8.28  
    8.29 -    def bridge_add(self, bridge):
    8.30 -        self.bridge = XendBridge.vif_bridge_add(self.controller.dom, self.vif, bridge)
    8.31 +    def get_vifname(self):
    8.32 +        return "vif%d.%d" % (self.controller.dom, self.vif)
    8.33 +
    8.34 +    def get_mac(self):
    8.35 +        return ':'.join(map(lambda x: "%x" % x, self.mac))
    8.36  
    8.37 -    def bridge_rem(self):
    8.38 -        if not self.bridge: return
    8.39 -        XendBridge.vif_bridge_rem(self.controller.dom, self.vif, self.bridge)
    8.40 -        self.bridge = None
    8.41 +    def vifctl_params(self):
    8.42 +        return { 'mac'   : self.get_mac(),
    8.43 +                 'bridge': self.bridge,
    8.44 +                 'ipaddr': self.ipaddr }
    8.45 +
    8.46 +    def up(self, bridge=None, ipaddr=[]):
    8.47 +        self.bridge = bridge
    8.48 +        self.ipaddr = ipaddr
    8.49 +        Vifctl.up(self.get_vifname(), **self.vifctl_params())
    8.50 +
    8.51 +    def down(self):
    8.52 +        Vifctl.down(self.get_vifname(), **self.vifctl_params())
    8.53  
    8.54      def destroy(self):
    8.55          def cb_destroy(val):
    8.56              self.controller.send_be_destroy(self.vif)
    8.57          print 'NetDev>destroy>', 'vif=', self.vif
    8.58          PrettyPrint.prettyprint(self.sxpr())
    8.59 -        self.bridge_rem()
    8.60 +        self.down()
    8.61          d = self.controller.factory.addDeferred()
    8.62          d.addCallback(cb_destroy)
    8.63          self.controller.send_be_disconnect(self.vif)
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/tools/xen/vifctl	Tue Jun 29 12:07:26 2004 +0000
     9.3 @@ -0,0 +1,148 @@
     9.4 +#!/usr/bin/python
     9.5 +#  -*- mode: python; -*-
     9.6 +#============================================================================
     9.7 +# Xen vif control script.
     9.8 +#
     9.9 +# vifctl init [bridge=<bridge>] [interface=<interface>]
    9.10 +#
    9.11 +#    Called when xend starts up. Default behaviour is to create <bridge>
    9.12 +#    and add <interface> to it, moving its IP address to <bridge> and adjusting routes.
    9.13 +#
    9.14 +# vifctl (up|down) vif=<vif> mac=<mac> [bridge=<bridge>] (ipaddr=<ipaddr>)*
    9.15 +#
    9.16 +#    Called when a vif is brought up or down. Default behaviour is to add
    9.17 +#    the vif to <bridge> on up and remove it from the bridge on down.
    9.18 +#    If ipaddr is specified, iptables rules for the ip addresses are
    9.19 +#    added on up and removed on down. The bridge a vif is added to can
    9.20 +#    be set in the vm config.
    9.21 +#
    9.22 +# The default bridge is nbe-br.
    9.23 +# The default interface is eth0.
    9.24 +#
    9.25 +#============================================================================
    9.26 +
    9.27 +import sys
    9.28 +import types
    9.29 +
    9.30 +from xen.util import Brctl
    9.31 +
    9.32 +from xen.xend import XendRoot
    9.33 +xroot = XendRoot.instance()
    9.34 +
    9.35 +class VifControl:
    9.36 +
    9.37 +    prefix = 'vifctl_'
    9.38 +
    9.39 +    DEFAULT_BRIDGE = 'nbe-br'
    9.40 +    DEFAULT_INTERFACE = 'eth0'
    9.41 +
    9.42 +    def __init__(self):
    9.43 +        self.name = 'vifctl'
    9.44 +    
    9.45 +    def main(self, args):
    9.46 +        if len(args) < 2:
    9.47 +            usage(args)
    9.48 +        self.name = args[0]
    9.49 +        cmd = self.prefix + args[1]
    9.50 +        meth = getattr(self, cmd, self.unknown)
    9.51 +        meth(args[1:])
    9.52 +
    9.53 +    def usage(self, args, out=sys.stderr):
    9.54 +        print >>out, 'Missing command, try \n%s help' % self.name
    9.55 +
    9.56 +    def unknown(self, args, out=sys.stderr):
    9.57 +        print >>out, 'Unknown command:', args[1]
    9.58 +        self.help(out=out)
    9.59 +        sys.exit(1)
    9.60 +
    9.61 +    def help(self, out=sys.stdout):
    9.62 +        print >>out, 'Commands are:',
    9.63 +        for x in vars(self):
    9.64 +            if x.startswith(prefix):
    9.65 +                cmd = x[len(prefix):]
    9.66 +                print  >>out, cmd,
    9.67 +        print >>out
    9.68 +
    9.69 +    def getparams(self, d, args, req=[]):
    9.70 +        """Parse args of the form 'key=val'. Valid keys are the ones
    9.71 +        in the dict 'd' passed in. If entries in 'd' have list values the
    9.72 +        values of the keys are appended.
    9.73 +
    9.74 +        If 'req' is specified it is a list of required keys.
    9.75 +        """
    9.76 +        for x in args:
    9.77 +            (k, v) = x.split('=')
    9.78 +            k = k.strip()
    9.79 +            v = v.strip()
    9.80 +            if k not in d:
    9.81 +                print >>sys.stderr, 'Invalid parameter: ', k
    9.82 +                sys.exit(1)
    9.83 +            vold = d[k]
    9.84 +            if isinstance(vold , types.ListType):
    9.85 +                d[k] = vold + v
    9.86 +            else:
    9.87 +                d[k] = v
    9.88 +        for x in req:
    9.89 +            if not d[x]:
    9.90 +                print >>sys.stderr, 'Missing parameter:', x
    9.91 +            sys.exit(1)
    9.92 +        return d
    9.93 +            
    9.94 +    def vifctl_help(self, args):
    9.95 +        self.help()
    9.96 +
    9.97 +    def default_bridge(self):
    9.98 +        return xroot.get_config_value('bridge', self.DEFAULT_BRIDGE)
    9.99 +
   9.100 +    def default_interface(self):
   9.101 +        return xroot.get_config_value('interface', self.DEFAULT_INTERFACE)
   9.102 +    
   9.103 +    def vifctl_init(self, args):
   9.104 +        """Entry point for 'vifctl init'.
   9.105 +        """
   9.106 +        d = { 'bridge'   : self.default_bridge(),
   9.107 +              'interface': self.default_interface() }
   9.108 +        params = self.getparams(d, args[1:])
   9.109 +        interface = params['interface']
   9.110 +        bridge = params['bridge']
   9.111 +        # Create bridge 'bridge'.
   9.112 +        Brctl.bridge_create(bridge)
   9.113 +        # Reconfigure so that 'interface' is added to 'bridge',
   9.114 +        # and 'bridge' has the IP address from 'interface'.
   9.115 +        Brctl.reconfigure(interface, bridge)
   9.116 +        
   9.117 +    def vifparams(self, args):
   9.118 +        d = { 'vif'   : None,
   9.119 +              'mac'   : None,
   9.120 +              'bridge': self.default_bridge(),
   9.121 +              'ipaddr': [] }
   9.122 +        d = self.getparams(d, args, req=['vif', 'mac'])
   9.123 +        return d
   9.124 +
   9.125 +    def vifctl_up(self, args):
   9.126 +        """Entry point for 'vifctl up'.
   9.127 +        """
   9.128 +        params = self.vifparams(args[1:])
   9.129 +        # Add the vif to its bridge.
   9.130 +        Brctl.vif_bridge_add(params)
   9.131 +        if params['ipaddr']:
   9.132 +            # Add iptables rules for the ip addresses.
   9.133 +            vif = params['vif']
   9.134 +            for ipaddr in params['ipaddr']:
   9.135 +               Brctl.vif_restrict_addr(vif, ipaddr)
   9.136 +
   9.137 +    def vifctl_down(self, args):
   9.138 +        """Entry point for 'vifctl down'.
   9.139 +        """
   9.140 +        params = self.vifparams(args[1:])
   9.141 +        # Remove the vif from its bridge.
   9.142 +        Brctl.vif_bridge_rem(params)
   9.143 +        if params['ip']:
   9.144 +            # Remove iptables rules for the ip addresses.
   9.145 +            vif = params['vif']
   9.146 +            for ip in params['ip']:
   9.147 +               Brctl.vif_restrict_addr(vif, ip, delete=1)
   9.148 +
   9.149 +
   9.150 +if __name__ == "__main__":
   9.151 +    VifControl().main(sys.argv)