Exemple #1
0
    def get_address(self, index=0):
        """
        Return the IP address of a NIC or guest (in host space).

        :param index: Name or index of the NIC whose address is requested.
        :return: 'localhost': Port redirection is in use
        :return: IP address of NIC if valid in arp cache.
        :raise VMMACAddressMissingError: If no MAC address is defined for the
                requested NIC
        :raise VMIPAddressMissingError: If no IP address is found for the the
                NIC's MAC address
        :raise VMAddressVerificationError: If the MAC-IP address mapping cannot
                be verified (using arping)
        """
        if self.virtnet < index:
            raise VMInterfaceIndexError()
        nic = self.virtnet[index]
        # TODO: Determine port redirection in use w/o checking nettype
        if nic.nettype not in ['bridge', 'macvtap']:
            return "localhost"
        if not nic.has_key('mac') and self.params.get('vm_type') == 'libvirt':
            # Look it up from xml
            nic.mac = self.get_virsh_mac_address(index)
        # else TODO: Look up mac from existing qemu-kvm process
        if not nic.has_key('mac'):
            raise VMMACAddressMissingError(index)

        # Get the IP address from arp cache, try upper and lower case
        arp_ip = self.address_cache.get(nic.mac.upper())
        if not arp_ip:
            arp_ip = self.address_cache.get(nic.mac.lower())

        if not arp_ip and os.geteuid() != 0:
            # For non-root, tcpdump won't work for finding IP address, try arp
            ip_map = utils_net.parse_arp()
            arp_ip = ip_map.get(nic.mac.lower())
            if arp_ip:
                self.address_cache[nic.mac.lower()] = arp_ip

        if not arp_ip:
            raise VMIPAddressMissingError(nic.mac)

        # Make sure the IP address is assigned to one or more macs
        # for this guest
        macs = self.virtnet.mac_list()

        if not utils_net.verify_ip_address_ownership(arp_ip, macs):
            raise VMAddressVerificationError(nic.mac, arp_ip)
        logging.debug('Found/Verified IP %s for VM %s NIC %s' %
                      (arp_ip, self.name, str(index)))
        return arp_ip
Exemple #2
0
    def get_address(self, index=0):
        """
        Return the IP address of a NIC or guest (in host space).

        :param index: Name or index of the NIC whose address is requested.
        :return: 'localhost': Port redirection is in use
        :return: IP address of NIC if valid in arp cache.
        :raise VMMACAddressMissingError: If no MAC address is defined for the
                requested NIC
        :raise VMIPAddressMissingError: If no IP address is found for the the
                NIC's MAC address
        :raise VMAddressVerificationError: If the MAC-IP address mapping cannot
                be verified (using arping)
        """
        if self.virtnet < index:
            raise VMInterfaceIndexError()
        nic = self.virtnet[index]
        # TODO: Determine port redirection in use w/o checking nettype
        if nic.nettype not in ['bridge', 'macvtap']:
            return "localhost"
        if not nic.has_key('mac') and self.params.get('vm_type') == 'libvirt':
            # Look it up from xml
            nic.mac = self.get_virsh_mac_address(index)
        # else TODO: Look up mac from existing qemu-kvm process
        if not nic.has_key('mac'):
            raise VMMACAddressMissingError(index)

        # Get the IP address from arp cache, try upper and lower case
        arp_ip = self.address_cache.get(nic.mac.upper())
        if not arp_ip:
            arp_ip = self.address_cache.get(nic.mac.lower())

        if not arp_ip and os.geteuid() != 0:
            # For non-root, tcpdump won't work for finding IP address, try arp
            ip_map = utils_net.parse_arp()
            arp_ip = ip_map.get(nic.mac.lower())
            if arp_ip:
                self.address_cache[nic.mac.lower()] = arp_ip

        if not arp_ip:
            raise VMIPAddressMissingError(nic.mac)

        # Make sure the IP address is assigned to one or more macs
        # for this guest
        macs = self.virtnet.mac_list()

        if not utils_net.verify_ip_address_ownership(arp_ip, macs):
            raise VMAddressVerificationError(nic.mac, arp_ip)
        logging.debug('Found/Verified IP %s for VM %s NIC %s' % (
            arp_ip, self.name, str(index)))
        return arp_ip
