Example #1
0
    def _get_fc_hba_adapter_ports(self, adapter_name):
        hba_ports = []
        with self._get_hba_handle(adapter_name=adapter_name) as hba_handle:
            adapter_attributes = self._get_adapter_attributes(hba_handle)
            port_count = adapter_attributes.NumberOfPorts

            for port_index in range(port_count):
                port_attr = self._get_adapter_port_attributes(
                    hba_handle, port_index)
                wwnn = _utils.byte_array_to_hex_str(port_attr.NodeWWN.wwn)
                wwpn = _utils.byte_array_to_hex_str(port_attr.PortWWN.wwn)

                hba_port_info = dict(node_name=wwnn, port_name=wwpn)
                hba_ports.append(hba_port_info)
        return hba_ports
Example #2
0
    def test_byte_array_to_hex_str(self):
        fake_byte_array = bytearray(range(3))

        resulted_string = _utils.byte_array_to_hex_str(fake_byte_array)
        expected_string = '000102'

        self.assertEqual(expected_string, resulted_string)
Example #3
0
    def _send_scsi_inquiry_v2(self, hba_handle, port_wwn_struct,
                              remote_port_wwn_struct, fcp_lun, cdb_byte1,
                              cdb_byte2):
        port_wwn = _utils.byte_array_to_hex_str(port_wwn_struct.wwn)
        remote_port_wwn = _utils.byte_array_to_hex_str(
            remote_port_wwn_struct.wwn)

        LOG.debug(
            "Sending SCSI INQUIRY to WWPN %(remote_port_wwn)s, "
            "FCP LUN %(fcp_lun)s from WWPN %(port_wwn)s. "
            "CDB byte 1 %(cdb_byte1)s, CDB byte 2: %(cdb_byte2)s.",
            dict(port_wwn=port_wwn,
                 remote_port_wwn=remote_port_wwn,
                 fcp_lun=fcp_lun,
                 cdb_byte1=hex(cdb_byte1),
                 cdb_byte2=hex(cdb_byte2)))

        resp_buffer_sz = ctypes.c_uint32(SCSI_INQ_BUFF_SZ)
        resp_buffer = (ctypes.c_ubyte * resp_buffer_sz.value)()

        sense_buffer_sz = ctypes.c_uint32(SENSE_BUFF_SZ)
        sense_buffer = (ctypes.c_ubyte * sense_buffer_sz.value)()

        scsi_status = ctypes.c_ubyte()

        try:
            self._run_and_check_output(hbaapi.HBA_ScsiInquiryV2, hba_handle,
                                       port_wwn_struct, remote_port_wwn_struct,
                                       ctypes.c_uint64(fcp_lun),
                                       ctypes.c_uint8(cdb_byte1),
                                       ctypes.c_uint8(cdb_byte2),
                                       ctypes.byref(resp_buffer),
                                       ctypes.byref(resp_buffer_sz),
                                       ctypes.byref(scsi_status),
                                       ctypes.byref(sense_buffer),
                                       ctypes.byref(sense_buffer_sz))
        finally:
            sense_data = _utils.byte_array_to_hex_str(
                sense_buffer[:sense_buffer_sz.value])
            LOG.debug(
                "SCSI inquiry returned sense data: %(sense_data)s. "
                "SCSI status: %(scsi_status)s.",
                dict(sense_data=sense_data, scsi_status=scsi_status.value))

        return resp_buffer
Example #4
0
    def get_fc_target_mappings(self, node_wwn):
        """Retrieve FCP target mappings.

        :param node_wwn: a HBA node WWN represented as a hex string.
        :returns: a list of FCP mappings represented as dicts.
        """
        mappings = []
        node_wwn_struct = self._wwn_struct_from_hex_str(node_wwn)

        with self._get_hba_handle(
                adapter_wwn_struct=node_wwn_struct) as hba_handle:
            fcp_mappings = self._get_target_mapping(hba_handle)
            for entry in fcp_mappings.Entries:
                wwnn = _utils.byte_array_to_hex_str(entry.FcpId.NodeWWN.wwn)
                wwpn = _utils.byte_array_to_hex_str(entry.FcpId.PortWWN.wwn)
                mapping = dict(node_name=wwnn,
                               port_name=wwpn,
                               device_name=entry.ScsiId.OSDeviceName,
                               lun=entry.ScsiId.ScsiOSLun,
                               fcp_lun=entry.FcpId.FcpLun)
                mappings.append(mapping)
        return mappings
