diff --git a/tools/xc/py/XenoUtil.py b/tools/xc/py/XenoUtil.py index f9b8728290..1a135bfdff 100644 --- a/tools/xc/py/XenoUtil.py +++ b/tools/xc/py/XenoUtil.py @@ -64,3 +64,67 @@ def lookup_blkdev_partn_info(partition): string.atol(m.group(2)), m.group(3) ) return None + + +def get_current_ipaddr(dev='eth0'): + """Return a string containing the primary IP address for the given + network interface (default 'eth0'). + """ + fd = os.popen( '/sbin/ifconfig ' + dev + ' 2>/dev/null' ) + lines = fd.readlines() + for line in lines: + m = re.search( '^\s+inet addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*', + line ) + if m: + return m.group(1) + return None + +def get_current_ipmask(dev='eth0'): + """Return a string containing the primary IP netmask for the given + network interface (default 'eth0'). + """ + fd = os.popen( '/sbin/ifconfig ' + dev + ' 2>/dev/null' ) + lines = fd.readlines() + for line in lines: + m = re.search( '^.+Mask:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*', + line ) + if m: + return m.group(1) + return None + +def get_current_ipgw(dev='eth0'): + """Return a string containing the IP gateway for the given + network interface (default 'eth0'). + """ + fd = os.popen( '/sbin/route -n' ) + lines = fd.readlines() + for line in lines: + m = re.search( '^\S+\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)' + + '\s+\S+\s+\S*G.*' + dev + '.*', line ) + if m: + return m.group(1) + return None + +def setup_vfr_rules_for_vif(dom,vif,addr): + """Takes a tuple ( domain-id, vif-id, ip-addr ), where the ip-addr + is expressed as a textual dotted quad, and set up appropriate routing + rules in Xen. No return value. + """ + fd = os.open( '/proc/xeno/vfr', os.O_WRONLY ) + if ( re.search( '169\.254', addr) ): + os.write( fd, 'ADD ACCEPT srcaddr=' + addr + + ' srcaddrmask=255.255.255.255' + + ' srcdom=' + str(dom) + ' srcidx=' + str(vif) + + ' dstdom=0 dstidx=0 proto=any\n' ) + else: + os.write( fd, 'ADD ACCEPT srcaddr=' + addr + + ' srcaddrmask=255.255.255.255' + + ' srcdom=' + str(dom) + ' srcidx=' + str(vif) + + ' dst=PHYS proto=any\n' ) + os.write( fd, 'ADD ACCEPT dstaddr=' + addr + + ' dstaddrmask=255.255.255.255' + + ' src=ANY' + + ' dstdom=' + str(dom) + ' dstidx=' + str(vif) + + ' proto=any\n' ) + os.close( fd ) + return None