From patchwork Wed Aug 12 16:00:00 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Goldish X-Patchwork-Id: 40912 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 n7CFwLLW005478 for ; Wed, 12 Aug 2009 15:58:23 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754038AbZHLP5a (ORCPT ); Wed, 12 Aug 2009 11:57:30 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1754036AbZHLP53 (ORCPT ); Wed, 12 Aug 2009 11:57:29 -0400 Received: from mx2.redhat.com ([66.187.237.31]:42770 "EHLO mx2.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754031AbZHLP52 (ORCPT ); Wed, 12 Aug 2009 11:57:28 -0400 Received: from int-mx2.corp.redhat.com (int-mx2.corp.redhat.com [172.16.27.26]) by mx2.redhat.com (8.13.8/8.13.8) with ESMTP id n7CFvTvk005569; Wed, 12 Aug 2009 11:57:29 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx2.corp.redhat.com (8.13.1/8.13.1) with ESMTP id n7CFvRJZ004567; Wed, 12 Aug 2009 11:57:28 -0400 Received: from localhost.localdomain (dhcp-1-31.tlv.redhat.com [10.35.1.31]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id n7CFvFCK029667; Wed, 12 Aug 2009 11:57:26 -0400 From: Michael Goldish To: autotest@test.kernel.org, kvm@vger.kernel.org Cc: Michael Goldish Subject: [KVM-AUTOTEST PATCH v2 08/11] KVM test: dynamically detect and cache MAC-IP pairs using tcpdump Date: Wed, 12 Aug 2009 19:00:00 +0300 Message-Id: In-Reply-To: <40e596a8c1d099f0afdd2ad18f2f4a0908b89f20.1250091576.git.mgoldish@redhat.com> References: <1250092803-32477-1-git-send-email-mgoldish@redhat.com> <10a541e518180735e86a4dbc8e759912d0ec314c.1250091576.git.mgoldish@redhat.com> <42ceeed70c7c1ec9f4d78d91d67a290f3ac11e6e.1250091576.git.mgoldish@redhat.com> <8284ff0173612b63412132bb8c864f2c8a258b55.1250091576.git.mgoldish@redhat.com> <05eac6f84757616309273b01ac4d8800efbdb083.1250091576.git.mgoldish@redhat.com> <5fbc30f0d24e3d4b12b93f40fe14264e5963048c.1250091576.git.mgoldish@redhat.com> <40e596a8c1d099f0afdd2ad18f2f4a0908b89f20.1250091576.git.mgoldish@redhat.com> In-Reply-To: <10a541e518180735e86a4dbc8e759912d0ec314c.1250091576.git.mgoldish@redhat.com> References: <10a541e518180735e86a4dbc8e759912d0ec314c.1250091576.git.mgoldish@redhat.com> X-Scanned-By: MIMEDefang 2.58 on 172.16.27.26 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Run tcpdump in the background at all times and listen for DHCP communication. Whenever a DHCP server assigns an IP address to a MAC address, cache it for future reference. Signed-off-by: Michael Goldish --- client/tests/kvm/kvm_preprocessing.py | 41 +++++++++++++++++++++++++++++++++ 1 files changed, 41 insertions(+), 0 deletions(-) diff --git a/client/tests/kvm/kvm_preprocessing.py b/client/tests/kvm/kvm_preprocessing.py index 22c2b8b..20b8cf6 100644 --- a/client/tests/kvm/kvm_preprocessing.py +++ b/client/tests/kvm/kvm_preprocessing.py @@ -196,6 +196,26 @@ def preprocess(test, params, env): @param params: A dict containing all VM and image parameters. @param env: The environment (a dict-like object). """ + # Start tcpdump if it isn't already running + if not env.has_key("address_cache"): + env["address_cache"] = {} + if env.has_key("tcpdump") and not env["tcpdump"].is_alive(): + env["tcpdump"].close() + del env["tcpdump"] + if not env.has_key("tcpdump"): + command = "/usr/sbin/tcpdump -npvi any 'dst port 68'" + logging.debug("Starting tcpdump (%s)...", command) + env["tcpdump"] = kvm_subprocess.kvm_tail( + command=command, + output_func=_update_address_cache, + output_params=(env["address_cache"],)) + if kvm_utils.wait_for(lambda: not env["tcpdump"].is_alive(), + 0.1, 0.1, 1.0): + logging.warn("Could not start tcpdump") + logging.warn("Status: %s" % env["tcpdump"].get_status()) + logging.warn("Output:" + kvm_utils.format_str_for_message( + env["tcpdump"].get_output())) + # Destroy and remove VMs that are no longer needed in the environment requested_vms = kvm_utils.get_sub_dict_names(params, "vms") for key in env.keys(): @@ -280,6 +300,12 @@ def postprocess(test, params, env): int(params.get("post_command_timeout", "600")), params.get("post_command_noncritical") == "yes") + # Terminate tcpdump if no VMs are alive + living_vms = [vm for vm in kvm_utils.env_get_all_vms(env) if vm.is_alive()] + if not living_vms and env.has_key("tcpdump"): + env["tcpdump"].close() + del env["tcpdump"] + def postprocess_on_error(test, params, env): """ @@ -290,3 +316,18 @@ def postprocess_on_error(test, params, env): @param env: The environment (a dict-like object). """ params.update(kvm_utils.get_sub_dict(params, "on_error")) + + +def _update_address_cache(address_cache, line): + if re.search("Your.IP", line, re.IGNORECASE): + matches = re.findall(r"\d*\.\d*\.\d*\.\d*", line) + if matches: + address_cache["last_seen"] = matches[0] + if re.search("Client.Ethernet.Address", line, re.IGNORECASE): + matches = re.findall(r"\w*:\w*:\w*:\w*:\w*:\w*", line) + if matches and address_cache.get("last_seen"): + mac_address = matches[0].lower() + logging.debug("(address cache) Adding cache entry: %s ---> %s", + mac_address, address_cache.get("last_seen")) + address_cache[mac_address] = address_cache.get("last_seen") + del address_cache["last_seen"]