123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- //
- // Copyright @ 2014 Hangzhou Topzen Ltd.
- // Author: Tang (tang@hztopzen.com) @ 2014-12
- //
- #ifndef __ISCSIUTILS_H
- #define __ISCSIUTILS_H
- #include "Types.h"
- #if defined(WIN32) || defined(WINDOWS)
- #include "iscsierr.h"
- #include "iscsidsc.h"
- #endif
- namespace tzc {
- #if defined(WIN32) || defined(WINDOWS) // Windows
- typedef HRESULT (__stdcall * FTAddTargetPortal)
- (PCHAR, ULONG, PISCSI_LOGIN_OPTIONS, ISCSI_SECURITY_FLAGS, PISCSI_TARGET_PORTALA);
- typedef HRESULT (__stdcall * FTGetSessionList)
- (PULONG, PULONG, PISCSI_SESSION_INFOA);
- typedef HRESULT (__stdcall * FTLoginTarget)
- (PCHAR, BOOLEAN, PCHAR, ULONG, PISCSI_TARGET_PORTALA, ISCSI_SECURITY_FLAGS,
- PISCSI_TARGET_MAPPINGA, PISCSI_LOGIN_OPTIONS, ULONG, PCHAR, BOOLEAN,
- PISCSI_UNIQUE_SESSION_ID, PISCSI_UNIQUE_CONNECTION_ID);
- typedef HRESULT (__stdcall * FTReportTargetPortals)
- (PCHAR, PCHAR, PUSHORT, PULONG, PISCSI_TARGET_PORTALA);
- typedef HRESULT (__stdcall * FTReportTargets)
- (BOOLEAN, PULONG, PCHAR);
- typedef HRESULT (__stdcall * FTLogoutITarget)
- (PISCSI_UNIQUE_SESSION_ID);
- typedef HRESULT (__stdcall * FTRemoveSendTargetPortal)
- (PCHAR, ULONG, PISCSI_TARGET_PORTALA);
- typedef HRESULT (__stdcall * FTReportSendTargetPortals)
- (PULONG, PISCSI_TARGET_PORTAL_INFOA);
- typedef HRESULT (__stdcall * FTRefreshSendTargetPortals)
- (PCHAR, ULONG, PISCSI_TARGET_PORTALA);
- typedef HRESULT (__stdcall * FTGetSessionDeviceList)
- (PISCSI_UNIQUE_SESSION_ID, PULONG, PISCSI_DEVICE_ON_SESSIONA);
- typedef HRESULT (__stdcall * FTGetLocalInitiatorIQN)(PCHAR);
- typedef HRESULT (__stdcall * FTSetLocalInitiatorIQN)(PCHAR);
- #endif // endif of Windows
- #define DEFAULT_ISCSI_TARGET_PORT (3260)
- #define MAX_TARGET_NUM_PER_IPSAN (128)
- #define MAX_DEVICE_NUM_PER_IPSAN (256)
- #define MAX_PORTAL_NUM_PER_IPSAN (256)
- #define MAX_PORTAL_NUM_PER_TARGET (16)
- #define MAX_SESSION_NUM_PER_HOST (256)
- #define MAX_TARGET_IQN_LEN (256)
- #define MAX_IPADDR_LEN (32)
- #define MAX_PATH_LEN (128)
- #define MAX_MALLOC_SIZE (65536)
- enum __iSCSI_RTS_CODE {
- iSCSI_RTS_SUCCEED = 0x00,
- iSCSI_RTS_FAILED,
- iSCSI_RTS_SESSION_LOGON,
- iSCSI_RTS_SESSION_LOGOFF,
- iSCSI_RTS_INVALID_PARAM,
- iSCSI_RTS_LOAD_DLL_FAILED = 0x05,
- iSCSI_RTS_SYSCALL_FAILED,
- iSCSI_RTS_REPORT_TARGET_FAILED,
- iSCSI_RTS_REPORT_PORTAL_FAILED,
- iSCSI_RTS_ADD_PORTAL_FAILED,
-
- iSCSI_RTS_SET_LOCALIQN_FAILED = 0x0A,
- iSCSI_RTS_GET_LOCALIQN_FAILED,
- iSCSI_RTS_LOGIN_PORTAL_FAILED,
- iSCSI_RTS_LOGOUT_PORTAL_FAILED,
- iSCSI_RTS_REFRESH_STPORTAL_FAILED,
-
- iSCSI_RTS_REPORT_STPORTAL_FAILED = 0x0F,
- iSCSI_RTS_REMOVE_STPORTAL_FAILED,
- iSCSI_RTS_DAEMON_RUNNING,
- iSCSI_RTS_DAEMON_DOWN,
- iSCSI_RTS_DAEMON_DEAD,
-
- iSCSI_RTS_GET_DEVICE_FAILED,
- iSCSI_RTS_DEVICE_INEXISTED,
- iSCSI_RTS_WAITING_ATTACH_DEVICE,
-
- iSCSI_RTS_END
- };
- enum __iSCSI_CMD_ON_LINUX { // Only valid on Linux
- iSCSI_CMD_BEGIN = 0x55,
- iSCSI_CMD_GET_PORTALS,
- iSCSI_CMD_DISCOVERY,
- iSCSI_CMD_LOGIN_PORTAL,
- iSCSI_CMD_LOGIN_ALL,
- iSCSI_CMD_LOGOUT_PORTAL,
- iSCSI_CMD_LOGOUT_ALL,
- iSCSI_CMD_RESCAN_SESSION,
- iSCSI_CMD_RESCAN_ALL,
- iSCSI_CMD_GET_SESSIONS,
- iSCSI_CMD_GET_SESSION_STATUS,
- iSCSI_CMD_GET_SESSION_DEVICES,
- iSCSI_CMD_START_DAEMON,
- iSCSI_CMD_STOP_DAEMON,
- iSCSI_CMD_WATCH_DAEMON,
- iSCSI_CMD_REMOVE_PORTAL,
- iSCSI_CMD_END
- };
- #define FAKE_CMD_FOR_WINDOWS iSCSI_CMD_END
- enum __IPSAN_TYPE {
- IPSAN_CLASS_1000 = 0x10, // IMS Class
- IPSAN_CLASS_2000 = 0x20, // DotHill Class
- IPSAN_CLASS_3000 = 0x30, // MicroSAN Class
- IPSAN_CLASS_3100 = 0x40, // SkySAN Class
- IPSAN_TYPE_3000L = 0x41,
- IPSAN_TYPE_3000L2 = 0x42,
- IPSAN_TYPE_3000S = 0x43,
- IPSAN_TYPE_3000S2 = 0x44,
- IPSAN_TYPE_UNKNOW = 0xFF,
- };
- #define IPSAN_CLASS_MASK (0xF0)
- typedef TZ_Uint32 IPSAN_TYPE;
- typedef TZ_Uint32 iSCSI_RTS;
- struct SCSIDEVICE
- {
- SCSIDEVICE():
- iHost(0), iChannel(0), iTarget(0), iLun(0)
- {
- memset(szPath, 0x00, MAX_PATH_LEN);
- }
- TZ_UCHAR iHost;
- TZ_UCHAR iChannel;
- TZ_UCHAR iTarget;
- TZ_UCHAR iLun;
- TZ_CHAR szPath[MAX_PATH_LEN];
- };
- struct ISCSIDISK
- {
- ISCSIDISK()
- {
- memset(szTargetIQN, 0x00, MAX_TARGET_IQN_LEN);
- }
- SCSIDEVICE stDevice;
- TZ_CHAR szTargetIQN[MAX_TARGET_IQN_LEN];
- };
- struct ISCSIPortal
- {
- ISCSIPortal(): iPort(DEFAULT_ISCSI_TARGET_PORT)
- {
- memset(szTargetIQN, 0x00, MAX_TARGET_IQN_LEN);
- memset(szIPAddr, 0x00, MAX_IPADDR_LEN);
- }
- TZ_CHAR szTargetIQN[MAX_TARGET_IQN_LEN];
- TZ_CHAR szIPAddr[MAX_IPADDR_LEN];
- TZ_USHORT iPort;
- };
- class DECLDLL CiSCSIUtils
- {
- public:
- CiSCSIUtils(IPSAN_TYPE iType = IPSAN_TYPE_3000L);
- ~CiSCSIUtils(void);
- public:
- iSCSI_RTS Initialize();
- iSCSI_RTS iSCSIDiscoveryIPSAN(IN TZ_CHAR * szIPAddr,
- OUT std::vector<ISCSIPortal> & vPortalList);
- iSCSI_RTS iSCSILoginAll();
- iSCSI_RTS iSCSILoginIPSAN(IN TZ_CHAR * szIPAddr,
- OUT std::vector<ISCSIDISK> & viSCSIDiskList);
- iSCSI_RTS iSCSILoginIPSANTarget(IN TZ_CHAR * szIPAddr,
- IN TZ_CHAR * szTargetIQN, OUT std::vector<SCSIDEVICE> & vDeviceList);
- iSCSI_RTS iSCSILoginIPSANDevice(IN TZ_CHAR * szIPAddr,
- IN TZ_CHAR * szTargetIQN, IN TZ_UCHAR iLun, OUT SCSIDEVICE & stDevice);
- // You should login the IPSAN first when you wan to call these two functions
- iSCSI_RTS iSCSIGetAttachedDeviceByLun(IN TZ_CHAR * szTargetIQN,
- IN TZ_UCHAR iLun, OUT SCSIDEVICE & stDevice);
- iSCSI_RTS iSCSIGetSessionDeviceCount(IN TZ_CHAR * szTargetIQN,
- OUT TZ_UINT & uiCount);
- iSCSI_RTS iSCSILogoutALL();
- iSCSI_RTS iSCSILogoutIPSAN(IN TZ_CHAR * szIPAddr);
- iSCSI_RTS iSCSILogoutIPSANTarget(IN OPTIONAL TZ_CHAR * szIPAddr,
- IN TZ_CHAR * szTargetIQN);
- iSCSI_RTS iSCSIRemoveTargetPortal(IN TZ_CHAR * szIPAddr, IN TZ_CHAR * szTargetIQN);
- iSCSI_RTS iSCSIGetSessionStatus(IN TZ_CHAR * szTargetIQN);
- iSCSI_RTS iSCSIStartDaemon();
- iSCSI_RTS iSCSIStopDaemon();
- iSCSI_RTS iSCSIRestartDaemon();
- iSCSI_RTS SetInitiatorIQN(IN TZ_CHAR * szInitiatorIQN);
- iSCSI_RTS GetInitiatorIQN(OUT TZ_CHAR * szInitiatorIQN);
- protected:
- iSCSI_RTS reportiSCSITargetList(IN TZ_CHAR * szIPAddr,
- OUT std::vector<ISCSIPortal> & vPortalList);
- iSCSI_RTS addiSCSITargetPortal(IN TZ_CHAR * szIPAddr,
- IN TZ_CHAR * szUserName, IN TZ_CHAR * szPassword,
- OUT std::vector<ISCSIPortal> & vPortalList);
- #if defined(WIN32) || defined(WINDOWS)
- iSCSI_RTS getSessionStatus(IN TZ_CHAR * szTargetIQN,
- OUT PISCSI_UNIQUE_SESSION_ID stSessionID);
- #elif defined(LINUX)
- iSCSI_RTS getSessionStatus(IN TZ_CHAR * szTargetIQN, OUT TZ_UINT & iSessionID);
- #endif
- #if defined(WIN32) || defined(WINDOWS)
- iSCSI_RTS getSessionDevices(IN PISCSI_UNIQUE_SESSION_ID pSessionID,
- OUT std::vector<SCSIDEVICE> & vDeviceList);
- #elif defined(LINUX)
- iSCSI_RTS getSessionDevices(IN const TZ_UINT iSessionID,
- OUT std::vector<SCSIDEVICE> & vDeviceList);
- #endif
- iSCSI_RTS loginTargetPortal(IN ISCSIPortal & stPortal,
- OUT std::vector<SCSIDEVICE> & vDeviceList);
- iSCSI_RTS logoutTargetPortal(IN ISCSIPortal & stPortal);
- iSCSI_RTS refreshSendTargetPortals(IN TZ_CHAR * szIPAddr);
- iSCSI_RTS refreshOSDeviceList(TZ_UINT iLinuxSID = INVALID_VALUE32);
- TZ_BOOL validCommand(const TZ_UINT iCmnd);
- TZ_BOOL portalExisted(IN TZ_CHAR * szIPAddr, IN TZ_CHAR * szTargetIQN);
- iSCSI_RTS delaySeconds(const TZ_UINT iCount);
- iSCSI_RTS generateFileName(std::string & sFileName);
-
- iSCSI_RTS removeFile(const std::string & sFileName);
- iSCSI_RTS getDaemonStatus();
- iSCSI_RTS executeCommand(IN const TZ_UINT iCmnd, IN const std::string & sParam,
- OUT std::string & sRstFilePath);
- private:
- IPSAN_TYPE m_iIPSANType;
- #if defined(WIN32) || defined(WINDOWS)
- HMODULE m_dllMethodHandle;
- FTAddTargetPortal m_fAddTargetPortal;
- FTGetSessionList m_fGetSessionList;
- FTLoginTarget m_fLoginTarget;
- FTLogoutITarget m_fLogoutITarget;
- FTReportTargets m_fReportTargets;
- FTReportTargetPortals m_fReportTargetPortals;
- FTRemoveSendTargetPortal m_fRemoveSendTargetPortal;
- FTReportSendTargetPortals m_fReportSendTargetPortals;
- FTRefreshSendTargetPortals m_fRefreshSendTargetPortals;
- FTGetSessionDeviceList m_fGetSessionDeviceList;
- FTGetLocalInitiatorIQN m_fGetLocalInitiatorIQN;
- FTSetLocalInitiatorIQN m_fSetLocalInitiatorIQN;
- #elif defined(LINUX)
- static std::map<TZ_UINT, std::string> m_mapCommandTable;
- static std::string m_sDefaultParam;
- #endif
- };
- } // namespace tzc
- #endif /* ----- #ifndef __ISCSIUTILS_H ----- */
|