iSCSIUtils.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. //
  2. // Copyright @ 2014 Hangzhou Topzen Ltd.
  3. // Author: Tang (tang@hztopzen.com) @ 2014-12
  4. //
  5. #ifndef __ISCSIUTILS_H
  6. #define __ISCSIUTILS_H
  7. #include "Types.h"
  8. #if defined(WIN32) || defined(WINDOWS)
  9. #include "iscsierr.h"
  10. #include "iscsidsc.h"
  11. #endif
  12. namespace tzc {
  13. #if defined(WIN32) || defined(WINDOWS) // Windows
  14. typedef HRESULT (__stdcall * FTAddTargetPortal)
  15. (PCHAR, ULONG, PISCSI_LOGIN_OPTIONS, ISCSI_SECURITY_FLAGS, PISCSI_TARGET_PORTALA);
  16. typedef HRESULT (__stdcall * FTGetSessionList)
  17. (PULONG, PULONG, PISCSI_SESSION_INFOA);
  18. typedef HRESULT (__stdcall * FTLoginTarget)
  19. (PCHAR, BOOLEAN, PCHAR, ULONG, PISCSI_TARGET_PORTALA, ISCSI_SECURITY_FLAGS,
  20. PISCSI_TARGET_MAPPINGA, PISCSI_LOGIN_OPTIONS, ULONG, PCHAR, BOOLEAN,
  21. PISCSI_UNIQUE_SESSION_ID, PISCSI_UNIQUE_CONNECTION_ID);
  22. typedef HRESULT (__stdcall * FTReportTargetPortals)
  23. (PCHAR, PCHAR, PUSHORT, PULONG, PISCSI_TARGET_PORTALA);
  24. typedef HRESULT (__stdcall * FTReportTargets)
  25. (BOOLEAN, PULONG, PCHAR);
  26. typedef HRESULT (__stdcall * FTLogoutITarget)
  27. (PISCSI_UNIQUE_SESSION_ID);
  28. typedef HRESULT (__stdcall * FTRemoveSendTargetPortal)
  29. (PCHAR, ULONG, PISCSI_TARGET_PORTALA);
  30. typedef HRESULT (__stdcall * FTReportSendTargetPortals)
  31. (PULONG, PISCSI_TARGET_PORTAL_INFOA);
  32. typedef HRESULT (__stdcall * FTRefreshSendTargetPortals)
  33. (PCHAR, ULONG, PISCSI_TARGET_PORTALA);
  34. typedef HRESULT (__stdcall * FTGetSessionDeviceList)
  35. (PISCSI_UNIQUE_SESSION_ID, PULONG, PISCSI_DEVICE_ON_SESSIONA);
  36. typedef HRESULT (__stdcall * FTGetLocalInitiatorIQN)(PCHAR);
  37. typedef HRESULT (__stdcall * FTSetLocalInitiatorIQN)(PCHAR);
  38. #endif // endif of Windows
  39. #define DEFAULT_ISCSI_TARGET_PORT (3260)
  40. #define MAX_TARGET_NUM_PER_IPSAN (128)
  41. #define MAX_DEVICE_NUM_PER_IPSAN (256)
  42. #define MAX_PORTAL_NUM_PER_IPSAN (256)
  43. #define MAX_PORTAL_NUM_PER_TARGET (16)
  44. #define MAX_SESSION_NUM_PER_HOST (256)
  45. #define MAX_TARGET_IQN_LEN (256)
  46. #define MAX_IPADDR_LEN (32)
  47. #define MAX_PATH_LEN (128)
  48. #define MAX_MALLOC_SIZE (65536)
  49. enum __iSCSI_RTS_CODE {
  50. iSCSI_RTS_SUCCEED = 0x00,
  51. iSCSI_RTS_FAILED,
  52. iSCSI_RTS_SESSION_LOGON,
  53. iSCSI_RTS_SESSION_LOGOFF,
  54. iSCSI_RTS_INVALID_PARAM,
  55. iSCSI_RTS_LOAD_DLL_FAILED = 0x05,
  56. iSCSI_RTS_SYSCALL_FAILED,
  57. iSCSI_RTS_REPORT_TARGET_FAILED,
  58. iSCSI_RTS_REPORT_PORTAL_FAILED,
  59. iSCSI_RTS_ADD_PORTAL_FAILED,
  60. iSCSI_RTS_SET_LOCALIQN_FAILED = 0x0A,
  61. iSCSI_RTS_GET_LOCALIQN_FAILED,
  62. iSCSI_RTS_LOGIN_PORTAL_FAILED,
  63. iSCSI_RTS_LOGOUT_PORTAL_FAILED,
  64. iSCSI_RTS_REFRESH_STPORTAL_FAILED,
  65. iSCSI_RTS_REPORT_STPORTAL_FAILED = 0x0F,
  66. iSCSI_RTS_REMOVE_STPORTAL_FAILED,
  67. iSCSI_RTS_DAEMON_RUNNING,
  68. iSCSI_RTS_DAEMON_DOWN,
  69. iSCSI_RTS_DAEMON_DEAD,
  70. iSCSI_RTS_GET_DEVICE_FAILED,
  71. iSCSI_RTS_DEVICE_INEXISTED,
  72. iSCSI_RTS_WAITING_ATTACH_DEVICE,
  73. iSCSI_RTS_END
  74. };
  75. enum __iSCSI_CMD_ON_LINUX { // Only valid on Linux
  76. iSCSI_CMD_BEGIN = 0x55,
  77. iSCSI_CMD_GET_PORTALS,
  78. iSCSI_CMD_DISCOVERY,
  79. iSCSI_CMD_LOGIN_PORTAL,
  80. iSCSI_CMD_LOGIN_ALL,
  81. iSCSI_CMD_LOGOUT_PORTAL,
  82. iSCSI_CMD_LOGOUT_ALL,
  83. iSCSI_CMD_RESCAN_SESSION,
  84. iSCSI_CMD_RESCAN_ALL,
  85. iSCSI_CMD_GET_SESSIONS,
  86. iSCSI_CMD_GET_SESSION_STATUS,
  87. iSCSI_CMD_GET_SESSION_DEVICES,
  88. iSCSI_CMD_START_DAEMON,
  89. iSCSI_CMD_STOP_DAEMON,
  90. iSCSI_CMD_WATCH_DAEMON,
  91. iSCSI_CMD_REMOVE_PORTAL,
  92. iSCSI_CMD_END
  93. };
  94. #define FAKE_CMD_FOR_WINDOWS iSCSI_CMD_END
  95. enum __IPSAN_TYPE {
  96. IPSAN_CLASS_1000 = 0x10, // IMS Class
  97. IPSAN_CLASS_2000 = 0x20, // DotHill Class
  98. IPSAN_CLASS_3000 = 0x30, // MicroSAN Class
  99. IPSAN_CLASS_3100 = 0x40, // SkySAN Class
  100. IPSAN_TYPE_3000L = 0x41,
  101. IPSAN_TYPE_3000L2 = 0x42,
  102. IPSAN_TYPE_3000S = 0x43,
  103. IPSAN_TYPE_3000S2 = 0x44,
  104. IPSAN_TYPE_UNKNOW = 0xFF,
  105. };
  106. #define IPSAN_CLASS_MASK (0xF0)
  107. typedef TZ_Uint32 IPSAN_TYPE;
  108. typedef TZ_Uint32 iSCSI_RTS;
  109. struct SCSIDEVICE
  110. {
  111. SCSIDEVICE():
  112. iHost(0), iChannel(0), iTarget(0), iLun(0)
  113. {
  114. memset(szPath, 0x00, MAX_PATH_LEN);
  115. }
  116. TZ_UCHAR iHost;
  117. TZ_UCHAR iChannel;
  118. TZ_UCHAR iTarget;
  119. TZ_UCHAR iLun;
  120. TZ_CHAR szPath[MAX_PATH_LEN];
  121. };
  122. struct ISCSIDISK
  123. {
  124. ISCSIDISK()
  125. {
  126. memset(szTargetIQN, 0x00, MAX_TARGET_IQN_LEN);
  127. }
  128. SCSIDEVICE stDevice;
  129. TZ_CHAR szTargetIQN[MAX_TARGET_IQN_LEN];
  130. };
  131. struct ISCSIPortal
  132. {
  133. ISCSIPortal(): iPort(DEFAULT_ISCSI_TARGET_PORT)
  134. {
  135. memset(szTargetIQN, 0x00, MAX_TARGET_IQN_LEN);
  136. memset(szIPAddr, 0x00, MAX_IPADDR_LEN);
  137. }
  138. TZ_CHAR szTargetIQN[MAX_TARGET_IQN_LEN];
  139. TZ_CHAR szIPAddr[MAX_IPADDR_LEN];
  140. TZ_USHORT iPort;
  141. };
  142. class DECLDLL CiSCSIUtils
  143. {
  144. public:
  145. CiSCSIUtils(IPSAN_TYPE iType = IPSAN_TYPE_3000L);
  146. ~CiSCSIUtils(void);
  147. public:
  148. iSCSI_RTS Initialize();
  149. iSCSI_RTS iSCSIDiscoveryIPSAN(IN TZ_CHAR * szIPAddr,
  150. OUT std::vector<ISCSIPortal> & vPortalList);
  151. iSCSI_RTS iSCSILoginAll();
  152. iSCSI_RTS iSCSILoginIPSAN(IN TZ_CHAR * szIPAddr,
  153. OUT std::vector<ISCSIDISK> & viSCSIDiskList);
  154. iSCSI_RTS iSCSILoginIPSANTarget(IN TZ_CHAR * szIPAddr,
  155. IN TZ_CHAR * szTargetIQN, OUT std::vector<SCSIDEVICE> & vDeviceList);
  156. iSCSI_RTS iSCSILoginIPSANDevice(IN TZ_CHAR * szIPAddr,
  157. IN TZ_CHAR * szTargetIQN, IN TZ_UCHAR iLun, OUT SCSIDEVICE & stDevice);
  158. // You should login the IPSAN first when you wan to call these two functions
  159. iSCSI_RTS iSCSIGetAttachedDeviceByLun(IN TZ_CHAR * szTargetIQN,
  160. IN TZ_UCHAR iLun, OUT SCSIDEVICE & stDevice);
  161. iSCSI_RTS iSCSIGetSessionDeviceCount(IN TZ_CHAR * szTargetIQN,
  162. OUT TZ_UINT & uiCount);
  163. iSCSI_RTS iSCSILogoutALL();
  164. iSCSI_RTS iSCSILogoutIPSAN(IN TZ_CHAR * szIPAddr);
  165. iSCSI_RTS iSCSILogoutIPSANTarget(IN OPTIONAL TZ_CHAR * szIPAddr,
  166. IN TZ_CHAR * szTargetIQN);
  167. iSCSI_RTS iSCSIRemoveTargetPortal(IN TZ_CHAR * szIPAddr, IN TZ_CHAR * szTargetIQN);
  168. iSCSI_RTS iSCSIGetSessionStatus(IN TZ_CHAR * szTargetIQN);
  169. iSCSI_RTS iSCSIStartDaemon();
  170. iSCSI_RTS iSCSIStopDaemon();
  171. iSCSI_RTS iSCSIRestartDaemon();
  172. iSCSI_RTS SetInitiatorIQN(IN TZ_CHAR * szInitiatorIQN);
  173. iSCSI_RTS GetInitiatorIQN(OUT TZ_CHAR * szInitiatorIQN);
  174. protected:
  175. iSCSI_RTS reportiSCSITargetList(IN TZ_CHAR * szIPAddr,
  176. OUT std::vector<ISCSIPortal> & vPortalList);
  177. iSCSI_RTS addiSCSITargetPortal(IN TZ_CHAR * szIPAddr,
  178. IN TZ_CHAR * szUserName, IN TZ_CHAR * szPassword,
  179. OUT std::vector<ISCSIPortal> & vPortalList);
  180. #if defined(WIN32) || defined(WINDOWS)
  181. iSCSI_RTS getSessionStatus(IN TZ_CHAR * szTargetIQN,
  182. OUT PISCSI_UNIQUE_SESSION_ID stSessionID);
  183. #elif defined(LINUX)
  184. iSCSI_RTS getSessionStatus(IN TZ_CHAR * szTargetIQN, OUT TZ_UINT & iSessionID);
  185. #endif
  186. #if defined(WIN32) || defined(WINDOWS)
  187. iSCSI_RTS getSessionDevices(IN PISCSI_UNIQUE_SESSION_ID pSessionID,
  188. OUT std::vector<SCSIDEVICE> & vDeviceList);
  189. #elif defined(LINUX)
  190. iSCSI_RTS getSessionDevices(IN const TZ_UINT iSessionID,
  191. OUT std::vector<SCSIDEVICE> & vDeviceList);
  192. #endif
  193. iSCSI_RTS loginTargetPortal(IN ISCSIPortal & stPortal,
  194. OUT std::vector<SCSIDEVICE> & vDeviceList);
  195. iSCSI_RTS logoutTargetPortal(IN ISCSIPortal & stPortal);
  196. iSCSI_RTS refreshSendTargetPortals(IN TZ_CHAR * szIPAddr);
  197. iSCSI_RTS refreshOSDeviceList(TZ_UINT iLinuxSID = INVALID_VALUE32);
  198. TZ_BOOL validCommand(const TZ_UINT iCmnd);
  199. TZ_BOOL portalExisted(IN TZ_CHAR * szIPAddr, IN TZ_CHAR * szTargetIQN);
  200. iSCSI_RTS delaySeconds(const TZ_UINT iCount);
  201. iSCSI_RTS generateFileName(std::string & sFileName);
  202. iSCSI_RTS removeFile(const std::string & sFileName);
  203. iSCSI_RTS getDaemonStatus();
  204. iSCSI_RTS executeCommand(IN const TZ_UINT iCmnd, IN const std::string & sParam,
  205. OUT std::string & sRstFilePath);
  206. private:
  207. IPSAN_TYPE m_iIPSANType;
  208. #if defined(WIN32) || defined(WINDOWS)
  209. HMODULE m_dllMethodHandle;
  210. FTAddTargetPortal m_fAddTargetPortal;
  211. FTGetSessionList m_fGetSessionList;
  212. FTLoginTarget m_fLoginTarget;
  213. FTLogoutITarget m_fLogoutITarget;
  214. FTReportTargets m_fReportTargets;
  215. FTReportTargetPortals m_fReportTargetPortals;
  216. FTRemoveSendTargetPortal m_fRemoveSendTargetPortal;
  217. FTReportSendTargetPortals m_fReportSendTargetPortals;
  218. FTRefreshSendTargetPortals m_fRefreshSendTargetPortals;
  219. FTGetSessionDeviceList m_fGetSessionDeviceList;
  220. FTGetLocalInitiatorIQN m_fGetLocalInitiatorIQN;
  221. FTSetLocalInitiatorIQN m_fSetLocalInitiatorIQN;
  222. #elif defined(LINUX)
  223. static std::map<TZ_UINT, std::string> m_mapCommandTable;
  224. static std::string m_sDefaultParam;
  225. #endif
  226. };
  227. } // namespace tzc
  228. #endif /* ----- #ifndef __ISCSIUTILS_H ----- */