]> git.ozlabs.org Git - ccan/blob - ccan/iscsi/tools/iscsiclient.c
tdb: add test for tdb_summary
[ccan] / ccan / iscsi / tools / iscsiclient.c
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.
7  */
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.
13  */
14
15 /* This is the host/port we connect to.*/
16 #define TARGET "10.1.1.27:3260"
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <stdint.h>
21 #include <string.h>
22 #include <poll.h>
23 #include <ccan/iscsi/iscsi.h>
24 #include <ccan/iscsi/scsi-lowlevel.h>
25
26 struct client_state {
27        char *message;
28        int has_discovered_target;
29        char *target_name;
30        char *target_address;
31        int lun;
32        int block_size;
33 };
34
35 void nop_out_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
36 {
37         struct client_state *clnt = (struct client_state *)private_data;
38         struct iscsi_data *data = command_data;
39
40         printf("NOP-IN status:%d\n", status);
41         if (data->size > 0) {
42                 printf("NOP-IN data:%s\n", data->data);
43         }
44         exit(10);
45 }
46
47
48 void write10_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
49 {
50         struct client_state *clnt = (struct client_state *)private_data;
51         struct scsi_task *task = command_data;
52         int i;
53
54         if (status == ISCSI_STATUS_CHECK_CONDITION) {
55
56                 printf("Write10 failed with sense key:%d ascq:%04x\n", task->sense.key, task->sense.ascq);
57                 exit(10);
58         }
59
60         printf("Write successful\n");
61         exit(10);
62 }
63
64
65 void read10_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
66 {
67         struct client_state *clnt = (struct client_state *)private_data;
68         struct scsi_task *task = command_data;
69         int i;
70
71         if (status == ISCSI_STATUS_CHECK_CONDITION) {
72                 printf("Read10 failed with sense key:%d ascq:%04x\n", task->sense.key, task->sense.ascq);
73                 exit(10);
74         }
75
76         printf("READ10 successful. Block content:\n");
77         for (i=0;i<task->datain.size;i++) {
78                 printf("%02x ", task->datain.data[i]);
79                 if (i%16==15)
80                         printf("\n");
81                 if (i==69)
82                         break;
83         }
84         printf("...\n");
85
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");
90                 exit(10);
91         }
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");
95 //              exit(10);
96 //      }
97 }
98
99 void readcapacity10_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
100 {
101         struct client_state *clnt = (struct client_state *)private_data;
102         struct scsi_task *task = command_data;
103         struct scsi_readcapacity10 *rc10;
104         int full_size;
105
106         if (status == ISCSI_STATUS_CHECK_CONDITION) {
107                 printf("Readcapacity10 failed with sense key:%d ascq:%04x\n", task->sense.key, task->sense.ascq);
108                 exit(10);
109         }
110
111         full_size = scsi_datain_getfullsize(task);
112         if (full_size < task->datain.size) {
113                 printf("not enough data for full size readcapacity10\n");
114                 exit(10);
115         }
116
117         rc10 = scsi_datain_unmarshall(task);
118         if (rc10 == NULL) {
119                 printf("failed to unmarshall readcapacity10 data\n");
120                 exit(10);
121         }
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);
124         free(rc10);
125
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");
128                 exit(10);
129         }
130 }
131
132 void modesense6_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
133 {
134         struct client_state *clnt = (struct client_state *)private_data;
135         struct scsi_task *task = command_data;
136         int full_size;
137
138         if (status == ISCSI_STATUS_CHECK_CONDITION) {
139                 printf("Modesense6 failed with sense key:%d ascq:%04x\n", task->sense.key, task->sense.ascq);
140                 exit(10);
141         }
142
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");
148                         exit(10);
149                 }
150                 return;
151         }
152
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");
157                 exit(10);
158         }
159 }
160
161 void inquiry_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
162 {
163         struct client_state *clnt = (struct client_state *)private_data;
164         struct scsi_task *task = command_data;
165         struct scsi_inquiry_standard *inq;
166
167         if (status == ISCSI_STATUS_CHECK_CONDITION) {
168                 printf("Inquiry failed with sense key:%d ascq:%04x\n", task->sense.key, task->sense.ascq);
169                 exit(10);
170         }
171
172         printf("INQUIRY successful for standard data.\n");
173         inq = scsi_datain_unmarshall(task);
174         if (inq == NULL) {
175                 printf("failed to unmarshall inquiry datain blob\n");
176                 exit(10);
177         }
178
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");
183                 exit(10);
184         }
185
186 }
187
188 void testunitready_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
189 {
190         struct client_state *clnt = (struct client_state *)private_data;
191         struct scsi_task *task = command_data;
192
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");
197
198                         if (iscsi_testunitready_async(iscsi, clnt->lun, testunitready_cb, private_data) != 0) {
199                                 printf("failed to send testunitready command\n");
200                                 exit(10);
201                         }
202                 }
203                 return;
204         }
205
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");
209                 exit(10);
210         }
211 }
212
213
214 void reportluns_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
215 {
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;
220         int i;
221
222         if (status != ISCSI_STATUS_GOOD) {
223                 printf("Reportluns failed with unknown status code :%d\n", status);
224                 return;
225         }
226
227         full_report_size = scsi_datain_getfullsize(task);
228
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");
234                         exit(10);
235                 }
236                 return;
237         }
238
239         
240         list = scsi_datain_unmarshall(task);
241         if (list == NULL) {
242                 printf("failed to unmarshall reportluns datain blob\n");
243                 exit(10);
244         }
245         for (i=0; i < list->num; i++) {
246                 printf("LUN:%d found\n", list->luns[i]);
247                 clnt->lun = list->luns[i];
248         }
249
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");
254                 exit(10);
255         }
256 }
257
258
259 void normallogin_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
260 {
261         if (status != 0) {
262                 printf("Failed to log in to target. status :0x%04x\n", status);
263                 exit(10);
264         }
265
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");
269                 exit(10);
270         }
271 }
272
273
274 void normalconnect_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
275 {
276         printf("Connected to iscsi socket\n");
277
278         if (status != 0) {
279                 printf("normalconnect_cb: connection  failed status:%d\n", status);
280                 exit(10);
281         }
282
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");
287                 exit(10);
288         }
289 }
290
291
292
293 void discoverylogout_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
294 {
295         struct client_state *clnt = (struct client_state *)private_data;
296         
297         printf("discovery session logged out, Message from main() was:[%s]\n", clnt->message);
298
299         printf("disconnect socket\n");
300         if (iscsi_disconnect(iscsi) != 0) {
301                 printf("Failed to disconnect old socket\n");
302                 exit(10);
303         }
304
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");
309                 exit(10);
310         }
311         if (iscsi_set_alias(iscsi, "ronnie") != 0) {
312                 printf("Failed to add alias\n");
313                 exit(10);
314         }
315         if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
316                 printf("Failed to set settion type to normal\n");
317                 exit(10);
318         }
319
320         if (iscsi_connect_async(iscsi, clnt->target_address, normalconnect_cb, clnt) != 0) {
321                 printf("iscsi_connect failed\n");
322                 exit(10);
323         }
324 }
325
326 void discovery_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
327 {
328         struct client_state *clnt = (struct client_state *)private_data;
329         struct iscsi_discovery_address *addr;
330
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);
334         }
335
336         addr=command_data;
337         clnt->has_discovered_target = 1;
338         clnt->target_name    = strdup(addr->target_name);
339         clnt->target_address = strdup(addr->target_address);
340
341
342         printf("discovery complete, send logout command\n");
343
344         if (iscsi_logout_async(iscsi, discoverylogout_cb, private_data) != 0) {
345                 printf("iscsi_logout_async failed\n");
346                 exit(10);
347         }
348 }
349
350
351 void discoverylogin_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
352 {
353         if (status != 0) {
354                 printf("Failed to log in to target. status :0x%04x\n", status);
355                 exit(10);
356         }
357
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");
361                 exit(10);
362         }
363
364 }
365
366 void discoveryconnect_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
367 {
368         printf("Connected to iscsi socket status:0x%08x\n", status);
369
370         if (status != 0) {
371                 printf("discoveryconnect_cb: connection  failed status:%d\n", status);
372                 exit(10);
373         }
374
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");
379                 exit(10);
380         }
381 }
382
383
384 int main(int argc, char *argv[])
385 {
386         struct iscsi_context *iscsi;
387         struct pollfd pfd;
388         struct client_state clnt;
389
390         printf("iscsi client\n");
391
392         iscsi = iscsi_create_context("iqn.2002-10.com.ronnie:client");
393         if (iscsi == NULL) {
394                 printf("Failed to create context\n");
395                 exit(10);
396         }
397
398         if (iscsi_set_alias(iscsi, "ronnie") != 0) {
399                 printf("Failed to add alias\n");
400                 exit(10);
401         }
402
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");
407                 exit(10);
408         }
409
410         for (;;) {
411                 pfd.fd = iscsi_get_fd(iscsi);
412                 pfd.events = iscsi_which_events(iscsi);
413
414                 if (poll(&pfd, 1, -1) < 0) {
415                         printf("Poll failed");
416                         exit(10);
417                 }
418                 if (iscsi_service(iscsi, pfd.revents) < 0) {
419                         printf("iscsi_service failed\n");
420                         break;
421                 }
422         }
423
424 printf("STOP\n");
425 exit(10);
426
427         printf("ok\n");
428         return 0;
429 }
430