SessionManager.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Copyright (C) 2012-2016 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Threading;
  10. using Utilities;
  11. namespace ISCSI.Server
  12. {
  13. internal class SessionManager
  14. {
  15. private object m_nextTSIHLock = new object();
  16. private ushort m_nextTSIH = 1; // Next Target Session Identifying Handle
  17. private List<ISCSISession> m_activeSessions = new List<ISCSISession>();
  18. public ISCSISession StartSession(string initiatorName, ulong isid)
  19. {
  20. ushort tsih = GetNextTSIH();
  21. ISCSISession session = new ISCSISession(initiatorName, isid, tsih);
  22. lock (m_activeSessions)
  23. {
  24. m_activeSessions.Add(session);
  25. }
  26. return session;
  27. }
  28. public ISCSISession FindSession(string initiatorName, ulong isid, ushort tsih)
  29. {
  30. lock (m_activeSessions)
  31. {
  32. int index = GetSessionIndex(initiatorName, isid, tsih);
  33. if (index >= 0)
  34. {
  35. return m_activeSessions[index];
  36. }
  37. }
  38. return null;
  39. }
  40. public ISCSISession FindSession(string initiatorName, ulong isid, string targetName)
  41. {
  42. lock (m_activeSessions)
  43. {
  44. for (int index = 0; index < m_activeSessions.Count; index++)
  45. {
  46. ISCSISession session = m_activeSessions[index];
  47. if (String.Equals(session.InitiatorName, initiatorName, StringComparison.OrdinalIgnoreCase) &&
  48. session.ISID == isid &&
  49. session.Target != null &&
  50. String.Equals(session.Target.TargetName, targetName, StringComparison.OrdinalIgnoreCase))
  51. {
  52. return session;
  53. }
  54. }
  55. }
  56. return null;
  57. }
  58. public List<ISCSISession> FindTargetSessions(string targetName)
  59. {
  60. lock (m_activeSessions)
  61. {
  62. List<ISCSISession> result = new List<ISCSISession>();
  63. foreach (ISCSISession session in m_activeSessions)
  64. {
  65. if (session.Target != null)
  66. {
  67. if (String.Equals(session.Target.TargetName, targetName, StringComparison.OrdinalIgnoreCase))
  68. {
  69. result.Add(session);
  70. }
  71. }
  72. }
  73. return result;
  74. }
  75. }
  76. public void RemoveSession(ISCSISession session, SessionTerminationReason reason)
  77. {
  78. lock (m_activeSessions)
  79. {
  80. int index = GetSessionIndex(session.InitiatorName, session.ISID, session.TSIH);
  81. if (index >= 0)
  82. {
  83. ISCSITarget target = m_activeSessions[index].Target;
  84. if (target != null)
  85. {
  86. target.NotifySessionTermination(session.InitiatorName, session.ISID, reason);
  87. }
  88. m_activeSessions.RemoveAt(index);
  89. }
  90. }
  91. }
  92. public bool IsTargetInUse(string targetName)
  93. {
  94. lock (m_activeSessions)
  95. {
  96. foreach (ISCSISession session in m_activeSessions)
  97. {
  98. if (session.Target != null)
  99. {
  100. if (String.Equals(session.Target.TargetName, targetName, StringComparison.OrdinalIgnoreCase))
  101. {
  102. return true;
  103. }
  104. }
  105. }
  106. }
  107. return false;
  108. }
  109. private int GetSessionIndex(string initiatorName, ulong isid, ushort tsih)
  110. {
  111. for (int index = 0; index < m_activeSessions.Count; index++)
  112. {
  113. if (String.Equals(initiatorName, m_activeSessions[index].InitiatorName, StringComparison.OrdinalIgnoreCase) &&
  114. m_activeSessions[index].ISID == isid &&
  115. m_activeSessions[index].TSIH == tsih)
  116. {
  117. return index;
  118. }
  119. }
  120. return -1;
  121. }
  122. private ushort GetNextTSIH()
  123. {
  124. // The iSCSI Target selects a non-zero value for the TSIH at
  125. // session creation (when an initiator presents a 0 value at Login).
  126. // After being selected, the same TSIH value MUST be used whenever the
  127. // initiator or target refers to the session and a TSIH is required.
  128. lock (m_nextTSIHLock)
  129. {
  130. ushort nextTSIH = m_nextTSIH;
  131. m_nextTSIH++;
  132. if (m_nextTSIH == 0)
  133. {
  134. m_nextTSIH++;
  135. }
  136. return nextTSIH;
  137. }
  138. }
  139. }
  140. }