ia64/xen-unstable

changeset 970:ea0572c4bc9f

bitkeeper revision 1.624 (3fbdff8cjOXXJYub36D6Ko7MDfqOsA)

XenoUtil.py:
More XenoUtil funcs.
author kaf24@scramble.cl.cam.ac.uk
date Fri Nov 21 12:05:32 2003 +0000 (2003-11-21)
parents 65538950807b
children b5c1b4c684f2
files tools/xc/py/XenoUtil.py
line diff
     1.1 --- a/tools/xc/py/XenoUtil.py	Fri Nov 21 11:18:29 2003 +0000
     1.2 +++ b/tools/xc/py/XenoUtil.py	Fri Nov 21 12:05:32 2003 +0000
     1.3 @@ -64,3 +64,67 @@ def lookup_blkdev_partn_info(partition):
     1.4                       string.atol(m.group(2)),
     1.5                       m.group(3) )
     1.6      return None
     1.7 +
     1.8 +
     1.9 +def get_current_ipaddr(dev='eth0'):
    1.10 +    """Return a string containing the primary IP address for the given
    1.11 +    network interface (default 'eth0').
    1.12 +    """
    1.13 +    fd = os.popen( '/sbin/ifconfig ' + dev + ' 2>/dev/null' )
    1.14 +    lines = fd.readlines()
    1.15 +    for line in lines:
    1.16 +        m = re.search( '^\s+inet addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*',
    1.17 +                       line )
    1.18 +        if m:
    1.19 +            return m.group(1)
    1.20 +    return None
    1.21 +
    1.22 +def get_current_ipmask(dev='eth0'):
    1.23 +    """Return a string containing the primary IP netmask for the given
    1.24 +    network interface (default 'eth0').
    1.25 +    """
    1.26 +    fd = os.popen( '/sbin/ifconfig ' + dev + ' 2>/dev/null' )
    1.27 +    lines = fd.readlines()
    1.28 +    for line in lines:
    1.29 +        m = re.search( '^.+Mask:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*',
    1.30 +                       line )
    1.31 +        if m:
    1.32 +            return m.group(1)
    1.33 +    return None
    1.34 +
    1.35 +def get_current_ipgw(dev='eth0'):
    1.36 +    """Return a string containing the IP gateway for the given
    1.37 +    network interface (default 'eth0').
    1.38 +    """
    1.39 +    fd = os.popen( '/sbin/route -n' )
    1.40 +    lines = fd.readlines()
    1.41 +    for line in lines:
    1.42 +        m = re.search( '^\S+\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)' +
    1.43 +                       '\s+\S+\s+\S*G.*' + dev + '.*', line )
    1.44 +        if m:
    1.45 +            return m.group(1)
    1.46 +    return None
    1.47 +
    1.48 +def setup_vfr_rules_for_vif(dom,vif,addr):
    1.49 +    """Takes a tuple ( domain-id, vif-id, ip-addr ), where the ip-addr
    1.50 +    is expressed as a textual dotted quad, and set up appropriate routing
    1.51 +    rules in Xen. No return value.
    1.52 +    """
    1.53 +    fd = os.open( '/proc/xeno/vfr', os.O_WRONLY )
    1.54 +    if ( re.search( '169\.254', addr) ):
    1.55 +        os.write( fd, 'ADD ACCEPT srcaddr=' + addr +
    1.56 +                  ' srcaddrmask=255.255.255.255' +
    1.57 +                  ' srcdom=' + str(dom) + ' srcidx=' + str(vif) +
    1.58 +                  ' dstdom=0 dstidx=0 proto=any\n' )
    1.59 +    else:
    1.60 +        os.write( fd, 'ADD ACCEPT srcaddr=' + addr +
    1.61 +                  ' srcaddrmask=255.255.255.255' +
    1.62 +                  ' srcdom=' + str(dom) + ' srcidx=' + str(vif) +
    1.63 +                  ' dst=PHYS proto=any\n' )
    1.64 +    os.write( fd, 'ADD ACCEPT dstaddr=' + addr +
    1.65 +              ' dstaddrmask=255.255.255.255' +
    1.66 +              ' src=ANY' +
    1.67 +              ' dstdom=' + str(dom) + ' dstidx=' + str(vif) +
    1.68 +              ' proto=any\n' )
    1.69 +    os.close( fd )
    1.70 +    return None