Message ID | 1252519932-30733-1-git-send-email-mgoldish@redhat.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
As the changes were mostly bugfixes and short, I managed to review and apply them all. Thank you very much Michael! On Wed, Sep 9, 2009 at 3:11 PM, Michael Goldish <mgoldish@redhat.com> wrote: > Use arping in addition to querying the arp cache. > Under certain circumstances a TCP connection may not trigger an ARP request so > it must be triggered explicitly using arping. > > Signed-off-by: Michael Goldish <mgoldish@redhat.com> > --- > Â client/tests/kvm/kvm_utils.py | Â 40 ++++++++++++++++++---------------------- > Â 1 files changed, 18 insertions(+), 22 deletions(-) > > diff --git a/client/tests/kvm/kvm_utils.py b/client/tests/kvm/kvm_utils.py > index dfca938..f046810 100644 > --- a/client/tests/kvm/kvm_utils.py > +++ b/client/tests/kvm/kvm_utils.py > @@ -152,39 +152,35 @@ def get_mac_ip_pair_from_dict(dict): > Â Â return (None, None) > > > -def verify_ip_address_ownership(ip, macs, timeout=3.0): > +def verify_ip_address_ownership(ip, macs, timeout=10.0): > Â Â """ > - Â Â Connect to a given IP address and make sure its MAC address equals one of > - Â Â the given MAC address. > + Â Â Use arping and the ARP cache to make sure a given IP address belongs to one > + Â Â of the given MAC addresses. > > Â Â @param ip: An IP address. > Â Â @param macs: A list or tuple of MAC addresses. > Â Â @return: True iff ip is assigned to a MAC address in macs. > Â Â """ > - Â Â def check_arp_cache(regex): > - Â Â Â Â o = commands.getoutput("/sbin/arp -n") > - Â Â Â Â return bool(re.search(regex, o, re.IGNORECASE)) > - > + Â Â # Compile a regex that matches the given IP address and any of the given > + Â Â # MAC addresses > Â Â mac_regex = "|".join("(%s)" % mac for mac in macs) > Â Â regex = re.compile(r"\b%s\b.*\b(%s)\b" % (ip, mac_regex)) > > - Â Â if check_arp_cache(regex): > + Â Â # Check the ARP cache > + Â Â o = commands.getoutput("/sbin/arp -n") > + Â Â if re.search(regex, o, re.IGNORECASE): > Â Â Â Â return True > > - Â Â s = socket.socket() > - Â Â s.setblocking(False) > - Â Â try: > - Â Â Â Â s.connect((ip, 55555)) > - Â Â except socket.error: > - Â Â Â Â pass > - Â Â end_time = time.time() + timeout > - Â Â while time.time() < end_time: > - Â Â Â Â time.sleep(0.2) > - Â Â Â Â if check_arp_cache(regex): > - Â Â Â Â Â Â s.close() > - Â Â Â Â Â Â return True > - Â Â s.close() > - Â Â return False > + Â Â # Get the name of the bridge device for arping > + Â Â o = commands.getoutput("/sbin/ip route get %s" % ip) > + Â Â dev = re.findall("dev\s+\S+", o, re.IGNORECASE) > + Â Â if not dev: > + Â Â Â Â return False > + Â Â dev = dev[0].split()[-1] > + > + Â Â # Send an ARP request > + Â Â o = commands.getoutput("/sbin/arping -f -c 3 -I %s %s" % (dev, ip)) > + Â Â return bool(re.search(regex, o, re.IGNORECASE)) > > > Â # Functions for working with the environment (a dict-like object) > -- > 1.5.4.1 > > _______________________________________________ > Autotest mailing list > Autotest@test.kernel.org > http://test.kernel.org/cgi-bin/mailman/listinfo/autotest >
diff --git a/client/tests/kvm/kvm_utils.py b/client/tests/kvm/kvm_utils.py index dfca938..f046810 100644 --- a/client/tests/kvm/kvm_utils.py +++ b/client/tests/kvm/kvm_utils.py @@ -152,39 +152,35 @@ def get_mac_ip_pair_from_dict(dict): return (None, None) -def verify_ip_address_ownership(ip, macs, timeout=3.0): +def verify_ip_address_ownership(ip, macs, timeout=10.0): """ - Connect to a given IP address and make sure its MAC address equals one of - the given MAC address. + Use arping and the ARP cache to make sure a given IP address belongs to one + of the given MAC addresses. @param ip: An IP address. @param macs: A list or tuple of MAC addresses. @return: True iff ip is assigned to a MAC address in macs. """ - def check_arp_cache(regex): - o = commands.getoutput("/sbin/arp -n") - return bool(re.search(regex, o, re.IGNORECASE)) - + # Compile a regex that matches the given IP address and any of the given + # MAC addresses mac_regex = "|".join("(%s)" % mac for mac in macs) regex = re.compile(r"\b%s\b.*\b(%s)\b" % (ip, mac_regex)) - if check_arp_cache(regex): + # Check the ARP cache + o = commands.getoutput("/sbin/arp -n") + if re.search(regex, o, re.IGNORECASE): return True - s = socket.socket() - s.setblocking(False) - try: - s.connect((ip, 55555)) - except socket.error: - pass - end_time = time.time() + timeout - while time.time() < end_time: - time.sleep(0.2) - if check_arp_cache(regex): - s.close() - return True - s.close() - return False + # Get the name of the bridge device for arping + o = commands.getoutput("/sbin/ip route get %s" % ip) + dev = re.findall("dev\s+\S+", o, re.IGNORECASE) + if not dev: + return False + dev = dev[0].split()[-1] + + # Send an ARP request + o = commands.getoutput("/sbin/arping -f -c 3 -I %s %s" % (dev, ip)) + return bool(re.search(regex, o, re.IGNORECASE)) # Functions for working with the environment (a dict-like object)
Use arping in addition to querying the arp cache. Under certain circumstances a TCP connection may not trigger an ARP request so it must be triggered explicitly using arping. Signed-off-by: Michael Goldish <mgoldish@redhat.com> --- client/tests/kvm/kvm_utils.py | 40 ++++++++++++++++++---------------------- 1 files changed, 18 insertions(+), 22 deletions(-)