From 22c2b95d8f7880ab9a632a52846fc073d896e57d Mon Sep 17 00:00:00 2001 From: prateekpandey14 Date: Mon, 16 Aug 2021 20:54:04 +0530 Subject: [PATCH] fix(state): reset the CurrentHostIP on closed iscsi connection Signed-off-by: prateekpandey14 --- pkg/port/iscsit/iscsid.go | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/pkg/port/iscsit/iscsid.go b/pkg/port/iscsit/iscsid.go index 4d18a87..efd6415 100644 --- a/pkg/port/iscsit/iscsid.go +++ b/pkg/port/iscsit/iscsid.go @@ -47,6 +47,7 @@ const ( var ( EnableStats bool CurrentHostIP string + IPMutex sync.Mutex ) type ISCSITargetDriver struct { @@ -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() @@ -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, @@ -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...") @@ -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() } } @@ -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 { @@ -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 @@ -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 }