/
iSCSISessionManager.c
219 lines (191 loc) · 9.45 KB
/
iSCSISessionManager.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
/*
* Copyright (c) 2016, Nareg Sinenian
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "iSCSISessionManager.h"
#include "iSCSISession.h"
#include "iSCSIHBAInterface.h"
/*! Name of the initiator. */
CFStringRef kiSCSIInitiatorIQN = CFSTR("iqn.2015-01.com.localhost");
/*! Alias of the initiator. */
CFStringRef kiSCSIInitiatorAlias = CFSTR("default");
struct __iSCSISessionManager
{
CFAllocatorRef allocator;
iSCSIHBAInterfaceRef hbaInterface;
iSCSISessionManagerCallBacks callbacks;
CFStringRef initiatorName;
CFStringRef initiatorAlias;
};
/*! This function is called handle session or connection network timeouts.
* When a timeout occurs the kernel deactivates the session and connection.
* The session layer (this layer) must release the connection after propogating
* the notification onto the user of the session manager. */
void iSCSIHBANotificationTimeoutMessageHandler(iSCSISessionManagerRef managerRef,
iSCSIHBANotificationMessage * msg)
{
// Retrieve the target name and portal address associated with
// the timeout. Pass information along to pre-designated runloop
// so that clients of this layer can act
iSCSITargetRef target = iSCSISessionCopyTargetForId(managerRef,msg->sessionId);
iSCSIPortalRef portal = iSCSISessionCopyPortalForConnectionId(managerRef,msg->sessionId,msg->connectionId);
// Release the stale session/connection
iSCSIHBAInterfaceReleaseConnection(managerRef->hbaInterface,msg->sessionId,msg->connectionId);
// Call user-defined callback function if one exists
if(managerRef->callbacks.timeoutCallback)
managerRef->callbacks.timeoutCallback(target,portal);
iSCSITargetRelease(target);
iSCSIPortalRelease(portal);
}
/*! Called to handle asynchronous events that involve dropped sessions, connections,
* logout requests and parameter negotiation. This function is not called for asynchronous
* SCSI messages or vendor-specific messages. */
void iSCSIHBANotificationAsyncMessageHandler(iSCSISessionManagerRef managerRef,
iSCSIHBANotificationAsyncMessage * msg)
{
enum iSCSIPDUAsyncMsgEvent asyncEvent = (enum iSCSIPDUAsyncMsgEvent)msg->asyncEvent;
enum iSCSILogoutStatusCode statusCode;
switch (asyncEvent) {
// We are required to issue a logout request
case kiSCSIPDUAsyncMsgLogout:
iSCSISessionRemoveConnection(managerRef,msg->sessionId,msg->connectionId,&statusCode);
break;
// We have been asked to re-negotiate parameters for this connection
// (this is currently unsupported and we logout)
case kiSCSIPDUAsyncMsgNegotiateParams:
iSCSISessionRemoveConnection(managerRef,msg->sessionId,msg->connectionId,&statusCode);
break;
default:
break;
}
}
static void iSCSIHBANotificationHandler(iSCSIHBAInterfaceRef interface,
enum iSCSIHBANotificationTypes type,
iSCSIHBANotificationMessage * msg,void * info)
{
iSCSISessionManagerRef managerRef = (iSCSISessionManagerRef)info;
// Process an asynchronous message
switch(type)
{
// The kernel received an iSCSI asynchronous event message
case kiSCSIHBANotificationAsyncMessage:
iSCSIHBANotificationAsyncMessageHandler(managerRef,(iSCSIHBANotificationAsyncMessage *)msg);
break;
case kiSCSIHBANotificationTimeout:
iSCSIHBANotificationTimeoutMessageHandler(managerRef,(iSCSIHBANotificationMessage *)msg);
break;
case kiSCSIHBANotificationTerminate: break;
default: break;
};
}
/*! Call to initialize iSCSI session management functions. This function will
* initialize the kernel layer after which other session-related functions
* may be called.
* @param rl the runloop to use for executing session-related functions.
* @return an error code indicating the result of the operation. */
iSCSISessionManagerRef iSCSISessionManagerCreate(CFAllocatorRef allocator,
iSCSISessionManagerCallBacks callbacks)
{
iSCSISessionManagerRef managerRef = CFAllocatorAllocate(allocator,sizeof(struct __iSCSISessionManager),0);
iSCSIHBANotificationContext notifyContext;
notifyContext.version = 0;
notifyContext.info = managerRef;
notifyContext.release = 0;
notifyContext.retain = 0;
notifyContext.copyDescription = 0;
iSCSIHBAInterfaceRef interface = iSCSIHBAInterfaceCreate(allocator,iSCSIHBANotificationHandler,¬ifyContext);
if(interface) {
managerRef->allocator = allocator;
managerRef->hbaInterface = interface;
managerRef->callbacks = callbacks;
managerRef->initiatorName = kiSCSIInitiatorIQN;
managerRef->initiatorAlias = kiSCSIInitiatorAlias;
}
else {
CFAllocatorDeallocate(allocator,managerRef);
managerRef = NULL;
}
return managerRef;
}
/*! Called to cleanup kernel resources used by the iSCSI session management
* functions. This function will close any connections to the kernel
* and stop processing messages related to the kernel.
* @param managerRef an instance of an iSCSISessionManagerRef. */
void iSCSISessionManagerRelease(iSCSISessionManagerRef managerRef)
{
CFAllocatorDeallocate(managerRef->allocator,managerRef);
}
/*! Creates a runloop source used to run the callback functions associated
* with the session manager reference. */
/*! Schedules execution of various tasks, including handling of kernel notifications
* on for the specified interface instance over the designated runloop.
* @param managerRef an instance of an iSCSISessionManagerRef.
* @param runLoop the runloop to schedule
* @param runLoopMode the execution mode for the runLoop. */
void iSCSISessionManagerScheduleWithRunLoop(iSCSISessionManagerRef managerRef,
CFRunLoopRef runLoop,
CFStringRef runLoopMode)
{
iSCSIHBAInterfaceScheduleWithRunloop(managerRef->hbaInterface,runLoop,runLoopMode);
}
/*! Unschedules execution of various tasks, including handling of session notifications
* on for the specified interface instance over the designated runloop.
* @param managerRef an instance of an iSCSISessionManagerRef.
* @param runLoop the runloop to schedule
* @param runLoopMode the execution mode for the runLoop. */
void iSCSISessionManagerUnscheduleWithRunloop(iSCSISessionManagerRef managerRef,
CFRunLoopRef runLoop,
CFStringRef runLoopMode)
{
iSCSIHBAInterfaceScheduleWithRunloop(managerRef->hbaInterface,runLoop,runLoopMode);
}
/*! Returns a reference to the underlying HBA interface instance.
* @param managerRef an instance of an iSCSISessionManagerRef.
* @return a reference to the underlying HBA interface instance. */
iSCSIHBAInterfaceRef iSCSISessionManagerGetHBAInterface(iSCSISessionManagerRef managerRef)
{
return managerRef->hbaInterface;
}
/*! Sets the initiator name to use for new sessions. This is the IQN-format name that is
* exchanged with a target during negotiation.
* @param managerRef an instance of an iSCSISessionManagerRef.
* @param initiatorIQN the initiator name. */
void iSCSISessionManagerSetInitiatorName(iSCSISessionManagerRef managerRef,
CFStringRef initiatorIQN)
{
CFRelease(managerRef->initiatorName);
managerRef->initiatorName = CFStringCreateCopy(kCFAllocatorDefault,initiatorIQN);
}
/*! Sets the initiator alias to use for new sessions. This is the IQN-format alias that is
* exchanged with a target during negotiation.
* @param managerRef an instance of an iSCSISessionManagerRef.
* @param initiatorAlias the initiator alias. */
void iSCSISessionManagerSetInitiatorAlias(iSCSISessionManagerRef managerRef,
CFStringRef initiatorAlias)
{
CFRelease(managerRef->initiatorAlias);
managerRef->initiatorAlias = CFStringCreateCopy(kCFAllocatorDefault,initiatorAlias);
}