Skip to content

Commit

Permalink
Merge pull request #108 from prateekpandey14/handle-connection-state
Browse files Browse the repository at this point in the history
fix(state): reset the CurrentHostIP on closed iscsi connection
  • Loading branch information
carmark authored Aug 17, 2021
2 parents 9fe48c7 + 22c2b95 commit e5d5366
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions pkg/port/iscsit/iscsid.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const (
var (
EnableStats bool
CurrentHostIP string
IPMutex sync.Mutex
)

type ISCSITargetDriver struct {
Expand Down Expand Up @@ -219,9 +220,11 @@ func (s *ISCSITargetDriver) Run() error {

remoteIP := strings.Split(conn.RemoteAddr().String(), ":")[0]

IPMutex.Lock()
if CurrentHostIP == "" {
CurrentHostIP = remoteIP
}
IPMutex.Unlock()

if s.blockMultipleHostLogin && remoteIP != CurrentHostIP {
conn.Close()
Expand All @@ -230,7 +233,7 @@ func (s *ISCSITargetDriver) Run() error {
continue
}

log.Info(conn.LocalAddr().String())
log.Info("connection establishing at: ", conn.LocalAddr().String())
s.setClientStatus(true)

iscsiConn := &iscsiConnection{conn: conn,
Expand Down Expand Up @@ -284,7 +287,19 @@ func (s *ISCSITargetDriver) handler(events byte, conn *iscsiConnection) {

if events&DATAIN != 0 {
log.Debug("rx handler processing...")
go s.rxHandler(conn)
go func() {
s.rxHandler(conn)
if conn.state == CONN_STATE_CLOSE {
log.Warningf("iscsi connection[%d] closed", conn.cid)
conn.close()
IPMutex.Lock()
remoteIP := strings.Split(conn.conn.RemoteAddr().String(), ":")[0]
if CurrentHostIP == remoteIP {
CurrentHostIP = ""
}
IPMutex.Unlock()
}
}()
}
if conn.state != CONN_STATE_CLOSE && events&DATAOUT != 0 {
log.Debug("tx handler processing...")
Expand All @@ -293,7 +308,12 @@ func (s *ISCSITargetDriver) handler(events byte, conn *iscsiConnection) {
if conn.state == CONN_STATE_CLOSE {
log.Warningf("iscsi connection[%d] closed", conn.cid)
conn.close()
CurrentHostIP = ""
IPMutex.Lock()
remoteIP := strings.Split(conn.conn.RemoteAddr().String(), ":")[0]
if CurrentHostIP == remoteIP {
CurrentHostIP = ""
}
IPMutex.Unlock()
}
}

Expand All @@ -319,7 +339,8 @@ func (s *ISCSITargetDriver) rxHandler(conn *iscsiConnection) {
log.Debug("rx handler: IOSTATE_RX_BHS")
length, err = conn.readData(buf)
if err != nil {
log.Error(err)
log.Error("read BHS failed:", err)
conn.state = CONN_STATE_CLOSE
return
}
if length == 0 {
Expand Down Expand Up @@ -363,7 +384,8 @@ func (s *ISCSITargetDriver) rxHandler(conn *iscsiConnection) {
for length < dl {
l, err := conn.readData(cmd.RawData[length:])
if err != nil {
log.Error(err)
log.Error("read data failed:", err)
conn.state = CONN_STATE_CLOSE
return
}
length += l
Expand Down Expand Up @@ -478,7 +500,12 @@ func iscsiExecLogout(conn *iscsiConnection) error {
conn.resp.ExpCmdSN = conn.session.ExpCmdSN
conn.resp.MaxCmdSN = conn.session.ExpCmdSN + conn.session.MaxQueueCommand
}
CurrentHostIP = ""
IPMutex.Lock()
remoteIP := strings.Split(conn.conn.RemoteAddr().String(), ":")[0]
if CurrentHostIP == remoteIP {
CurrentHostIP = ""
}
IPMutex.Unlock()
return nil
}

Expand Down

0 comments on commit e5d5366

Please sign in to comment.