From patchwork Wed Sep 9 18:11:54 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Goldish X-Patchwork-Id: 46423 Received: from vger.kernel.org (vger.kernel.org [209.132.176.167]) by demeter.kernel.org (8.14.2/8.14.2) with ESMTP id n89IFT8j019092 for ; Wed, 9 Sep 2009 18:15:30 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752506AbZIISPZ (ORCPT ); Wed, 9 Sep 2009 14:15:25 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1752487AbZIISPZ (ORCPT ); Wed, 9 Sep 2009 14:15:25 -0400 Received: from mx1.redhat.com ([209.132.183.28]:44123 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752046AbZIISPY (ORCPT ); Wed, 9 Sep 2009 14:15:24 -0400 Received: from int-mx05.intmail.prod.int.phx2.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.18]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id n89IFQlj017153; Wed, 9 Sep 2009 14:15:26 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx05.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id n89IFPKl017591; Wed, 9 Sep 2009 14:15:26 -0400 Received: from localhost.localdomain (dhcp-1-188.tlv.redhat.com [10.35.1.188]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id n89IFNFM012008; Wed, 9 Sep 2009 14:15:23 -0400 From: Michael Goldish To: autotest@test.kernel.org, kvm@vger.kernel.org Cc: Michael Goldish Subject: [PATCH 01/19] KVM test: kvm_utils.py: make verify_ip_address_ownership() more robust Date: Wed, 9 Sep 2009 21:11:54 +0300 Message-Id: <1252519932-30733-1-git-send-email-mgoldish@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.18 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 --- 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)