| 1 | |
package org.jscsi.target.connection.phase; |
| 2 | |
|
| 3 | |
|
| 4 | |
import java.io.IOException; |
| 5 | |
import java.security.DigestException; |
| 6 | |
|
| 7 | |
import javax.naming.OperationNotSupportedException; |
| 8 | |
|
| 9 | |
import org.jscsi.exception.InternetSCSIException; |
| 10 | |
import org.jscsi.parser.BasicHeaderSegment; |
| 11 | |
import org.jscsi.parser.ProtocolDataUnit; |
| 12 | |
import org.jscsi.parser.scsi.SCSICommandParser; |
| 13 | |
import org.jscsi.target.connection.Connection; |
| 14 | |
import org.jscsi.target.connection.stage.TMStage; |
| 15 | |
import org.jscsi.target.connection.stage.fullfeature.FormatUnitStage; |
| 16 | |
import org.jscsi.target.connection.stage.fullfeature.InquiryStage; |
| 17 | |
import org.jscsi.target.connection.stage.fullfeature.LogoutStage; |
| 18 | |
import org.jscsi.target.connection.stage.fullfeature.ModeSenseStage; |
| 19 | |
import org.jscsi.target.connection.stage.fullfeature.PingStage; |
| 20 | |
import org.jscsi.target.connection.stage.fullfeature.ReadCapacityStage; |
| 21 | |
import org.jscsi.target.connection.stage.fullfeature.ReadStage; |
| 22 | |
import org.jscsi.target.connection.stage.fullfeature.ReportLunsStage; |
| 23 | |
import org.jscsi.target.connection.stage.fullfeature.RequestSenseStage; |
| 24 | |
import org.jscsi.target.connection.stage.fullfeature.SendDiagnosticStage; |
| 25 | |
import org.jscsi.target.connection.stage.fullfeature.TargetFullFeatureStage; |
| 26 | |
import org.jscsi.target.connection.stage.fullfeature.TestUnitReadyStage; |
| 27 | |
import org.jscsi.target.connection.stage.fullfeature.TextNegotiationStage; |
| 28 | |
import org.jscsi.target.connection.stage.fullfeature.UnsupportedOpCodeStage; |
| 29 | |
import org.jscsi.target.connection.stage.fullfeature.WriteStage; |
| 30 | |
import org.jscsi.target.scsi.cdb.ScsiOperationCode; |
| 31 | |
import org.jscsi.target.settings.SettingsException; |
| 32 | |
import org.slf4j.Logger; |
| 33 | |
import org.slf4j.LoggerFactory; |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
public final class TargetFullFeaturePhase extends TargetPhase { |
| 43 | |
|
| 44 | 0 | private static final Logger LOGGER = LoggerFactory.getLogger(TargetFullFeaturePhase.class); |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
private TargetFullFeatureStage stage; |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
private boolean running; |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
public TargetFullFeaturePhase (Connection connection) { |
| 63 | 0 | super(connection); |
| 64 | |
|
| 65 | 0 | } |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
public boolean execute () throws DigestException , IOException , InterruptedException , InternetSCSIException , SettingsException { |
| 79 | |
|
| 80 | 0 | running = true; |
| 81 | 0 | while (running) { |
| 82 | 0 | ProtocolDataUnit pdu = connection.receivePdu(); |
| 83 | 0 | BasicHeaderSegment bhs = pdu.getBasicHeaderSegment(); |
| 84 | |
|
| 85 | |
|
| 86 | 0 | switch (bhs.getOpCode()) { |
| 87 | |
|
| 88 | |
case SCSI_COMMAND : |
| 89 | 0 | if (connection.getTargetSession().isNormalSession()) { |
| 90 | 0 | final SCSICommandParser parser = (SCSICommandParser) bhs.getParser(); |
| 91 | 0 | ScsiOperationCode scsiOpCode = ScsiOperationCode.valueOf(parser.getCDB().get(0)); |
| 92 | |
|
| 93 | 0 | LOGGER.debug("scsiOpCode = " + scsiOpCode); |
| 94 | |
|
| 95 | |
|
| 96 | 0 | if (scsiOpCode != null) { |
| 97 | 0 | switch (scsiOpCode) { |
| 98 | |
case TEST_UNIT_READY : |
| 99 | 0 | stage = new TestUnitReadyStage(this); |
| 100 | 0 | break; |
| 101 | |
case REQUEST_SENSE : |
| 102 | 0 | stage = new RequestSenseStage(this); |
| 103 | 0 | break; |
| 104 | |
case FORMAT_UNIT : |
| 105 | 0 | stage = new FormatUnitStage(this); |
| 106 | 0 | break; |
| 107 | |
case INQUIRY : |
| 108 | 0 | stage = new InquiryStage(this); |
| 109 | 0 | break; |
| 110 | |
case MODE_SELECT_6 : |
| 111 | 0 | stage = null; |
| 112 | 0 | scsiOpCode = null; |
| 113 | 0 | break; |
| 114 | |
case MODE_SENSE_6 : |
| 115 | 0 | stage = new ModeSenseStage(this); |
| 116 | 0 | if (!((ModeSenseStage) stage).canHandle(pdu)) { |
| 117 | 0 | stage = null; |
| 118 | 0 | scsiOpCode = null; |
| 119 | |
} |
| 120 | |
break; |
| 121 | |
case SEND_DIAGNOSTIC : |
| 122 | 0 | stage = new SendDiagnosticStage(this); |
| 123 | 0 | break; |
| 124 | |
case READ_CAPACITY_10 : |
| 125 | |
case READ_CAPACITY_16 : |
| 126 | 0 | stage = new ReadCapacityStage(this); |
| 127 | 0 | break; |
| 128 | |
case WRITE_6 : |
| 129 | |
case WRITE_10 : |
| 130 | 0 | stage = new WriteStage(this); |
| 131 | 0 | break; |
| 132 | |
case READ_6 : |
| 133 | |
case READ_10 : |
| 134 | 0 | stage = new ReadStage(this); |
| 135 | 0 | break; |
| 136 | |
case REPORT_LUNS : |
| 137 | 0 | stage = new ReportLunsStage(this); |
| 138 | 0 | break; |
| 139 | |
default : |
| 140 | 0 | scsiOpCode = null; |
| 141 | |
|
| 142 | |
} |
| 143 | |
} |
| 144 | 0 | if (scsiOpCode == null) { |
| 145 | 0 | LOGGER.error("Unsupported SCSI OpCode 0x" + Integer.toHexString(parser.getCDB().get(0) & 255) + " in SCSI Command PDU."); |
| 146 | 0 | stage = new UnsupportedOpCodeStage(this); |
| 147 | |
} |
| 148 | |
|
| 149 | 0 | } else { |
| 150 | 0 | throw new InternetSCSIException("received SCSI command in discovery session"); |
| 151 | |
} |
| 152 | |
break; |
| 153 | |
|
| 154 | |
case SCSI_TM_REQUEST : |
| 155 | 0 | stage = new TMStage(this); |
| 156 | 0 | break; |
| 157 | |
case NOP_OUT : |
| 158 | 0 | stage = new PingStage(this); |
| 159 | 0 | break; |
| 160 | |
case TEXT_REQUEST : |
| 161 | 0 | stage = new TextNegotiationStage(this); |
| 162 | 0 | break; |
| 163 | |
case LOGOUT_REQUEST : |
| 164 | 0 | stage = new LogoutStage(this); |
| 165 | 0 | running = false; |
| 166 | 0 | break; |
| 167 | |
default : |
| 168 | 0 | LOGGER.error("Recieved unsupported opcode for " + pdu.getBasicHeaderSegment().getOpCode()); |
| 169 | 0 | stage = new UnsupportedOpCodeStage(this); |
| 170 | |
} |
| 171 | |
|
| 172 | |
|
| 173 | 0 | stage.execute(pdu); |
| 174 | 0 | } |
| 175 | |
|
| 176 | 0 | return false; |
| 177 | |
} |
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
public void stop(){ |
| 183 | 0 | this.running = false; |
| 184 | 0 | } |
| 185 | |
} |