Exemple #3
0
    def get_address(self, index=0):
        """
        Return the IP address of a NIC or guest (in host space).

        :param index: Name or index of the NIC whose address is requested.
        :return: 'localhost': Port redirection is in use
        :return: IP address of NIC if valid in arp cache.
        :raise VMMACAddressMissingError: If no MAC address is defined for the
                requested NIC
        :raise VMIPAddressMissingError: If no IP address is found for the the
                NIC's MAC address
        :raise VMAddressVerificationError: If the MAC-IP address mapping cannot
                be verified (using arping)
        """
        nic = self.virtnet[index]
        self.ip_version = self.params.get("ip_version", "ipv4").lower()
        # TODO: Determine port redirection in use w/o checking nettype
        if nic.nettype not in ['bridge', 'macvtap']:
            hostname = socket.gethostname()
            return socket.gethostbyname(hostname)
        if not nic.has_key('mac') and self.params.get('vm_type') == 'libvirt':
            # Look it up from xml
            nic.mac = self.get_virsh_mac_address(index)
        # else TODO: Look up mac from existing qemu-kvm process
        if not nic.has_key('mac'):
            raise VMMACAddressMissingError(index)
        if self.ip_version == "ipv4":
            # Get the IP address from arp cache, try upper and lower case
            arp_ip = self.address_cache.get(nic.mac.upper())
            if not arp_ip:
                arp_ip = self.address_cache.get(nic.mac.lower())

            if not arp_ip and os.geteuid() != 0:
                # For non-root, tcpdump won't work for finding IP address,
                # try arp
                ip_map = utils_net.parse_arp()
                arp_ip = ip_map.get(nic.mac.lower())
                if arp_ip:
                    self.address_cache[nic.mac.lower()] = arp_ip

            if not arp_ip:
                raise VMIPAddressMissingError(nic.mac)

            # Make sure the IP address is assigned to one or more macs
            # for this guest
            macs = self.virtnet.mac_list()

            # SR-IOV cards may not be in same subnet with the card used by
            # host by default, so arp checks won't work. Therefore, do not
            # raise VMAddressVerificationError when SR-IOV is used.
            nic_params = self.params.object_params(nic.nic_name)
            pci_assignable = nic_params.get("pci_assignable") != "no"

            if not utils_net.verify_ip_address_ownership(arp_ip, macs):
                if pci_assignable:
                    msg = "Could not verify DHCP lease: %s-> %s." % (nic.mac,
                                                                     arp_ip)
                    msg += (" Maybe %s is not in the same subnet "
                            "as the host (SR-IOV in use)" % arp_ip)
                    logging.error(msg)
                else:
                    raise VMAddressVerificationError(nic.mac, arp_ip)

            logging.debug('Found/Verified IP %s for VM %s NIC %s',
                          arp_ip, self.name, str(index))
            return arp_ip

        elif self.ip_version == "ipv6":
            # Try to get and return IPV6 address
            if self.params.get('using_linklocal') == "yes":
                ipv6_addr = utils_net.ipv6_from_mac_addr(nic.mac)
            # Using global address
            else:
                mac_key = "%s_6" % nic.mac
                ipv6_addr = self.address_cache.get(mac_key.lower())
            if not ipv6_addr:
                raise VMIPAddressMissingError(nic.mac)
            # Check whether the ipv6 address is reachable
            utils_net.refresh_neigh_table(nic.netdst, ipv6_addr)
            if not utils_misc.wait_for(lambda: utils_net.neigh_reachable(
                                       ipv6_addr, nic.netdst),
                                       30, 0, 1, "Wait neighbour reachable"):
                raise VMAddressVerificationError(nic.mac, ipv6_addr)
            return ipv6_addr
