PR implementation

This commit is contained in:
Le Zhang
2016-10-25 10:34:13 +08:00
parent f724702ecb
commit 82e6d230a2
19 changed files with 2382 additions and 75 deletions

View File

@@ -54,7 +54,7 @@ type iscsiConnection struct {
sessionType int
sessionParam []ISCSISessionParam
tid int
cid uint16
CID uint16
rxIOState int
txIOState int
refcount int

View File

@@ -30,6 +30,7 @@ import (
"github.com/gostor/gotgt/pkg/port"
"github.com/gostor/gotgt/pkg/scsi"
"github.com/gostor/gotgt/pkg/util"
"github.com/satori/go.uuid"
)
type ISCSITargetService struct {
@@ -165,7 +166,7 @@ func (s *ISCSITargetService) handler(events byte, conn *iscsiConnection) {
s.txHandler(conn)
}
if conn.state == CONN_STATE_CLOSE {
glog.Warningf("iscsi connection[%d] closed", conn.cid)
glog.Warningf("iscsi connection[%d] closed", conn.CID)
conn.close()
}
}
@@ -324,6 +325,7 @@ func (s *ISCSITargetService) iscsiExecLogin(conn *iscsiConnection) error {
{"DataSequenceInOrder", "Yes"},
}),
}
conn.CID = cmd.ConnID
pairs := util.ParseKVText(cmd.RawData)
if initiatorName, ok := pairs["InitiatorName"]; ok {
conn.initiator = initiatorName
@@ -376,11 +378,14 @@ func (s *ISCSITargetService) iscsiExecLogin(conn *iscsiConnection) error {
case SESSION_NORMAL:
if conn.session == nil {
// create a new session
sess, err := s.NewISCSISession(conn)
sess, err := s.NewISCSISession(conn, cmd.ISID)
if err != nil {
glog.Error(err)
return err
}
itnexus := &api.ITNexus{uuid.NewV1(), GeniSCSIITNexusID(sess)}
scsi.AddITNexus(&sess.Target.SCSITarget, itnexus)
sess.ITNexusID = itnexus.ID
conn.session = sess
}
case SESSION_DISCOVERY:
@@ -804,7 +809,7 @@ func (s *ISCSITargetService) iscsiExecTask(task *iscsiTask) error {
task.scmd.Direction = api.SCSIDataWrite
}
}
task.scmd.CommandITNID = task.conn.session.Tsih
task.scmd.ITNexusID = task.conn.session.ITNexusID
task.scmd.SCB = bytes.NewBuffer(cmd.CDB)
task.scmd.SCBLength = len(cmd.CDB)
task.scmd.Lun = cmd.LUN

View File

@@ -78,8 +78,8 @@ type iSCSITPGT struct {
type ISCSITarget struct {
api.SCSITarget
api.SCSITargetDriverCommon
TPGTs map[uint16]*iSCSITPGT
Sessions []*ISCSISession
TPGTs map[uint16]*iSCSITPGT /* Key is a TPGT number */
Sessions map[uint16]*ISCSISession /* Key is an TSIH */
SessionParam []ISCSISessionParam
Alias string
MaxSessions int
@@ -87,6 +87,23 @@ type ISCSITarget struct {
Rdma int
NopInterval int
NopCount int
TSIHCounter uint16
}
/*
* RFC 3720 iSCSI SSID = ISID , TPGT
* ISID is an 6 bytes number, TPGT is an 2 bytes number
* We combime ISID and TPGT together to be SSID
*/
func MakeSSID(ISID uint64, TPGT uint16) uint64 {
SSID := ISID<<16 | uint64(TPGT)
return SSID
}
func ParseSSID(SSID uint64) (uint64, uint16) {
TPGT := uint16(uint64(0xFFFF) & SSID)
ISID := SSID >> 16
return ISID, TPGT
}
func (tgt *ISCSITarget) FindTPG(portal string) (uint16, error) {
@@ -103,8 +120,10 @@ func (tgt *ISCSITarget) FindTPG(portal string) (uint16, error) {
func newISCSITarget(target *api.SCSITarget) *ISCSITarget {
return &ISCSITarget{
SCSITarget: *target,
TPGTs: make(map[uint16]*iSCSITPGT),
SCSITarget: *target,
TPGTs: make(map[uint16]*iSCSITPGT),
Sessions: make(map[uint16]*ISCSISession),
TSIHCounter: 1,
}
}

View File

@@ -20,6 +20,8 @@ import (
"fmt"
"math/rand"
"time"
"github.com/satori/go.uuid"
)
var (
@@ -149,8 +151,9 @@ type ISCSISession struct {
Initiator string
InitiatorAlias string
Target *ISCSITarget
Isid uint64
Tsih uint64
ISID uint64
TSIH uint64
ITNexusID uuid.UUID
ExpCmdSN uint32
// only one connection per session
@@ -215,7 +218,7 @@ type iscsiPdu struct {
}
// New creates a new session.
func (s *ISCSITargetService) NewISCSISession(conn *iscsiConnection) (*ISCSISession, error) {
func (s *ISCSITargetService) NewISCSISession(conn *iscsiConnection, isid uint64) (*ISCSISession, error) {
var (
target *ISCSITarget
tsih uint64
@@ -235,7 +238,7 @@ func (s *ISCSITargetService) NewISCSISession(conn *iscsiConnection) (*ISCSISessi
rand.Seed(int64(time.Now().UTC().Nanosecond()))
tsih = uint64(rand.Uint32())
for _, s := range target.Sessions {
if s.Tsih == tsih {
if s.TSIH == tsih {
tsih = 0
break
}
@@ -246,7 +249,8 @@ func (s *ISCSITargetService) NewISCSISession(conn *iscsiConnection) (*ISCSISessi
}
sess := &ISCSISession{
Tsih: tsih,
TSIH: tsih,
ISID: isid,
Initiator: conn.initiator,
InitiatorAlias: conn.initiatorAlias,
Target: target,
@@ -259,3 +263,14 @@ func (s *ISCSITargetService) NewISCSISession(conn *iscsiConnection) (*ISCSISessi
conn.session = sess
return sess, nil
}
/*
* iSCSI I_T nexus identifer = (iSCSI Initiator Name + 'i' + ISID, iSCSI Target Name + 't' + Portal Group Tag)
*/
func GeniSCSIITNexusID(sess *ISCSISession) string {
strID := fmt.Sprintf("%si0x%12x,%st%d",
sess.Initiator, sess.ISID,
sess.Target.SCSITarget.Name,
sess.Connections[0].tpgt)
return strID
}