Merge pull request #52 from carmark/task

support iscsi task function
This commit is contained in:
Lei Xue
2017-06-21 15:30:43 +08:00
committed by GitHub
10 changed files with 244 additions and 55 deletions

View File

@@ -34,8 +34,10 @@ script:
- cd ${HOME}/libiscsi
- export ISCSITEST=yes
- ./autogen.sh
- ./configure
- make
- ./configure 2>&1 >/dev/null
- make 2>&1 >/dev/null
# This case will be failed, because the gotgt does not support async mode now.
#- ./test-tool/iscsi-test-cu -d -A -V --test=iSCSI.iSCSITMF iscsi://127.0.0.1:3260/${TARGET}/0
- ./test-tool/iscsi-test-cu -d -A --test=SCSI.TestUnitReady iscsi://127.0.0.1:3260/${TARGET}/0
- ./test-tool/iscsi-test-cu -d -A --test=SCSI.ReadCapacity10 iscsi://127.0.0.1:3260/${TARGET}/0
- ./test-tool/iscsi-test-cu -d -A --test=SCSI.ReadCapacity16 iscsi://127.0.0.1:3260/${TARGET}/0

View File

@@ -151,11 +151,20 @@ type SCSIDataBuffer struct {
Resid int32
}
type SCSICommandState uint64
var (
SCSICommandQueued SCSICommandState = 1
SCSICommandProcessed SCSICommandState = 2
SCSICommandAsync SCSICommandState = 3
SCSICommandNotLast SCSICommandState = 4
)
type SCSICommand struct {
Target *SCSITarget
DeviceID uint64
Device *SCSILu
State uint64
State SCSICommandState
Direction SCSIDataDirection
InSDBBuffer SCSIDataBuffer
OutSDBBuffer SCSIDataBuffer

View File

@@ -1,3 +1,19 @@
/*
Copyright 2017 The GoStor Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package iscsit
import (
@@ -61,6 +77,12 @@ var opCodeMap = map[OpCode]string{
const DataPadding = 4
type ISCSITaskManagementFunc struct {
Result byte
TaskFunc uint32
ReferencedTaskTag uint32
}
type ISCSICommand struct {
OpCode OpCode
RawHeader []byte
@@ -105,6 +127,9 @@ type ISCSICommand struct {
Status byte
SCSIResponse byte
// Task request
ISCSITaskManagementFunc
// R2T
R2TSN uint32
DesiredLength uint32
@@ -193,13 +218,13 @@ func parseHeader(data []byte) (*ISCSICommand, error) {
// TODO: sync.Pool
m := &ISCSICommand{}
m.Immediate = 0x40&data[0] == 0x40
m.OpCode = OpCode(data[0] & 0x3f)
m.OpCode = OpCode(data[0] & ISCSI_OPCODE_MASK)
m.Final = 0x80&data[1] == 0x80
m.AHSLen = int(data[4]) * 4
m.DataLen = int(ParseUint(data[5:8]))
m.TaskTag = uint32(ParseUint(data[16:20]))
switch m.OpCode {
case OpSCSICmd, OpSCSITaskReq:
case OpSCSICmd:
m.LUN = [8]byte{data[9]}
m.ExpectedDataLen = uint32(ParseUint(data[20:24]))
m.CmdSN = uint32(ParseUint(data[24:28]))
@@ -207,6 +232,10 @@ func parseHeader(data []byte) (*ISCSICommand, error) {
m.Write = data[1]&0x20 == 0x20
m.CDB = data[32:48]
m.ExpStatSN = uint32(ParseUint(data[28:32]))
fallthrough
case OpSCSITaskReq:
m.ReferencedTaskTag = uint32(ParseUint(data[20:24]))
m.TaskFunc = uint32(data[1] & ISCSI_FLAG_TM_FUNC_MASK)
case OpSCSIResp:
case OpSCSIOut:
m.LUN = [8]byte{data[9]}
@@ -253,8 +282,10 @@ func (m *ISCSICommand) scsiCmdRespBytes() []byte {
buf.WriteByte(byte(m.SCSIResponse))
buf.WriteByte(byte(m.Status))
buf.WriteByte(0x00)
buf.Write(util.MarshalUint64(uint64(len(m.RawData)))[5:]) // 5-8
// Skip through to byte 16
for i := 0; i < 3*4; i++ {
for i := 0; i < 8; i++ {
buf.WriteByte(0x00)
}
buf.Write(util.MarshalUint64(uint64(m.TaskTag))[4:])
@@ -408,7 +439,7 @@ func (m *ISCSICommand) scsiTMFRespBytes() []byte {
buf := &bytes.Buffer{}
buf.WriteByte(byte(OpSCSITaskResp))
buf.WriteByte(0x80)
buf.WriteByte(0x00)
buf.WriteByte(m.Result)
buf.WriteByte(0x00)
// Skip through to byte 16

View File

@@ -1,5 +1,5 @@
/*
Copyright 2016 The GoStor Authors All rights reserved.
Copyright 2017 The GoStor Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -96,11 +96,12 @@ const (
)
type iscsiTask struct {
tag uint32
conn *iscsiConnection
cmd *ISCSICommand
scmd *api.SCSICommand
state taskState
tag uint32
conn *iscsiConnection
cmd *ISCSICommand
scmd *api.SCSICommand
state taskState
result byte
offset int
r2tCount int
@@ -146,11 +147,13 @@ func (conn *iscsiConnection) ReInstatement(newConn *iscsiConnection) {
conn.conn = newConn.conn
}
func (conn *iscsiConnection) buildRespPackage(oc OpCode) error {
func (conn *iscsiConnection) buildRespPackage(oc OpCode, task *iscsiTask) error {
conn.txTask = &iscsiTask{conn: conn, cmd: conn.req, tag: conn.req.TaskTag, scmd: &api.SCSICommand{}}
conn.txIOState = IOSTATE_TX_BHS
conn.statSN += 1
task := conn.rxTask
if task == nil {
task = conn.rxTask
}
resp := &ISCSICommand{
StatSN: conn.req.ExpStatSN,
TaskTag: conn.req.TaskTag,
@@ -174,7 +177,10 @@ func (conn *iscsiConnection) buildRespPackage(oc OpCode) error {
resp.HasStatus = true
scmd := task.scmd
resp.Status = scmd.Result
if scmd.Direction == api.SCSIDataRead || scmd.Direction == api.SCSIDataWrite {
if scmd.Result != 0 && scmd.SenseBuffer != nil {
length := util.MarshalUint32(uint32(scmd.SenseLength))
resp.RawData = append(length[2:4], scmd.SenseBuffer.Bytes()...)
} else if scmd.Direction == api.SCSIDataRead || scmd.Direction == api.SCSIDataWrite {
if scmd.InSDBBuffer.Buffer != nil {
buf := scmd.InSDBBuffer.Buffer.Bytes()
resp.RawData = buf
@@ -182,14 +188,18 @@ func (conn *iscsiConnection) buildRespPackage(oc OpCode) error {
resp.RawData = []byte{}
}
}
if scmd.Result != 0 && scmd.SenseBuffer != nil {
resp.RawData = scmd.SenseBuffer.Bytes()
}
case OpNoopIn, OpSCSITaskResp, OpReject:
case OpNoopIn, OpReject:
resp.OpCode = oc
resp.Final = true
resp.NSG = FullFeaturePhase
resp.ExpCmdSN = conn.req.CmdSN + 1
case OpSCSITaskResp:
resp.OpCode = oc
resp.Final = true
resp.NSG = FullFeaturePhase
resp.ExpCmdSN = conn.req.CmdSN + 1
resp.Result = task.result
case OpLoginResp:
resp.OpCode = OpLoginResp
resp.Transit = conn.loginParam.tgtTrans
@@ -212,3 +222,41 @@ func (conn *iscsiConnection) buildRespPackage(oc OpCode) error {
conn.resp = resp
return nil
}
func (conn *iscsiConnection) State() string {
switch conn.state {
case CONN_STATE_FREE:
return "free"
case CONN_STATE_SECURITY:
return "begin security"
case CONN_STATE_SECURITY_AUTH:
return "security auth"
case CONN_STATE_SECURITY_DONE:
return "done security"
case CONN_STATE_SECURITY_LOGIN:
return "security login"
case CONN_STATE_SECURITY_FULL:
return "security full"
case CONN_STATE_LOGIN:
return "begin login"
case CONN_STATE_LOGIN_FULL:
return "done login"
case CONN_STATE_FULL:
return "full feature"
case CONN_STATE_KERNEL:
return "kernel"
case CONN_STATE_CLOSE:
return "close"
case CONN_STATE_EXIT:
return "exit"
case CONN_STATE_SCSI:
return "scsi"
case CONN_STATE_INIT:
return "init"
case CONN_STATE_START:
return "start"
case CONN_STATE_READY:
return "ready"
}
return ""
}

View File

@@ -1,5 +1,5 @@
/*
Copyright 2016 The GoStor Authors All rights reserved.
Copyright 2017 The GoStor Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -45,12 +45,12 @@ type ISCSITargetDriver struct {
}
func init() {
scsi.RegisterTargetDriver("iscsi", NewISCSITargetDriver)
scsi.RegisterTargetDriver(iSCSIDriverName, NewISCSITargetDriver)
}
func NewISCSITargetDriver(base *scsi.SCSITargetService) (scsi.SCSITargetDriver, error) {
return &ISCSITargetDriver{
Name: "iscsi",
Name: iSCSIDriverName,
iSCSITargets: map[string]*ISCSITarget{},
SCSI: base,
TSIHPool: map[uint16]bool{0: true, 65535: true},
@@ -111,11 +111,11 @@ func (s *ISCSITargetDriver) AddiSCSIPortal(tgtName string, tpgt uint16, portal s
)
if target, ok = s.iSCSITargets[tgtName]; !ok {
return fmt.Errorf("no target %s", tgtName)
return fmt.Errorf("No such target: %s", tgtName)
}
if tpgtInfo, ok = target.TPGTs[tpgt]; !ok {
return fmt.Errorf("no tpgt %d", tpgt)
return fmt.Errorf("No such TPGT: %d", tpgt)
}
tgtPortals := tpgtInfo.Portals
@@ -359,7 +359,7 @@ func (s *ISCSITargetDriver) iscsiExecLogin(conn *iscsiConnection) error {
conn.state = CONN_STATE_LOGIN
}
return conn.buildRespPackage(OpLoginResp)
return conn.buildRespPackage(OpLoginResp, nil)
}
func iscsiExecLogout(conn *iscsiConnection) error {
@@ -414,19 +414,15 @@ func (s *ISCSITargetDriver) iscsiExecText(conn *iscsiConnection) error {
}
func iscsiExecNoopOut(conn *iscsiConnection) error {
return conn.buildRespPackage(OpNoopIn)
}
func iscsiExecTMFunction(conn *iscsiConnection) error {
return conn.buildRespPackage(OpSCSITaskResp)
return conn.buildRespPackage(OpNoopIn, nil)
}
func iscsiExecReject(conn *iscsiConnection) error {
return conn.buildRespPackage(OpReject)
return conn.buildRespPackage(OpReject, nil)
}
func iscsiExecR2T(conn *iscsiConnection) error {
return conn.buildRespPackage(OpReady)
return conn.buildRespPackage(OpReady, nil)
}
func (s *ISCSITargetDriver) txHandler(conn *iscsiConnection) {
@@ -522,27 +518,21 @@ SendRemainingData:
}
}
log.Debugf("connection state: %d", conn.state)
log.Debugf("connection state: %v", conn.State())
switch conn.state {
case CONN_STATE_CLOSE, CONN_STATE_EXIT:
log.Warnf("set connection to close")
conn.state = CONN_STATE_CLOSE
case CONN_STATE_SECURITY_LOGIN:
conn.state = CONN_STATE_LOGIN
log.Debugf("CONN_STATE_LOGIN")
case CONN_STATE_LOGIN:
log.Debugf("CONN_STATE_LOGIN")
conn.rxIOState = IOSTATE_RX_BHS
s.handler(DATAIN, conn)
case CONN_STATE_SECURITY_FULL, CONN_STATE_LOGIN_FULL:
if conn.session.SessionType == SESSION_NORMAL {
conn.state = CONN_STATE_KERNEL
log.Debugf("CONN_STATE_KERNEL")
conn.state = CONN_STATE_SCSI
log.Debugf("CONN_STATE_SCSI")
} else {
conn.state = CONN_STATE_FULL
log.Debugf("CONN_STATE_FULL")
}
conn.rxIOState = IOSTATE_RX_BHS
s.handler(DATAIN, conn)
@@ -598,18 +588,20 @@ func (s *ISCSITargetDriver) scsiCommandHandler(conn *iscsiConnection) (err error
if err = s.iscsiTaskQueueHandler(task); err != nil {
return
} else {
if scmd.Direction == api.SCSIDataRead {
conn.buildRespPackage(OpSCSIIn)
if scmd.Direction == api.SCSIDataRead && scmd.SenseBuffer == nil {
conn.buildRespPackage(OpSCSIIn, task)
} else {
conn.buildRespPackage(OpSCSIResp)
conn.buildRespPackage(OpSCSIResp, task)
}
conn.rxTask = nil
}
case OpSCSITaskReq:
// task management function
conn.txTask = &iscsiTask{conn: conn, cmd: conn.req, tag: conn.req.TaskTag, state: taskPending}
conn.txIOState = IOSTATE_TX_BHS
iscsiExecTMFunction(conn)
task := &iscsiTask{conn: conn, cmd: conn.req, tag: conn.req.TaskTag, scmd: nil}
conn.rxTask = task
if err = s.iscsiTaskQueueHandler(task); err != nil {
return
}
case OpSCSIOut:
log.Debugf("iSCSI Data-out processing...")
var task *iscsiTask
@@ -651,7 +643,7 @@ func (s *ISCSITargetDriver) scsiCommandHandler(conn *iscsiConnection) (err error
if err = s.iscsiExecTask(task); err != nil {
return
} else {
conn.buildRespPackage(OpSCSIResp)
conn.buildRespPackage(OpSCSIResp, task)
conn.rxTask = nil
}
case OpNoopOut:
@@ -696,13 +688,16 @@ func (s *ISCSITargetDriver) iscsiTaskQueueHandler(task *iscsiTask) error {
if len(sess.PendingTasks) == 0 {
return nil
}
sess.PendingTasksMutex.Lock()
task = sess.PendingTasks.Pop().(*iscsiTask)
cmd = task.cmd
if cmd.CmdSN != cmdsn {
sess.PendingTasks.Push(task)
sess.PendingTasksMutex.Unlock()
return nil
}
task.state = taskSCSI
sess.PendingTasksMutex.Unlock()
goto retry
} else {
if cmd.CmdSN < sess.ExpCmdSN {
@@ -711,9 +706,11 @@ func (s *ISCSITargetDriver) iscsiTaskQueueHandler(task *iscsiTask) error {
return err
}
log.Debugf("add task(%d) into task queue", task.cmd.CmdSN)
// add this connection into queue and set this task as pending task
// add this task into queue and set it as a pending task
sess.PendingTasksMutex.Lock()
task.state = taskPending
sess.PendingTasks.Push(task)
sess.PendingTasksMutex.Unlock()
return fmt.Errorf("pending")
}
@@ -755,6 +752,50 @@ func (s *ISCSITargetDriver) iscsiExecTask(task *iscsiTask) error {
case OpNoopOut:
// just do it in iscsi layer
case OpSCSITaskReq:
sess := task.conn.session
switch cmd.TaskFunc {
case ISCSI_TM_FUNC_ABORT_TASK:
stask := &iscsiTask{}
sess.PendingTasksMutex.Lock()
for i, t := range sess.PendingTasks {
if cmd.ReferencedTaskTag == t.tag {
stask = sess.PendingTasks[i]
sess.PendingTasks = append(sess.PendingTasks[:i], sess.PendingTasks[i+1:]...)
break
}
}
sess.PendingTasksMutex.Unlock()
if stask == nil {
task.result = ISCSI_TMF_RSP_NO_TASK
} else {
// abort this task
log.Debugf("abort the task[%v]", stask.tag)
if stask.scmd == nil {
stask.scmd = &api.SCSICommand{Result: api.SAM_STAT_TASK_ABORTED}
}
stask.conn = task.conn
log.Debugf("stask.conn: %#v", stask.conn)
stask.conn.buildRespPackage(OpSCSIResp, stask)
stask.conn.rxTask = nil
s.handler(DATAOUT, stask.conn)
task.result = ISCSI_TMF_RSP_COMPLETE
}
case ISCSI_TM_FUNC_ABORT_TASK_SET:
case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
case ISCSI_TM_FUNC_CLEAR_ACA:
fallthrough
case ISCSI_TM_FUNC_CLEAR_TASK_SET:
fallthrough
case ISCSI_TM_FUNC_TARGET_WARM_RESET, ISCSI_TM_FUNC_TARGET_COLD_RESET, ISCSI_TM_FUNC_TASK_REASSIGN:
task.result = ISCSI_TMF_RSP_NOT_SUPPORTED
return fmt.Errorf("The task function is not supported")
default:
task.result = ISCSI_TMF_RSP_REJECTED
return fmt.Errorf("Unknown task function")
}
// return response to initiator
return task.conn.buildRespPackage(OpSCSITaskResp, task)
}
return nil
}

View File

@@ -25,6 +25,8 @@ import (
"github.com/gostor/gotgt/pkg/api"
)
const iSCSIDriverName = "iscsi"
const (
IOSTATE_FREE = iota
@@ -53,7 +55,7 @@ const (
IOSTATE_TX_END
)
var ISCSI_OPCODE_MASK = 0x3F
var ISCSI_OPCODE_MASK byte = 0x3F
type ISCSIDiscoveryMethod string
@@ -95,7 +97,7 @@ type ISCSITarget struct {
/*
* 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
* We combine ISID and TPGT together to be SSID
*/
func MakeSSID(ISID uint64, TPGT uint16) uint64 {
SSID := ISID<<16 | uint64(TPGT)

View File

@@ -253,6 +253,7 @@ type ISCSISession struct {
ConnectionsRWMutex sync.RWMutex
Commands []*ISCSICommand
PendingTasks taskQueue
PendingTasksMutex sync.RWMutex
MaxQueueCommand uint32
SessionParam ISCSISessionParamList
Info string

55
pkg/port/iscsit/task.go Normal file
View File

@@ -0,0 +1,55 @@
/*
Copyright 2017 The GoStor Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// iSCSI task management
package iscsit
const (
ISCSI_FLAG_TM_FUNC_MASK byte = 0x7F
// Function values
// aborts the task identified by the Referenced Task Tag field
ISCSI_TM_FUNC_ABORT_TASK = 1
// aborts all Tasks issued via this session on the logical unit
ISCSI_TM_FUNC_ABORT_TASK_SET = 2
// clears the Auto Contingent Allegiance condition
ISCSI_TM_FUNC_CLEAR_ACA = 3
// aborts all Tasks in the appropriate task set as defined by the TST field in the Control mode page
ISCSI_TM_FUNC_CLEAR_TASK_SET = 4
ISCSI_TM_FUNC_LOGICAL_UNIT_RESET = 5
ISCSI_TM_FUNC_TARGET_WARM_RESET = 6
ISCSI_TM_FUNC_TARGET_COLD_RESET = 7
// reassigns connection allegiance for the task identified by the Referenced Task Tag field to this connection, thus resuming the iSCSI exchanges for the task
ISCSI_TM_FUNC_TASK_REASSIGN = 8
// Response values
// Function complete
ISCSI_TMF_RSP_COMPLETE = 0x00
// Task does not exist
ISCSI_TMF_RSP_NO_TASK = 0x01
// LUN does not exist
ISCSI_TMF_RSP_NO_LUN = 0x02
// Task still allegiant
ISCSI_TMF_RSP_TASK_ALLEGIANT = 0x03
// Task allegiance reassignment not supported
ISCSI_TMF_RSP_NO_FAILOVER = 0x04
// Task management function not supported
ISCSI_TMF_RSP_NOT_SUPPORTED = 0x05
// Function authorization failed
ISCSI_TMF_RSP_AUTH_FAILED = 0x06
// Function rejected
ISCSI_TMF_RSP_REJECTED = 0xff
)

View File

@@ -89,6 +89,7 @@ func luPerformCommand(tid int, cmd *api.SCSICommand) api.SAMStat {
fnop := fn.(SCSIDeviceOperation)
// TODO host := cmd.ITNexus.Host
host := 0
cmd.State = api.SCSICommandProcessed
return fnop.CommandPerformFunc(host, cmd)
}
return api.SAMStatGood

View File

@@ -1,5 +1,5 @@
/*
Copyright 2016 The GoStor Authors All rights reserved.
Copyright 2017 The GoStor Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -151,10 +151,9 @@ func BuildSenseData(cmd *api.SCSICommand, key byte, asc SCSISubError) {
}
senseBuffer.WriteByte((byte(asc) >> 8) & 0xff)
senseBuffer.WriteByte(byte(asc) & 0xff)
senseBuffer.WriteByte(0x00)
senseBuffer.WriteByte(0x00)
senseBuffer.WriteByte(0x00)
senseBuffer.WriteByte(0x00)
for i := 0; i < 4; i++ {
senseBuffer.WriteByte(0x00)
}
cmd.SenseLength = length + 8
}
if ok {