ia64/xen-unstable

view tools/xm-test/lib/XmTestLib/Network.py @ 9046:d741fa272309

While testing TCP & UDP tests (coming soon), I tracked down a bug in the
IP addressing scheme
used by xm-test: each machine running network tests is using the same IP
addresses, so two machines
in the same network can cross-pollinate/interfere with each other's tests.

This patch changes the IP addressing scheme to add some randomness. It's
gone through a couple of dozen
passes of xm-test, some on -bridge and some on -route, while xm-test was
running on another box on the same network.

Signed-off-by: Jim Dykman <dykman@us.ibm.com>
author emellor@leeni.uk.xensource.com
date Mon Feb 27 16:14:11 2006 +0100 (2006-02-27)
parents 324caad9e145
children
line source
1 #!/usr/bin/python
2 """
3 Network.py - Common utilities for network tests
5 Copyright (C) International Business Machines Corp., 2005
6 Author: Jim Dykman <dykman@us.ibm.com>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; under version 2 of the License.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 """
22 import sys;
23 import os;
24 import atexit;
25 import random;
27 from Test import *
28 from Xm import *
29 from config import *
31 class NetworkError(Exception):
32 def __init__(self, msg):
33 self.errMsg = msg
35 def __str__(self):
36 return str(self.errMsg)
38 def undo_dom0_alias(eth, ip):
39 traceCommand("ip addr del " + ip + " dev " + eth)
41 def net_from_ip(ip):
42 return ip[:ip.rfind(".")] + ".0/24"
44 class XmNetwork:
46 def __init__(self):
47 # Check for existing zeroconf address. We are using the zeroconf
48 # address range as static IP addresses.... if someone is using
49 # real zeroconf addresses, then we're going to skip tests to
50 # avoid interfering with them.
51 rc, out = traceCommand(
52 "ip addr show |grep \"inet 169.254\" | grep -v vif")
54 if rc == 0:
55 SKIP("Zeroconf address found: " + out)
57 # Randomize one octet of the IP addresses we choose, so that
58 # multiple machines running network tests don't interfere
59 # with each other.
60 self.subnet = random.randint(1,254)
62 def calc_ip_address(self, dom, interface):
63 # Generate an IP address from the dom# and eth#:
64 # 169.254.(self.subnet).(eth#)*16 + (dom# + 1)
65 ethnum = int(interface[len("eth"):])
66 if (ethnum > 15):
67 raise NetworkError("ethnum > 15 : " + interface)
68 domnum = int(dom[len("dom"):])
69 if (domnum > 14):
70 raise NetworkError("domnum > 14 : " + dom)
72 return "169.254."+ str(self.subnet) + "." + str(ethnum*16+domnum+1)
74 def ip(self, dom, interface, todomname=None, toeth=None, bridge=None):
75 newip = self.calc_ip_address(dom, interface)
77 # If the testcase is going to talk to dom0, we need to add an
78 # IP address in the proper subnet
79 if dom == "dom0":
80 if ENABLE_HVM_SUPPORT:
81 # HVM uses ioemu which uses a bridge
82 if not bridge:
83 SKIP("no bridge supplied")
84 else:
85 vifname = bridge
86 else:
87 # The domain's vif is a convenient place to add to
88 vifname = "vif" + str(domid(todomname)) + "." + toeth[3:]
90 # register the exit handler FIRST, just in case
91 atexit.register(undo_dom0_alias, vifname, newip)
93 # add the alias
94 status, output = traceCommand("ip addr add " + newip +
95 " dev " + vifname)
96 if status:
97 SKIP("\"ip addr add\" failed")
99 if ENABLE_HVM_SUPPORT:
100 # We need to add a route to the bridge device
101 network = net_from_ip(newip)
102 status, output = traceCommand("ip route add " + network + " dev " + vifname + " scope link")
104 if status:
105 SKIP("\"ip route add\" failed")
107 return newip
109 def mask(self, dom, interface):
110 return "255.255.255.240"