Exemple #4
0
    def get_address(self, index=0):
        """
        Return the IP address of a NIC or guest (in host space).

        :param index: Name or index of the NIC whose address is requested.
        :return: 'localhost': Port redirection is in use
        :return: IP address of NIC if valid in arp cache.
        :raise VMMACAddressMissingError: If no MAC address is defined for the
                requested NIC
        :raise VMIPAddressMissingError: If no IP address is found for the the
                NIC's MAC address
        :raise VMAddressVerificationError: If the MAC-IP address mapping cannot
                be verified (using arping)
        """
        nic = self.virtnet[index]
        self.ip_version = self.params.get("ip_version", "ipv4").lower()
        # TODO: Determine port redirection in use w/o checking nettype
        if nic.nettype not in ['bridge', 'macvtap']:
            hostname = socket.gethostname()
            return socket.gethostbyname(hostname)
        if not nic.has_key('mac') and self.params.get('vm_type') == 'libvirt':
            # Look it up from xml
            nic.mac = self.get_virsh_mac_address(index)
        # else TODO: Look up mac from existing qemu-kvm process
        if not nic.has_key('mac'):
            raise VMMACAddressMissingError(index)
        if self.ip_version == "ipv4":
            # Get the IP address from arp cache, try upper and lower case
            arp_ip = self.address_cache.get(nic.mac.upper())
            if not arp_ip:
                arp_ip = self.address_cache.get(nic.mac.lower())

            if not arp_ip and os.geteuid() != 0:
                # For non-root, tcpdump won't work for finding IP address,
                # try arp
                ip_map = utils_net.parse_arp()
                arp_ip = ip_map.get(nic.mac.lower())
                if arp_ip:
                    self.address_cache[nic.mac.lower()] = arp_ip

            if not arp_ip:
                raise VMIPAddressMissingError(nic.mac)

            # Make sure the IP address is assigned to one or more macs
            # for this guest
            macs = self.virtnet.mac_list()

            # SR-IOV cards may not be in same subnet with the card used by
            # host by default, so arp checks won't work. Therefore, do not
            # raise VMAddressVerificationError when SR-IOV is used.
            nic_params = self.params.object_params(nic.nic_name)
            pci_assignable = nic_params.get("pci_assignable") != "no"

            if not utils_net.verify_ip_address_ownership(arp_ip, macs):
                if pci_assignable:
                    msg = "Could not verify DHCP lease: %s-> %s." % (nic.mac,
                                                                     arp_ip)
                    msg += (" Maybe %s is not in the same subnet "
                            "as the host (SR-IOV in use)" % arp_ip)
                    logging.error(msg)
                else:
                    raise VMAddressVerificationError(nic.mac, arp_ip)

            logging.debug('Found/Verified IP %s for VM %s NIC %s', arp_ip,
                          self.name, str(index))
            return arp_ip

        elif self.ip_version == "ipv6":
            # Try to get and return IPV6 address
            if self.params.get('using_linklocal') == "yes":
                ipv6_addr = utils_net.ipv6_from_mac_addr(nic.mac)
            # Using global address
            else:
                mac_key = "%s_6" % nic.mac
                ipv6_addr = self.address_cache.get(mac_key.lower())
            if not ipv6_addr:
                raise VMIPAddressMissingError(nic.mac)
            # Check whether the ipv6 address is reachable
            utils_net.refresh_neigh_table(nic.netdst, ipv6_addr)
            if not utils_misc.wait_for(
                    lambda: utils_net.neigh_reachable(ipv6_addr, nic.netdst),
                    30, 0, 1, "Wait neighbour reachable"):
                raise VMAddressVerificationError(nic.mac, ipv6_addr)
            return ipv6_addr
