-
Notifications
You must be signed in to change notification settings - Fork 240
/
initiator.c
2298 lines (1985 loc) · 61.4 KB
/
initiator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* iSCSI Session Management and Slow-path Control
*
* Copyright (C) 2004 Dmitry Yusupov, Alex Aizman
* Copyright (C) 2006 Mike Christie
* Copyright (C) 2006 Red Hat, Inc. All rights reserved.
* maintained by open-iscsi@googlegroups.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* See the file COPYING included with this distribution for more details.
*/
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include "initiator.h"
#include "transport.h"
#include "iscsid.h"
#include "iscsi_if.h"
#include "mgmt_ipc.h"
#include "event_poll.h"
#include "iscsi_ipc.h"
#include "idbm.h"
#include "log.h"
#include "iscsi_util.h"
#include "scsi.h"
#include "iscsi_sysfs.h"
#include "iscsi_settings.h"
#include "iface.h"
#include "host.h"
#include "sysdeps.h"
#include "iscsi_err.h"
#include "kern_err_table.h"
#define ISCSI_CONN_ERR_REOPEN_DELAY 3
#define ISCSI_INTERNAL_ERR_REOPEN_DELAY 5
#define PROC_DIR "/proc"
struct login_task_retry_info {
actor_t retry_actor;
queue_task_t *qtask;
node_rec_t *rec;
int retry_count;
};
static void iscsi_login_timedout(void *data);
static int iscsi_sched_ev_context(struct iscsi_ev_context *ev_context,
struct iscsi_conn *conn, unsigned long tmo,
int event);
static int queue_session_login_task_retry(struct login_task_retry_info *info,
node_rec_t *rec, queue_task_t *qtask);
static int iscsi_ev_context_alloc(iscsi_conn_t *conn)
{
int i;
for (i = 0; i < CONTEXT_POOL_MAX; i++) {
conn->context_pool[i] = calloc(1,
sizeof(struct iscsi_ev_context) +
ipc->ctldev_bufmax);
if (!conn->context_pool[i]) {
int j;
for (j = 0; j < i; j++)
free(conn->context_pool[j]);
return ENOMEM;
}
conn->context_pool[i]->conn = conn;
}
return 0;
}
static void iscsi_ev_context_free(iscsi_conn_t *conn)
{
int i;
for (i = 0; i < CONTEXT_POOL_MAX; i++) {
if (!conn->context_pool[i])
continue;
if (conn->context_pool[i]->allocated)
/* missing flush on shutdown */
conn_error(conn, "BUG: context_pool leak %p",
conn->context_pool[i]);
free(conn->context_pool[i]);
}
}
static struct iscsi_ev_context *
iscsi_ev_context_get(iscsi_conn_t *conn, int ev_size)
{
struct iscsi_ev_context *ev_context;
int i;
if (ev_size > ipc->ctldev_bufmax)
return NULL;
for (i = 0; i < CONTEXT_POOL_MAX; i++) {
if (!conn->context_pool[i])
continue;
if (!conn->context_pool[i]->allocated) {
ev_context = conn->context_pool[i];
memset(&ev_context->actor, 0,
sizeof(struct actor));
ev_context->allocated = 1;
/* some callers abuse this pointer */
ev_context->data = (void *)ev_context +
sizeof(struct iscsi_ev_context);
conn_debug(7, conn, "get ev context %p",
&ev_context->actor);
return ev_context;
}
}
return NULL;
}
static void iscsi_ev_context_put(struct iscsi_ev_context *ev_context)
{
log_debug(7, "put ev context %p:%s", &ev_context->actor,
(&ev_context->actor)->name);
ev_context->allocated = 0;
}
static void session_online_devs(int host_no, int sid)
{
iscsi_sysfs_for_each_device(NULL, host_no, sid,
iscsi_sysfs_set_device_online);
}
static conn_login_status_e
__login_response_status(iscsi_conn_t *conn,
enum iscsi_login_status login_status)
{
switch (login_status) {
case LOGIN_OK:
/* check the status class and detail */
return CONN_LOGIN_SUCCESS;
case LOGIN_REDIRECT:
return CONN_LOGIN_IMM_REDIRECT_RETRY;
case LOGIN_IO_ERROR:
case LOGIN_REDIRECTION_FAILED:
return CONN_LOGIN_RETRY;
default:
conn_error(conn, "Login error (Login status %d)", login_status);
break;
}
return CONN_LOGIN_FAILED;
}
static conn_login_status_e
__check_iscsi_status_class(iscsi_session_t *session, int cid,
uint8_t status_class, uint8_t status_detail)
{
iscsi_conn_t *conn = &session->conn[cid];
switch (status_class) {
case ISCSI_STATUS_CLS_SUCCESS:
return CONN_LOGIN_SUCCESS;
case ISCSI_STATUS_CLS_REDIRECT:
switch (status_detail) {
case ISCSI_LOGIN_STATUS_TGT_MOVED_TEMP:
return CONN_LOGIN_IMM_RETRY;
case ISCSI_LOGIN_STATUS_TGT_MOVED_PERM:
/*
* for a permanent redirect, we need to update the
* failback address
*/
memset(&conn->failback_saddr, 0,
sizeof(struct sockaddr_storage));
conn->failback_saddr = conn->saddr;
return CONN_LOGIN_IMM_REDIRECT_RETRY;
default:
conn_error(conn, "login rejected: redirection "
"type 0x%x not supported", status_detail);
return CONN_LOGIN_RETRY;
}
case ISCSI_STATUS_CLS_INITIATOR_ERR:
switch (status_detail) {
case ISCSI_LOGIN_STATUS_AUTH_FAILED:
log_error("session %d login rejected: Initiator "
"failed authentication with target",
session->id);
return CONN_LOGIN_AUTH_FAILED;
case ISCSI_LOGIN_STATUS_TGT_FORBIDDEN:
conn_error(conn, "login rejected: initiator "
"failed authorization with target");
return CONN_LOGIN_AUTH_FAILED;
case ISCSI_LOGIN_STATUS_TGT_NOT_FOUND:
conn_error(conn, "login rejected: initiator "
"error - target not found (%02x/%02x)",
status_class, status_detail);
return CONN_LOGIN_FAILED;
case ISCSI_LOGIN_STATUS_NO_VERSION:
/*
* FIXME: if we handle multiple protocol versions,
* before we log an error, try the other supported
* versions.
*/
conn_error(conn, "login rejected: incompatible "
"version (%02x/%02x), non-retryable, "
"giving up", status_class, status_detail);
return CONN_LOGIN_FAILED;
default:
conn_error(conn, "login rejected: initiator "
"error (%02x/%02x)", status_class, status_detail);
return CONN_LOGIN_FAILED;
}
case ISCSI_STATUS_CLS_TARGET_ERR:
conn_error(conn, "login rejected: target error "
"(%02x/%02x)", status_class, status_detail);
/*
* We have no idea what the problem is. But spec says initiator
* may retry later.
*/
return CONN_LOGIN_RETRY;
default:
conn_error(conn, "login response with unknown status "
"class 0x%x, detail 0x%x", status_class, status_detail);
break;
}
return CONN_LOGIN_FAILED;
}
static int
__session_conn_create(iscsi_session_t *session, int cid)
{
iscsi_conn_t *conn = &session->conn[cid];
conn_rec_t *conn_rec = &session->nrec.conn[cid];
node_rec_t *rec = &session->nrec;
int err;
conn->id = cid;
if (iscsi_ev_context_alloc(conn)) {
conn_error(conn, "cannot allocate context_pool");
return ISCSI_ERR_NOMEM;
}
/* set session reconnection retry max */
session->reopen_max = rec->session.reopen_max;
conn->state = ISCSI_CONN_STATE_FREE;
conn->session = session;
actor_init(&conn->login_timer, iscsi_login_timedout, NULL);
/*
* TODO: we must export the socket_fd/transport_eph from sysfs
* so if iscsid is resyncing up we can pick that up and cleanup up
* the old connection. Right now we leak a connection.
* We can also probably merge these two fields.
*/
conn->socket_fd = -1;
conn->transport_ep_handle = -1;
/* connection's timeouts */
conn->logout_timeout = conn_rec->timeo.logout_timeout;
if (!conn->logout_timeout) {
conn_error(conn, "Invalid timeo.logout_timeout. Must be greater "
"than zero. Using default %d.",
DEF_LOGOUT_TIMEO);
conn->logout_timeout = DEF_LOGOUT_TIMEO;
}
conn->login_timeout = conn_rec->timeo.login_timeout;
if (!conn->login_timeout) {
conn_error(conn, "Invalid timeo.login_timeout. Must be greater "
"than zero. Using default %d.",
DEF_LOGIN_TIMEO);
conn->login_timeout = DEF_LOGIN_TIMEO;
}
conn->auth_timeout = conn_rec->timeo.auth_timeout;
/* noop-out setting */
conn->noop_out_interval = conn_rec->timeo.noop_out_interval;
conn->noop_out_timeout = conn_rec->timeo.noop_out_timeout;
if (conn->noop_out_interval && !conn->noop_out_timeout) {
conn_error(conn, "Invalid timeo.noop_out_timeout. Must be greater "
"than zero. Using default %d.",
DEF_NOOP_OUT_TIMEO);
conn->noop_out_timeout = DEF_NOOP_OUT_TIMEO;
}
if (conn->noop_out_timeout && !conn->noop_out_interval) {
conn_error(conn, "Invalid timeo.noop_out_interval. Must be greater "
"than zero. Using default %d.",
DEF_NOOP_OUT_INTERVAL);
conn->noop_out_interval = DEF_NOOP_OUT_INTERVAL;
}
iscsi_copy_operational_params(conn, &session->nrec.session.iscsi,
&conn_rec->iscsi);
/* TCP options */
conn->tcp_window_size = conn_rec->tcp.window_size;
/* FIXME: type_of_service */
/* resolve the string address to an IP address */
err = iscsi_setup_portal(conn, conn_rec->address, conn_rec->port);
if (err)
return err;
return 0;
}
static void
session_release(iscsi_session_t *session)
{
log_debug(2, "Releasing session:%d %p", session->id, session);
if (session->target_alias)
free(session->target_alias);
iscsi_ev_context_free(&session->conn[0]);
free(session);
}
static iscsi_session_t*
__session_create(node_rec_t *rec, struct iscsi_transport *t, int *rc)
{
iscsi_session_t *session;
int hostno;
*rc = 0;
session = calloc(1, sizeof (*session));
if (session == NULL) {
log_debug(1, "can not allocate memory for session");
*rc = ISCSI_ERR_NOMEM;
return NULL;
}
log_debug(2, "Allocted session %p", session);
INIT_LIST_HEAD(&session->list);
session->t = t;
session->reopen_qtask.mgmt_ipc_fd = -1;
session->id = INVALID_SESSION_ID;
session->use_ipc = 1;
/* save node record. we might need it for redirection */
memcpy(&session->nrec, rec, sizeof(node_rec_t));
session->portal_group_tag = rec->tpgt;
session->type = ISCSI_SESSION_TYPE_NORMAL;
session->r_stage = R_STAGE_NO_CHANGE;
strlcpy(session->target_name, rec->name, TARGET_NAME_MAXLEN);
if (strlen(session->nrec.iface.iname))
session->initiator_name = session->nrec.iface.iname;
else if (dconfig->initiator_name)
session->initiator_name = dconfig->initiator_name;
else {
log_error("No initiator name set. Cannot create session.");
*rc = ISCSI_ERR_INVAL;
goto free_session;
}
if (strlen(session->nrec.iface.alias))
session->initiator_alias = session->nrec.iface.alias;
else
session->initiator_alias = dconfig->initiator_alias;
/* session's eh parameters */
session->replacement_timeout = rec->session.timeo.replacement_timeout;
session->fast_abort = rec->session.iscsi.FastAbort;
session->abort_timeout = rec->session.err_timeo.abort_timeout;
session->lu_reset_timeout = rec->session.err_timeo.lu_reset_timeout;
session->tgt_reset_timeout = rec->session.err_timeo.tgt_reset_timeout;
session->host_reset_timeout = rec->session.err_timeo.host_reset_timeout;
/* OUI and uniqifying number */
session->isid[0] = DRIVER_ISID_0;
session->isid[1] = DRIVER_ISID_1;
session->isid[2] = DRIVER_ISID_2;
session->isid[3] = 0;
session->isid[4] = 0;
session->isid[5] = 0;
/* setup authentication variables for the session*/
iscsi_setup_authentication(session, &rec->session.auth);
iscsi_session_init_params(session);
if (t->template->bind_ep_required) {
hostno = iscsi_sysfs_get_host_no_from_hwinfo(&rec->iface, rc);
if (!*rc) {
/*
* if the netdev or mac was set, then we are going to want
* to want to bind the all the conns/eps to a specific host
* if offload is used.
*/
session->conn[0].bind_ep = 1;
session->hostno = hostno;
} else if (*rc == ISCSI_ERR_HOST_NOT_FOUND) {
goto free_session;
} else {
*rc = 0;
}
}
/* reset session reopen count */
session->reopen_cnt = 0;
list_add_tail(&session->list, &t->sessions);
return session;
free_session:
free(session);
return NULL;
}
static void iscsi_flush_context_pool(struct iscsi_session *session)
{
struct iscsi_ev_context *ev_context;
struct iscsi_conn *conn = &session->conn[0];
int i;
for (i = 0; i < CONTEXT_POOL_MAX; i++) {
ev_context = conn->context_pool[i];
if (!ev_context)
continue;
if (ev_context->allocated) {
actor_delete(&(conn->context_pool[i]->actor));
iscsi_ev_context_put(ev_context);
}
}
}
static void
__session_destroy(iscsi_session_t *session)
{
log_debug(1, "destroying session:%d", session->id);
list_del(&session->list);
iscsi_flush_context_pool(session);
session_release(session);
}
static void
conn_delete_timers(iscsi_conn_t *conn)
{
actor_delete(&conn->login_timer);
actor_delete(&conn->nop_out_timer);
}
static int
session_conn_shutdown(iscsi_conn_t *conn, queue_task_t *qtask,
int err)
{
iscsi_session_t *session = conn->session;
conn_debug(2, conn, "disconnect conn");
/* this will check for a valid interconnect connection */
if (session->t->template->ep_disconnect)
session->t->template->ep_disconnect(conn);
if (session->id == INVALID_SESSION_ID)
goto cleanup;
if (!iscsi_sysfs_session_has_leadconn(session->id))
goto cleanup;
if (conn->state == ISCSI_CONN_STATE_IN_LOGIN ||
conn->state == ISCSI_CONN_STATE_IN_LOGOUT ||
conn->state == ISCSI_CONN_STATE_LOGGED_IN) {
conn_debug(2, conn, "stop conn (conn state %d)", conn->state);
if (ipc->stop_conn(session->t->handle, session->id,
conn->id, STOP_CONN_TERM)) {
conn_error(conn, "can't stop connection (%d)", errno);
return ISCSI_ERR_INTERNAL;
}
}
conn_debug(2, conn, "kdestroy conn");
if (ipc->destroy_conn(session->t->handle, session->id,
conn->id)) {
conn_error(conn, "can not safely destroy connection");
return ISCSI_ERR_INTERNAL;
}
cleanup:
if (session->id != INVALID_SESSION_ID) {
log_debug(2, "kdestroy session %u", session->id);
session->r_stage = R_STAGE_SESSION_DESTOYED;
if (ipc->destroy_session(session->t->handle, session->id)) {
log_error("can not safely destroy session %d",
session->id);
return ISCSI_ERR_INTERNAL;
}
}
log_warning("Connection%d:%d to [target: %s, portal: %s,%d] "
"through [iface: %s] is shutdown.",
session->id, conn->id, session->nrec.name,
session->nrec.conn[conn->id].address,
session->nrec.conn[conn->id].port,
session->nrec.iface.name);
mgmt_ipc_write_rsp(qtask, err);
conn_delete_timers(conn);
__session_destroy(session);
return ISCSI_SUCCESS;
}
static void
queue_delayed_reopen(queue_task_t *qtask, int delay)
{
iscsi_conn_t *conn = qtask->conn;
conn_debug(4, conn, "Requeue reopen attempt in %d secs", delay);
/*
* iscsi_login_eh can handle the login resched as
* if it were login time out
*/
actor_timer_mod(&conn->login_timer, delay, qtask);
}
static int iscsi_conn_connect(struct iscsi_conn *conn, queue_task_t *qtask)
{
struct iscsi_ev_context *ev_context;
int rc;
ev_context = iscsi_ev_context_get(conn, 0);
if (!ev_context) {
/* while reopening the recv pool should be full */
conn_error(conn, "BUG: __session_conn_reopen could not get conn "
"context for recv.");
return ENOMEM;
}
ev_context->data = qtask;
rc = conn->session->t->template->ep_connect(conn, 1);
if (rc < 0 && errno != EINPROGRESS) {
char serv[NI_MAXSERV];
getnameinfo((struct sockaddr *) &conn->saddr,
sizeof(conn->saddr),
conn->host, sizeof(conn->host), serv, sizeof(serv),
NI_NUMERICHOST|NI_NUMERICSERV);
conn_error(conn, "cannot make a connection to %s:%s (%d,%d)",
conn->host, serv, rc, errno);
iscsi_ev_context_put(ev_context);
return ENOTCONN;
}
iscsi_sched_ev_context(ev_context, conn, 0, EV_CONN_POLL);
conn_debug(3, conn, "Setting login timer %p timeout %d", &conn->login_timer,
conn->login_timeout);
actor_timer_mod(&conn->login_timer, conn->login_timeout, qtask);
return 0;
}
static void
__session_conn_reopen(iscsi_conn_t *conn, queue_task_t *qtask, int do_stop,
int redirected)
{
iscsi_session_t *session = conn->session;
uint32_t delay;
log_debug(1, "re-opening session %d (reopen_cnt %d)", session->id,
session->reopen_cnt);
qtask->conn = conn;
/* flush stale polls or errors queued */
iscsi_flush_context_pool(session);
conn_delete_timers(conn);
conn->state = ISCSI_CONN_STATE_XPT_WAIT;
conn->session->t->template->ep_disconnect(conn);
if (do_stop) {
/* state: ISCSI_CONN_STATE_CLEANUP_WAIT */
if (ipc->stop_conn(session->t->handle, session->id,
conn->id, do_stop)) {
log_error("can't stop connection %d:%d (%d)",
session->id, conn->id, errno);
delay = ISCSI_INTERNAL_ERR_REOPEN_DELAY;
goto queue_reopen;
}
log_debug(3, "connection %d:%d is stopped for recovery",
session->id, conn->id);
}
if (!redirected) {
delay = session->def_time2wait;
session->def_time2wait = 0;
if (delay)
goto queue_reopen;
}
if (!redirected)
session->reopen_cnt++;
/* uIP will needs to be re-triggered on the connection re-open */
if (iscsi_set_net_config(conn->session->t, conn->session,
&conn->session->nrec.iface) != 0)
goto queue_reopen;
if (iscsi_conn_connect(conn, qtask)) {
delay = ISCSI_CONN_ERR_REOPEN_DELAY;
goto queue_reopen;
}
return;
queue_reopen:
conn_debug(4, conn, "Waiting %u seconds before trying to reconnect.", delay);
queue_delayed_reopen(qtask, delay);
}
static void
session_conn_reopen(iscsi_conn_t *conn, queue_task_t *qtask, int do_stop)
{
/*
* If we were temporarily redirected, we need to fall back to
* the original address to see where the target will send us
* for the retry
*/
memset(&conn->saddr, 0, sizeof(struct sockaddr_storage));
conn->saddr = conn->failback_saddr;
__session_conn_reopen(conn, qtask, do_stop, 0);
}
static int iscsi_retry_initial_login(struct iscsi_conn *conn)
{
int initial_login_retry_max;
struct timeval now, timeout, fail_time;
initial_login_retry_max =
conn->session->nrec.session.initial_login_retry_max;
memset(&now, 0, sizeof(now));
memset(&timeout, 0, sizeof(timeout));
memset(&fail_time, 0, sizeof(fail_time));
timeout.tv_sec = initial_login_retry_max * conn->login_timeout;
if (gettimeofday(&now, NULL)) {
conn_error(conn, "Could not get time of day. Dropping down to "
"max retry check.");
return initial_login_retry_max > conn->session->reopen_cnt;
}
timeradd(&conn->initial_connect_time, &timeout, &fail_time);
/*
* if we have been trying for login_retry_max * login_timeout
* then it is time to give up
*/
if (timercmp(&now, &fail_time, >)) {
conn_debug(1, conn, "Giving up on initial login attempt after "
"%u seconds.",
initial_login_retry_max * conn->login_timeout);
return 0;
}
return 1;
}
static int iscsi_login_is_fatal_err(int err)
{
if (err == ISCSI_ERR_LOGIN_AUTH_FAILED ||
err == ISCSI_ERR_FATAL_LOGIN)
return 1;
return 0;
}
static void iscsi_login_eh(struct iscsi_conn *conn, struct queue_task *qtask,
int err)
{
struct iscsi_session *session = conn->session;
int stop_flag = 0;
conn_debug(3, conn, "iscsi_login_eh");
/*
* Flush polls and other events
*/
iscsi_flush_context_pool(conn->session);
switch (conn->state) {
case ISCSI_CONN_STATE_XPT_WAIT:
switch (session->r_stage) {
case R_STAGE_NO_CHANGE:
conn_debug(6, conn, "login failed ISCSI_CONN_STATE_XPT_WAIT/"
"R_STAGE_NO_CHANGE");
/* timeout during initial connect.
* clean connection. write ipc rsp or retry */
if (iscsi_login_is_fatal_err(err) ||
!iscsi_retry_initial_login(conn))
session_conn_shutdown(conn, qtask, err);
else {
stop_flag = (session->id < INVALID_SESSION_ID) ? STOP_CONN_TERM : 0;
conn_debug(6, conn, "connection %p socket_fd: %d stop_flag: %d\n",
conn, conn->socket_fd, stop_flag);
session_conn_reopen(conn, qtask, stop_flag);
}
break;
case R_STAGE_SESSION_REDIRECT:
conn_debug(6, conn, "login failed ISCSI_CONN_STATE_XPT_WAIT/"
"R_STAGE_SESSION_REDIRECT");
/* timeout during initial redirect connect
* clean connection. write ipc rsp or retry */
if (iscsi_login_is_fatal_err(err) ||
!iscsi_retry_initial_login(conn))
session_conn_shutdown(conn, qtask, err);
else
session_conn_reopen(conn, qtask, 0);
break;
case R_STAGE_SESSION_REOPEN:
conn_debug(6, conn, "login failed ISCSI_CONN_STATE_XPT_WAIT/"
"R_STAGE_SESSION_REOPEN (reopen_cnt=%d, reopen_max=%d)",
session->reopen_cnt, session->reopen_max);
if (session->reopen_max &&
(session->reopen_cnt > session->reopen_max)) {
log_info("Giving up on session %d after %d retries",
session->id, session->reopen_max);
session_conn_shutdown(conn, qtask, err);
break;
}
/* timeout during reopen connect. try again */
session_conn_reopen(conn, qtask, 0);
break;
case R_STAGE_SESSION_CLEANUP:
session_conn_shutdown(conn, qtask, err);
break;
default:
break;
}
break;
case ISCSI_CONN_STATE_IN_LOGIN:
switch (session->r_stage) {
case R_STAGE_NO_CHANGE:
case R_STAGE_SESSION_REDIRECT:
conn_debug(6, conn, "login failed ISCSI_CONN_STATE_IN_LOGIN/"
"R_STAGE_NO_CHANGE %d",
session->reopen_cnt);
/*
* send pdu timeout during initial connect or
* initial redirected connect. Clean connection
* and write rsp or retry.
*/
if (iscsi_login_is_fatal_err(err) ||
!iscsi_retry_initial_login(conn))
session_conn_shutdown(conn, qtask, err);
else
session_conn_reopen(conn, qtask,
STOP_CONN_RECOVER);
break;
case R_STAGE_SESSION_REOPEN:
conn_debug(6, conn, "login failed ISCSI_CONN_STATE_IN_LOGIN/"
"R_STAGE_SESSION_REOPEN %d",
session->reopen_cnt);
session_conn_reopen(conn, qtask, STOP_CONN_RECOVER);
break;
case R_STAGE_SESSION_CLEANUP:
session_conn_shutdown(conn, qtask,
ISCSI_ERR_PDU_TIMEOUT);
break;
default:
break;
}
break;
default:
conn_error(conn, "Ignoring login error %d in conn state %d.",
err, conn->state);
break;
}
}
static void
__conn_error_handle(iscsi_session_t *session, iscsi_conn_t *conn)
{
int i;
/*
* if we got an error while trying to logout for the user then
* just cleanup and return to the user.
*/
if (conn->logout_qtask) {
session_conn_shutdown(conn, conn->logout_qtask, ISCSI_SUCCESS);
return;
}
switch (conn->state) {
case ISCSI_CONN_STATE_IN_LOGOUT:
/* logout was from eh - fall down to cleanup */
case ISCSI_CONN_STATE_LOGGED_IN:
/* mark failed connection */
conn->state = ISCSI_CONN_STATE_CLEANUP_WAIT;
if (session->erl > 0) {
/* check if we still have some logged in connections */
for (i=0; i<ISCSI_CONN_MAX; i++) {
if (session->conn[i].state ==
ISCSI_CONN_STATE_LOGGED_IN)
break;
}
if (i != ISCSI_CONN_MAX) {
/* FIXME: re-assign leading connection
* for ERL>0 */
}
break;
}
/* mark all connections as failed */
for (i=0; i<ISCSI_CONN_MAX; i++) {
if (session->conn[i].state ==
ISCSI_CONN_STATE_LOGGED_IN)
session->conn[i].state =
ISCSI_CONN_STATE_CLEANUP_WAIT;
}
session->r_stage = R_STAGE_SESSION_REOPEN;
break;
case ISCSI_CONN_STATE_IN_LOGIN:
if (session->r_stage == R_STAGE_SESSION_REOPEN) {
queue_task_t *qtask;
if (session->notify_qtask)
qtask = session->notify_qtask;
else
qtask = &session->reopen_qtask;
iscsi_login_eh(conn, qtask, ISCSI_ERR_TRANS);
return;
}
conn_debug(1, conn, "ignoring conn error in login. "
"let it timeout");
return;
case ISCSI_CONN_STATE_XPT_WAIT:
conn_debug(1, conn, "ignoring conn error in XPT_WAIT. "
"let connection fail on its own");
return;
case ISCSI_CONN_STATE_CLEANUP_WAIT:
conn_debug(1, conn, "ignoring conn error in CLEANUP_WAIT. "
"let connection stop");
return;
default:
conn_debug(8, conn, "invalid state %d", conn->state);
return;
}
if (session->r_stage == R_STAGE_SESSION_REOPEN) {
session_conn_reopen(conn, &session->reopen_qtask,
STOP_CONN_RECOVER);
return;
}
}
static void session_conn_error(void *data)
{
struct iscsi_ev_context *ev_context = data;
enum iscsi_err error = *(enum iscsi_err *)ev_context->data;
iscsi_conn_t *conn = ev_context->conn;
iscsi_session_t *session = conn->session;
int sid = session->id;
log_warning("Kernel reported iSCSI connection %d:%d error (%d - %s) "
"state (%d)", session->id, conn->id, error,
kern_err_code_to_string(error), conn->state);
iscsi_ev_context_put(ev_context);
switch (error) {
case ISCSI_ERR_INVALID_HOST:
if (session_conn_shutdown(conn, NULL, ISCSI_SUCCESS))
log_error("BUG: Could not shutdown session:%d.", sid);
break;
default:
__conn_error_handle(session, conn);
}
}
static void iscsi_login_timedout(void *data)
{
struct queue_task *qtask = data;
struct iscsi_conn *conn = qtask->conn;
switch (conn->state) {
case ISCSI_CONN_STATE_XPT_WAIT:
iscsi_login_eh(conn, qtask, ISCSI_ERR_TRANS_TIMEOUT);
break;
case ISCSI_CONN_STATE_IN_LOGIN:
iscsi_login_eh(conn, qtask, ISCSI_ERR_PDU_TIMEOUT);
break;
default:
iscsi_login_eh(conn, qtask, ISCSI_ERR_INTERNAL);
break;
}
}
static void iscsi_login_redirect(iscsi_conn_t *conn)
{
iscsi_session_t *session = conn->session;
iscsi_login_context_t *c = &conn->login_context;
conn_debug(3, conn, "login redirect ...");
if (session->r_stage == R_STAGE_NO_CHANGE)
session->r_stage = R_STAGE_SESSION_REDIRECT;
__session_conn_reopen(conn, c->qtask, STOP_CONN_RECOVER, 1);
}
static int
__send_nopin_rsp(iscsi_conn_t *conn, struct iscsi_nopin *rhdr, char *data)
{
struct iscsi_nopout hdr;
memset(&hdr, 0, sizeof(struct iscsi_nopout));
hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
hdr.flags = ISCSI_FLAG_CMD_FINAL;
hdr.dlength[0] = rhdr->dlength[0];
hdr.dlength[1] = rhdr->dlength[1];
hdr.dlength[2] = rhdr->dlength[2];
memcpy(hdr.lun, rhdr->lun, 8);
hdr.ttt = rhdr->ttt;
hdr.itt = ISCSI_RESERVED_TAG;
return iscsi_io_send_pdu(conn, (struct iscsi_hdr*)&hdr,
ISCSI_DIGEST_NONE, data, ISCSI_DIGEST_NONE, 0);
}
static int
__send_nopout(iscsi_conn_t *conn)
{
struct iscsi_nopout hdr;
memset(&hdr, 0, sizeof(struct iscsi_nopout));
hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
hdr.flags = ISCSI_FLAG_CMD_FINAL;
hdr.itt = 0; /* XXX: let kernel send_pdu set for us*/
hdr.ttt = ISCSI_RESERVED_TAG;
/* we have hdr.lun reserved, and no data */
return iscsi_io_send_pdu(conn, (struct iscsi_hdr*)&hdr,
ISCSI_DIGEST_NONE, NULL, ISCSI_DIGEST_NONE, 0);
}
static void conn_nop_out_timeout(void *data)
{
iscsi_conn_t *conn = (iscsi_conn_t*)data;
iscsi_session_t *session = conn->session;
log_warning("Nop-out timedout after %d seconds on connection %d:%d "
"state (%d). Dropping session.", conn->noop_out_timeout,
session->id, conn->id, conn->state);
/* XXX: error handle */
__conn_error_handle(session, conn);
}
static void conn_send_nop_out(void *data)
{
iscsi_conn_t *conn = data;
/*
* we cannot start new request during logout and the logout timer
* will figure things out.
*/
if (conn->state == ISCSI_CONN_STATE_IN_LOGOUT)
return;
__send_nopout(conn);
actor_timer(&conn->nop_out_timer, conn->noop_out_timeout,
conn_nop_out_timeout, conn);
conn_debug(3, conn, "noop out timeout timer %p start, timeout %d",
&conn->nop_out_timer, conn->noop_out_timeout);
}
void free_initiator(void)
{
struct iscsi_transport *t;
iscsi_session_t *session, *tmp;
list_for_each_entry(t, &transports, list) {
list_for_each_entry_safe(session, tmp, &t->sessions, list) {
list_del(&session->list);
iscsi_flush_context_pool(session);
session_release(session);
}
}