From: Lucas Meneghel Rodrigues Subject: [PATCH] KVM test: Add support for ipv6 addresses Date: Wed, 24 Feb 2010 01:10:56 -0300 Message-ID: <1266984656-4036-1-git-send-email-lmr@redhat.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------1.6.6.1" Cc: Yogananth Subramanian , kvm@vger.kernel.org To: autotest@test.kernel.org Return-path: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: autotest-bounces@test.kernel.org Errors-To: autotest-bounces@test.kernel.org List-Id: kvm.vger.kernel.org This is a multi-part message in MIME format. --------------1.6.6.1 Content-Type: text/plain; charset=UTF-8; format=fixed Content-Transfer-Encoding: quoted-printable This patch enables ipv6 address support in kvm-autotest. The patch adds a new dictionary called "address6_cache" for ip6 address. Tcpdump is used to create this cache of link-local ipv6 address. Link-local ipv6 address is used because it eliminates to need to create complex configuration on both the host and the guest. The ipv6 address for a guest can be obtained by using the new function get_address6 in kvm_vm.py Signed-off-by: Yogananth Subramanian --- client/tests/kvm/kvm_preprocessing.py | 29 ++++++++++++++++++- client/tests/kvm/kvm_utils.py | 28 +++++++++++++++++++ client/tests/kvm/kvm_vm.py | 49 +++++++++++++++++++++++++++= ++++-- 3 files changed, 101 insertions(+), 5 deletions(-) --------------1.6.6.1 Content-Type: text/x-patch; name="0001-KVM-test-Add-support-for-ipv6-addresses.patch" Content-Disposition: attachment; filename="0001-KVM-test-Add-support-for-ipv6-addresses.patch" Content-Transfer-Encoding: quoted-printable diff --git a/client/tests/kvm/kvm_preprocessing.py b/client/tests/kvm/kvm= _preprocessing.py index e91d1da..4ea13b2 100644 --- a/client/tests/kvm/kvm_preprocessing.py +++ b/client/tests/kvm/kvm_preprocessing.py @@ -51,7 +51,8 @@ def preprocess_vm(test, params, env, name): logging.debug("VM object found in environment") else: logging.debug("VM object does not exist; creating it") - vm =3D kvm_vm.VM(name, params, test.bindir, env.get("address_cac= he")) + vm =3D kvm_vm.VM(name, params, test.bindir, env.get("address_cac= he"), + env.get("address6_cache")) kvm_utils.env_register_vm(env, name, vm) =20 start_vm =3D False @@ -199,6 +200,14 @@ def preprocess(test, params, env): command=3Dcommand, output_func=3D_update_address_cache, output_params=3D(env["address_cache"],)) + + env["address6_cache"] =3D {} + command6 =3D "/usr/sbin/tcpdump -npv ip6 -i any 'dst net ff02::2= '" + env["tcpdump6"] =3D kvm_subprocess.kvm_tail( + command=3Dcommand6, + output_func=3D_update_address6_cache, + output_params=3D(env["address6_cache"],)) + if kvm_utils.wait_for(lambda: not env["tcpdump"].is_alive(), 0.1, 0.1, 1.0): logging.warn("Could not start tcpdump") @@ -321,6 +330,8 @@ def postprocess(test, params, env): if not living_vms and env.has_key("tcpdump"): env["tcpdump"].close() del env["tcpdump"] + env["tcpdump6"].close() + del env["tcpdump6"] =20 =20 def postprocess_on_error(test, params, env): @@ -343,7 +354,21 @@ def _update_address_cache(address_cache, line): matches =3D re.findall(r"\w*:\w*:\w*:\w*:\w*:\w*", line) if matches and address_cache.get("last_seen"): mac_address =3D matches[0].lower() - logging.debug("(address cache) Adding cache entry: %s ---> %= s", + logging.debug("(address cache ipv4) Adding cache entry: %s -= --> %s", mac_address, address_cache.get("last_seen")) address_cache[mac_address] =3D address_cache.get("last_seen"= ) del address_cache["last_seen"] + +def _update_address6_cache(address6_cache, line): + if re.search("ff02::2:", line, re.IGNORECASE): + matches =3D re.findall(r"(fe80.+) >", line) + if matches: + address6_cache["last_seen"] =3D matches[0] + if re.search("source link-address option", line, re.IGNORECASE): + matches =3D re.findall(r"\w*:\w*:\w*:\w*:\w*:\w*", line) + if matches and address6_cache.get("last_seen"): + mac_address =3D matches[0].lower() + logging.debug("(address cache ipv6) Adding cache entry: %s -= --> %s", + mac_address, address6_cache.get("last_seen")) + address6_cache[mac_address] =3D address6_cache.get("last_see= n") + del address6_cache["last_seen"] diff --git a/client/tests/kvm/kvm_utils.py b/client/tests/kvm/kvm_utils.p= y index 4565dc1..a574520 100644 --- a/client/tests/kvm/kvm_utils.py +++ b/client/tests/kvm/kvm_utils.py @@ -208,6 +208,34 @@ def verify_ip_address_ownership(ip, macs, timeout=3D= 10.0): o =3D commands.getoutput("/sbin/arping -f -c 3 -I %s %s" % (dev, ip)= ) return bool(regex.search(o)) =20 +def verify_ip6_address_ownership(ip, macs, timeout=3D10.0): + """ + Use ping6 and the ND cache to make sure a given IP address belongs t= o one + of the given MAC addresses. + + @param ip: An IP6 address. + @param macs: A list or tuple of MAC addresses. + @return: True iff ip is assigned to a MAC address in macs. + """ + # Compile a regex that matches the given IP6 address and any of the = given + # MAC addresses + mac_regex =3D "|".join("(%s)" % mac for mac in macs) + regex =3D re.compile(r"\b%s\b.*\b(%s)\b" % (ip, mac_regex), re.IGNOR= ECASE) + + # Get the name of the bridge device for ping6 + o =3D commands.getoutput("/sbin/ip route get ff02::1" ) + dev =3D re.findall("dev\s+\S+", o, re.IGNORECASE) + if not dev: + return False + dev =3D dev[0].split()[-1] + + if bool(commands.getstatusoutput("/bin/ping6 -c 11 -I %s %s"% (dev, = ip)[0])): + return False + + # Check the ND cache + o =3D commands.getoutput("/sbin/ip -6 neigh show") + if regex.search(o): + return True =20 # Functions for working with the environment (a dict-like object) =20 diff --git a/client/tests/kvm/kvm_vm.py b/client/tests/kvm/kvm_vm.py index 921414d..76babd4 100755 --- a/client/tests/kvm/kvm_vm.py +++ b/client/tests/kvm/kvm_vm.py @@ -95,7 +95,7 @@ class VM: This class handles all basic VM operations. """ =20 - def __init__(self, name, params, root_dir, address_cache): + def __init__(self, name, params, root_dir, address_cache, address6_c= ache): """ Initialize the object and set a few attributes. =20 @@ -104,6 +104,7 @@ class VM: (see method make_qemu_command for a full description) @param root_dir: Base directory for relative filenames @param address_cache: A dict that maps MAC addresses to IP addre= sses + @param address_cache: A dict that maps MAC addresses to IP6 addr= esses """ self.process =3D None self.redirs =3D {} @@ -114,6 +115,7 @@ class VM: self.params =3D params self.root_dir =3D root_dir self.address_cache =3D address_cache + self.address6_cache =3D address6_cache self.pci_assignable =3D None =20 # Find available monitor filename @@ -127,7 +129,8 @@ class VM: break =20 =20 - def clone(self, name=3DNone, params=3DNone, root_dir=3DNone, address= _cache=3DNone): + def clone(self, name=3DNone, params=3DNone, root_dir=3DNone, address= _cache=3DNone, + address6_cache=3DNone): """ Return a clone of the VM object with optionally modified paramet= ers. The clone is initially not alive and needs to be started using c= reate(). @@ -138,6 +141,7 @@ class VM: @param params: Optional new VM creation parameters @param root_dir: Optional new base directory for relative filena= mes @param address_cache: A dict that maps MAC addresses to IP addre= sses + @param address_cache: A dict that maps MAC addresses to IP6 addr= esses """ if name is None: name =3D self.name @@ -147,7 +151,9 @@ class VM: root_dir =3D self.root_dir if address_cache is None: address_cache =3D self.address_cache - return VM(name, params, root_dir, address_cache) + if address6_cache is None: + address6_cache =3D self.address6_cache + return VM(name, params, root_dir, address_cache, address6_cache) =20 =20 def make_qemu_command(self, name=3DNone, params=3DNone, root_dir=3DN= one): @@ -720,6 +726,43 @@ class VM: else: return "localhost" =20 + def get_address6(self, index=3D0): + """ + Return the address of a NIC of the guest, in host space. + + If port redirection is used, return 'localhost' (the NIC has no = IP + address of its own). Otherwise return the NIC's IP6 address. + + @param index: Index of the NIC whose address is requested. + """ + nics =3D kvm_utils.get_sub_dict_names(self.params, "nics") + nic_name =3D nics[index] + nic_params =3D kvm_utils.get_sub_dict(self.params, nic_name) + if nic_params.get("nic_mode") =3D=3D "tap": + mac, ip =3D kvm_utils.get_mac_ip_pair_from_dict(nic_params) + if not mac: + logging.debug("MAC address unavailable") + return None + if not ip or nic_params.get("always_use_tcpdump") =3D=3D "ye= s": + # Get the IP6 address from the cache + ip =3D self.address6_cache.get(mac) + if not ip: + logging.debug("Could not find IP address for MAC add= ress: " + "%s" % mac) + return None + # Make sure the IP6 address is assigned to this guest + nic_dicts =3D [kvm_utils.get_sub_dict(self.params, nic) + for nic in nics] + macs =3D [kvm_utils.get_mac_ip_pair_from_dict(dict)[0] + for dict in nic_dicts] + if not kvm_utils.verify_ip6_address_ownership(ip, macs): + logging.debug("Could not verify MAC-IP address mappi= ng: " + "%s ---> %s" % (mac, ip)) + return None + return ip + else: + return "localhost" + =20 def get_port(self, port, nic_index=3D0): """ --------------1.6.6.1 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Autotest mailing list Autotest@test.kernel.org http://test.kernel.org/cgi-bin/mailman/listinfo/autotest --------------1.6.6.1--