Exemple #5
0
    def get_address(self, index=0):
        """
        Return the IP address of a NIC or guest (in host space).

        :param index: Name or index of the NIC whose address is requested.
        :return: 'localhost': Port redirection is in use
        :return: IP address of NIC if valid in arp cache.
        :raise VMMACAddressMissingError: If no MAC address is defined for the
                requested NIC
        :raise VMIPAddressMissingError: If no IP address is found for the the
                NIC's MAC address
        :raise VMAddressVerificationError: If the MAC-IP address mapping cannot
                be verified (using arping)
        """
        nic = self.virtnet[index]
        # TODO: Determine port redirection in use w/o checking nettype
        if nic.nettype not in ['bridge', 'macvtap']:
            hostname = socket.gethostname()
            return socket.gethostbyname(hostname)
        if not nic.has_key('mac') and self.params.get('vm_type') == 'libvirt':
            # Look it up from xml
            nic.mac = self.get_virsh_mac_address(index)
        # else TODO: Look up mac from existing qemu-kvm process
        if not nic.has_key('mac'):
            raise VMMACAddressMissingError(index)

        # Get the IP address from arp cache, try upper and lower case
        arp_ip = self.address_cache.get(nic.mac.upper())
        if not arp_ip:
            arp_ip = self.address_cache.get(nic.mac.lower())

        if not arp_ip and os.geteuid() != 0:
            # For non-root, tcpdump won't work for finding IP address, try arp
            ip_map = utils_net.parse_arp()
            arp_ip = ip_map.get(nic.mac.lower())
            if arp_ip:
                self.address_cache[nic.mac.lower()] = arp_ip

        if not arp_ip:
            raise VMIPAddressMissingError(nic.mac)

        # Make sure the IP address is assigned to one or more macs
        # for this guest
        macs = self.virtnet.mac_list()

        # SR-IOV card may not in same subnet with the card used by host by
        # default. So arp check cannot work.
        # At this situation verify_ip_address_ownership will raise
        # VMAddressVerificationError even IP and mac match.
        # So do not raise VMAddressVerificationError when SR-IOV used.
        nic_params = self.params.object_params(nic.nic_name)
        ignore_ip_address_verify = nic_params.get("pci_assignable") != "no"

        if not utils_net.verify_ip_address_ownership(arp_ip, macs):
            if ignore_ip_address_verify:
                msg = "Could not verify DHCP lease: %s --> %s." % (nic.mac,
                                                                   arp_ip)
                msg += " Maybe %s is not in same subnet with host." % arp_ip
                logging.error(msg)
            else:
                raise VMAddressVerificationError(nic.mac, arp_ip)
        logging.debug('Found/Verified IP %s for VM %s NIC %s' % (
            arp_ip, self.name, str(index)))
        return arp_ip
Exemple #6
0
    def get_address(self, index=0):
        """
        Return the IP address of a NIC or guest (in host space).

        :param index: Name or index of the NIC whose address is requested.
        :return: 'localhost': Port redirection is in use
        :return: IP address of NIC if valid in arp cache.
        :raise VMMACAddressMissingError: If no MAC address is defined for the
                requested NIC
        :raise VMIPAddressMissingError: If no IP address is found for the the
                NIC's MAC address
        :raise VMAddressVerificationError: If the MAC-IP address mapping cannot
                be verified (using arping)
        """
        nic = self.virtnet[index]
        # TODO: Determine port redirection in use w/o checking nettype
        if nic.nettype not in ['bridge', 'macvtap']:
            hostname = socket.gethostname()
            return socket.gethostbyname(hostname)
        if not nic.has_key('mac') and self.params.get('vm_type') == 'libvirt':
            # Look it up from xml
            nic.mac = self.get_virsh_mac_address(index)
        # else TODO: Look up mac from existing qemu-kvm process
        if not nic.has_key('mac'):
            raise VMMACAddressMissingError(index)

        # Get the IP address from arp cache, try upper and lower case
        arp_ip = self.address_cache.get(nic.mac.upper())
        if not arp_ip:
            arp_ip = self.address_cache.get(nic.mac.lower())

        if not arp_ip and os.geteuid() != 0:
            # For non-root, tcpdump won't work for finding IP address, try arp
            ip_map = utils_net.parse_arp()
            arp_ip = ip_map.get(nic.mac.lower())
            if arp_ip:
                self.address_cache[nic.mac.lower()] = arp_ip

        if not arp_ip:
            raise VMIPAddressMissingError(nic.mac)

        # Make sure the IP address is assigned to one or more macs
        # for this guest
        macs = self.virtnet.mac_list()

        # SR-IOV card may not in same subnet with the card used by host by
        # default. So arp check cannot work.
        # At this situation verify_ip_address_ownership will raise
        # VMAddressVerificationError even IP and mac match.
        # So do not raise VMAddressVerificationError when SR-IOV used.
        nic_params = self.params.object_params(nic.nic_name)
        ignore_ip_address_verify = nic_params.get("pci_assignable") != "no"

        if not utils_net.verify_ip_address_ownership(arp_ip, macs):
            if ignore_ip_address_verify:
                msg = "Could not verify DHCP lease: %s --> %s." % (nic.mac,
                                                                   arp_ip)
                msg += " Maybe %s is not in same subnet with host." % arp_ip
                logging.error(msg)
            else:
                raise VMAddressVerificationError(nic.mac, arp_ip)
        logging.debug('Found/Verified IP %s for VM %s NIC %s' % (
            arp_ip, self.name, str(index)))
        return arp_ip