Example #5
0
    def test_get_fc_target_mapping(self, mock_get_target_mapping,
                                   mock_close_adapter, mock_open_adapter,
                                   mock_wwn_struct_from_hex_str):
        # Remote WWNs
        fake_node_wwn = list(range(8))
        fake_port_wwn = list(range(8)[::-1])

        mock_fcp_mappings = mock.MagicMock()
        mock_entry = mock.MagicMock()
        mock_entry.FcpId.NodeWWN.wwn = fake_node_wwn
        mock_entry.FcpId.PortWWN.wwn = fake_port_wwn
        mock_fcp_mappings.Entries = [mock_entry]
        mock_get_target_mapping.return_value = mock_fcp_mappings

        resulted_mappings = self._fc_utils.get_fc_target_mappings(
            mock.sentinel.local_wwnn)

        expected_mappings = [{
            'node_name':
            _utils.byte_array_to_hex_str(fake_node_wwn),
            'port_name':
            _utils.byte_array_to_hex_str(fake_port_wwn),
            'device_name':
            mock_entry.ScsiId.OSDeviceName,
            'lun':
            mock_entry.ScsiId.ScsiOSLun,
            'fcp_lun':
            mock_entry.FcpId.FcpLun
        }]
        self.assertEqual(expected_mappings, resulted_mappings)

        mock_wwn_struct_from_hex_str.assert_called_once_with(
            mock.sentinel.local_wwnn)
        mock_open_adapter.assert_called_once_with(
            mock_wwn_struct_from_hex_str.return_value)

        mock_close_adapter.assert_called_once_with(
            mock_open_adapter.return_value)
Example #6
0
    def test_get_fc_hba_adapter_ports(self, mock_get_adapter_attributes,
                                      mock_get_adapter_port_attributes,
                                      mock_close_adapter, mock_open_adapter):
        fake_port_count = 1
        fake_port_index = 0
        # Local WWNs
        fake_node_wwn = list(range(3))
        fake_port_wwn = list(range(3))

        mock_adapter_attributes = mock.MagicMock()
        mock_adapter_attributes.NumberOfPorts = fake_port_count
        mock_port_attributes = mock.MagicMock()
        mock_port_attributes.NodeWWN.wwn = fake_node_wwn
        mock_port_attributes.PortWWN.wwn = fake_port_wwn

        mock_get_adapter_attributes.return_value = mock_adapter_attributes
        mock_get_adapter_port_attributes.return_value = mock_port_attributes

        resulted_hba_ports = self._fc_utils._get_fc_hba_adapter_ports(
            mock.sentinel.adapter_name)

        expected_hba_ports = [{
            'node_name':
            _utils.byte_array_to_hex_str(fake_node_wwn),
            'port_name':
            _utils.byte_array_to_hex_str(fake_port_wwn)
        }]
        self.assertEqual(expected_hba_ports, resulted_hba_ports)

        mock_open_adapter.assert_called_once_with(mock.sentinel.adapter_name)
        mock_close_adapter.assert_called_once_with(
            mock_open_adapter(mock.sentinel.adapter_nam))
        mock_get_adapter_attributes.assert_called_once_with(
            mock_open_adapter.return_value)
        mock_get_adapter_port_attributes.assert_called_once_with(
            mock_open_adapter.return_value, fake_port_index)
Example #7
0
    def _parse_scsi_id_desc(self, id_desc_addr, buff_sz):
        """Parse SCSI VPD identification descriptor."""
        id_desc_struct_sz = ctypes.sizeof(IDENTIFICATION_DESCRIPTOR)

        if buff_sz < id_desc_struct_sz:
            reason = _('Identifier descriptor overflow.')
            raise exceptions.SCSIIdDescriptorParsingError(reason=reason)

        id_desc = IDENTIFICATION_DESCRIPTOR.from_address(id_desc_addr)
        id_desc_sz = id_desc_struct_sz + id_desc.IdentifierLength
        identifier_addr = id_desc_addr + id_desc_struct_sz

        if id_desc_sz > buff_sz:
            reason = _('Identifier overflow.')
            raise exceptions.SCSIIdDescriptorParsingError(reason=reason)

        identifier = (ctypes.c_ubyte *
                      id_desc.IdentifierLength).from_address(identifier_addr)
        raw_id = bytearray(identifier)

        if id_desc.CodeSet == SCSI_ID_CODE_SET_ASCII:
            parsed_id = bytes(
                bytearray(identifier)).decode('ascii').strip('\x00')
        else:
            parsed_id = _utils.byte_array_to_hex_str(raw_id)

        id_dict = {
            'code_set': id_desc.CodeSet,
            'protocol': (id_desc.ProtocolIdentifier if id_desc.Piv else None),
            'type': id_desc.IdentifierType,
            'association': id_desc.Association,
            'raw_id': raw_id,
            'id': parsed_id,
            'raw_id_desc_size': id_desc_sz,
        }
        return id_dict
Example #8
0
    def test_get_wwn_struct_from_hex_str(self):
        wwn_b_array = list(range(8))
        wwn_str = _utils.byte_array_to_hex_str(wwn_b_array)

        wwn_struct = self._fc_utils._wwn_struct_from_hex_str(wwn_str)
        self.assertEqual(wwn_b_array, list(wwn_struct.wwn))