1 /* This is an example of using libiscsi.
2 * It basically logs in to the the target and performs a discovery.
3 * It then selects the last target in the returned list and
4 * starts a normal login to that target.
5 * Once logged in it issues a REPORTLUNS call and selects the last returned lun in the list.
6 * This LUN is then used to send INQUIRY, READCAPACITY10 and READ10 test calls to.
8 /* The reason why we have to specify an allocation length and sometimes probe, starting with a small value, probing how big the buffer
9 * should be, and asking again with a bigger buffer.
10 * Why not just always ask with a buffer that is big enough?
11 * The reason is that a lot of scsi targets are "sensitive" and ""buggy""
12 * many targets will just fail the operation completely if they thing alloc len is unreasonably big.
15 /* This is the host/port we connect to.*/
16 #define TARGET "10.1.1.27:3260"
23 #include <ccan/iscsi/iscsi.h>
24 #include <ccan/iscsi/scsi-lowlevel.h>
28 int has_discovered_target;
35 void nop_out_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
37 struct client_state *clnt = (struct client_state *)private_data;
38 struct iscsi_data *data = command_data;
40 printf("NOP-IN status:%d\n", status);
42 printf("NOP-IN data:%s\n", data->data);
48 void write10_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
50 struct client_state *clnt = (struct client_state *)private_data;
51 struct scsi_task *task = command_data;
54 if (status == ISCSI_STATUS_CHECK_CONDITION) {
56 printf("Write10 failed with sense key:%d ascq:%04x\n", task->sense.key, task->sense.ascq);
60 printf("Write successful\n");
65 void read10_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
67 struct client_state *clnt = (struct client_state *)private_data;
68 struct scsi_task *task = command_data;
71 if (status == ISCSI_STATUS_CHECK_CONDITION) {
72 printf("Read10 failed with sense key:%d ascq:%04x\n", task->sense.key, task->sense.ascq);
76 printf("READ10 successful. Block content:\n");
77 for (i=0;i<task->datain.size;i++) {
78 printf("%02x ", task->datain.data[i]);
86 printf("Finished, wont try to write data since that will likely destroy your LUN :-(\n");
87 printf("Send NOP-OUT\n");
88 if (iscsi_nop_out_async(iscsi, nop_out_cb, "Ping!", 6, private_data) != 0) {
89 printf("failed to send nop-out\n");
92 // printf("write the block back\n");
93 // if (iscsi_write10_async(iscsi, clnt->lun, write10_cb, task->data.datain, task->datain.size, 0, 0, 0, clnt->block_size, private_data) != 0) {
94 // printf("failed to send write10 command\n");
99 void readcapacity10_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
101 struct client_state *clnt = (struct client_state *)private_data;
102 struct scsi_task *task = command_data;
103 struct scsi_readcapacity10 *rc10;
106 if (status == ISCSI_STATUS_CHECK_CONDITION) {
107 printf("Readcapacity10 failed with sense key:%d ascq:%04x\n", task->sense.key, task->sense.ascq);
111 full_size = scsi_datain_getfullsize(task);
112 if (full_size < task->datain.size) {
113 printf("not enough data for full size readcapacity10\n");
117 rc10 = scsi_datain_unmarshall(task);
119 printf("failed to unmarshall readcapacity10 data\n");
122 clnt->block_size = rc10->block_size;
123 printf("READCAPACITY10 successful. Size:%d blocks blocksize:%d. Read first block\n", rc10->lba, rc10->block_size);
126 if (iscsi_read10_async(iscsi, clnt->lun, read10_cb, 0, clnt->block_size, clnt->block_size, private_data) != 0) {
127 printf("failed to send read10 command\n");
132 void modesense6_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
134 struct client_state *clnt = (struct client_state *)private_data;
135 struct scsi_task *task = command_data;
138 if (status == ISCSI_STATUS_CHECK_CONDITION) {
139 printf("Modesense6 failed with sense key:%d ascq:%04x\n", task->sense.key, task->sense.ascq);
143 full_size = scsi_datain_getfullsize(task);
144 if (full_size > task->datain.size) {
145 printf("did not get enough data for mode sense, sening modesense again asking for bigger buffer\n");
146 if (iscsi_modesense6_async(iscsi, clnt->lun, modesense6_cb, 0, SCSI_MODESENSE_PC_CURRENT, SCSI_MODESENSE_PAGECODE_RETURN_ALL_PAGES, 0, full_size, private_data) != 0) {
147 printf("failed to send modesense6 command\n");
153 printf("MODESENSE6 successful.\n");
154 printf("Send READCAPACITY10\n");
155 if (iscsi_readcapacity10_async(iscsi, clnt->lun, readcapacity10_cb, 0, 0, private_data) != 0) {
156 printf("failed to send readcapacity command\n");
161 void inquiry_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
163 struct client_state *clnt = (struct client_state *)private_data;
164 struct scsi_task *task = command_data;
165 struct scsi_inquiry_standard *inq;
167 if (status == ISCSI_STATUS_CHECK_CONDITION) {
168 printf("Inquiry failed with sense key:%d ascq:%04x\n", task->sense.key, task->sense.ascq);
172 printf("INQUIRY successful for standard data.\n");
173 inq = scsi_datain_unmarshall(task);
175 printf("failed to unmarshall inquiry datain blob\n");
179 printf("Device Type is %d. VendorId:%s ProductId:%s\n", inq->periperal_device_type, inq->vendor_identification, inq->product_identification);
180 printf("Send MODESENSE6\n");
181 if (iscsi_modesense6_async(iscsi, clnt->lun, modesense6_cb, 0, SCSI_MODESENSE_PC_CURRENT, SCSI_MODESENSE_PAGECODE_RETURN_ALL_PAGES, 0, 4, private_data) != 0) {
182 printf("failed to send modesense6 command\n");
188 void testunitready_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
190 struct client_state *clnt = (struct client_state *)private_data;
191 struct scsi_task *task = command_data;
193 if (status == ISCSI_STATUS_CHECK_CONDITION) {
194 printf("First testunitready failed with sense key:%d ascq:%04x\n", task->sense.key, task->sense.ascq);
195 if (task->sense.key == SCSI_SENSE_KEY_UNIT_ATTENTION && task->sense.ascq == SCSI_SENSE_ASCQ_BUS_RESET) {
196 printf("target device just came online, try again\n");
198 if (iscsi_testunitready_async(iscsi, clnt->lun, testunitready_cb, private_data) != 0) {
199 printf("failed to send testunitready command\n");
206 printf("TESTUNITREADY successful, do an inquiry on lun:%d\n", clnt->lun);
207 if (iscsi_inquiry_async(iscsi, clnt->lun, inquiry_cb, 0, 0, 64, private_data) != 0) {
208 printf("failed to send inquiry command\n");
214 void reportluns_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
216 struct client_state *clnt = (struct client_state *)private_data;
217 struct scsi_task *task = command_data;
218 struct scsi_reportluns_list *list;
219 uint32_t full_report_size;
222 if (status != ISCSI_STATUS_GOOD) {
223 printf("Reportluns failed with unknown status code :%d\n", status);
227 full_report_size = scsi_datain_getfullsize(task);
229 printf("REPORTLUNS status:%d data size:%d, full reports luns data size:%d\n", status, task->datain.size, full_report_size);
230 if (full_report_size > task->datain.size) {
231 printf("We did not get all the data we need in reportluns, ask again\n");
232 if (iscsi_reportluns_async(iscsi, reportluns_cb, 0, full_report_size, private_data) != 0) {
233 printf("failed to send reportluns command\n");
240 list = scsi_datain_unmarshall(task);
242 printf("failed to unmarshall reportluns datain blob\n");
245 for (i=0; i < list->num; i++) {
246 printf("LUN:%d found\n", list->luns[i]);
247 clnt->lun = list->luns[i];
250 printf("Will use LUN:%d\n", clnt->lun);
251 printf("Send testunitready to lun %d\n", clnt->lun);
252 if (iscsi_testunitready_async(iscsi, clnt->lun, testunitready_cb, private_data) != 0) {
253 printf("failed to send testunitready command\n");
259 void normallogin_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
262 printf("Failed to log in to target. status :0x%04x\n", status);
266 printf("Logged in normal session, send reportluns\n");
267 if (iscsi_reportluns_async(iscsi, reportluns_cb, 0, 16, private_data) != 0) {
268 printf("failed to send reportluns command\n");
274 void normalconnect_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
276 printf("Connected to iscsi socket\n");
279 printf("normalconnect_cb: connection failed status:%d\n", status);
283 printf("connected, send login command\n");
284 iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL);
285 if (iscsi_login_async(iscsi, normallogin_cb, private_data) != 0) {
286 printf("iscsi_login_async failed\n");
293 void discoverylogout_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
295 struct client_state *clnt = (struct client_state *)private_data;
297 printf("discovery session logged out, Message from main() was:[%s]\n", clnt->message);
299 printf("disconnect socket\n");
300 if (iscsi_disconnect(iscsi) != 0) {
301 printf("Failed to disconnect old socket\n");
305 printf("reconnect with normal login to [%s]\n", clnt->target_address);
306 printf("Use targetname [%s] when connecting\n", clnt->target_name);
307 if (iscsi_set_targetname(iscsi, clnt->target_name)) {
308 printf("Failed to set target name\n");
311 if (iscsi_set_alias(iscsi, "ronnie") != 0) {
312 printf("Failed to add alias\n");
315 if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
316 printf("Failed to set settion type to normal\n");
320 if (iscsi_connect_async(iscsi, clnt->target_address, normalconnect_cb, clnt) != 0) {
321 printf("iscsi_connect failed\n");
326 void discovery_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
328 struct client_state *clnt = (struct client_state *)private_data;
329 struct iscsi_discovery_address *addr;
331 printf("discovery callback status:%04x\n", status);
332 for(addr=command_data; addr; addr=addr->next) {
333 printf("Target:%s Address:%s\n", addr->target_name, addr->target_address);
337 clnt->has_discovered_target = 1;
338 clnt->target_name = strdup(addr->target_name);
339 clnt->target_address = strdup(addr->target_address);
342 printf("discovery complete, send logout command\n");
344 if (iscsi_logout_async(iscsi, discoverylogout_cb, private_data) != 0) {
345 printf("iscsi_logout_async failed\n");
351 void discoverylogin_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
354 printf("Failed to log in to target. status :0x%04x\n", status);
358 printf("Logged in to target, send discovery command\n");
359 if (iscsi_discovery_async(iscsi, discovery_cb, private_data) != 0) {
360 printf("failed to send discovery command\n");
366 void discoveryconnect_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
368 printf("Connected to iscsi socket status:0x%08x\n", status);
371 printf("discoveryconnect_cb: connection failed status:%d\n", status);
375 printf("connected, send login command\n");
376 iscsi_set_session_type(iscsi, ISCSI_SESSION_DISCOVERY);
377 if (iscsi_login_async(iscsi, discoverylogin_cb, private_data) != 0) {
378 printf("iscsi_login_async failed\n");
384 int main(int argc, char *argv[])
386 struct iscsi_context *iscsi;
388 struct client_state clnt;
390 printf("iscsi client\n");
392 iscsi = iscsi_create_context("iqn.2002-10.com.ronnie:client");
394 printf("Failed to create context\n");
398 if (iscsi_set_alias(iscsi, "ronnie") != 0) {
399 printf("Failed to add alias\n");
403 clnt.message = "Hello iSCSI";
404 clnt.has_discovered_target = 0;
405 if (iscsi_connect_async(iscsi, TARGET, discoveryconnect_cb, &clnt) != 0) {
406 printf("iscsi_connect failed\n");
411 pfd.fd = iscsi_get_fd(iscsi);
412 pfd.events = iscsi_which_events(iscsi);
414 if (poll(&pfd, 1, -1) < 0) {
415 printf("Poll failed");
418 if (iscsi_service(iscsi, pfd.revents) < 0) {
419 printf("iscsi_service failed\n");