iscsid_lists.c revision 1.1 1 1.1 agc /* $NetBSD: iscsid_lists.c,v 1.1 2011/10/23 21:11:23 agc Exp $ */
2 1.1 agc
3 1.1 agc /*-
4 1.1 agc * Copyright (c) 2005,2006,2011 The NetBSD Foundation, Inc.
5 1.1 agc * All rights reserved.
6 1.1 agc *
7 1.1 agc * This code is derived from software contributed to The NetBSD Foundation
8 1.1 agc * by Wasabi Systems, Inc.
9 1.1 agc *
10 1.1 agc * Redistribution and use in source and binary forms, with or without
11 1.1 agc * modification, are permitted provided that the following conditions
12 1.1 agc * are met:
13 1.1 agc * 1. Redistributions of source code must retain the above copyright
14 1.1 agc * notice, this list of conditions and the following disclaimer.
15 1.1 agc * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 agc * notice, this list of conditions and the following disclaimer in the
17 1.1 agc * documentation and/or other materials provided with the distribution.
18 1.1 agc *
19 1.1 agc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 agc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 agc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 agc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 agc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 agc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 agc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 agc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 agc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 agc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 agc * POSSIBILITY OF SUCH DAMAGE.
30 1.1 agc */
31 1.1 agc
32 1.1 agc
33 1.1 agc #include "iscsid_globals.h"
34 1.1 agc
35 1.1 agc /* counter for initiator ID */
36 1.1 agc STATIC uint32_t initiator_id = 0;
37 1.1 agc
38 1.1 agc /* -------------------------------------------------------------------------- */
39 1.1 agc
40 1.1 agc /*#ifdef ISCSI_NOTHREAD */
41 1.1 agc #if 0
42 1.1 agc
43 1.1 agc /*
44 1.1 agc * verify_session:
45 1.1 agc * Verify that a specific session still exists, delete it if not.
46 1.1 agc *
47 1.1 agc * Parameter: The session pointer.
48 1.1 agc */
49 1.1 agc
50 1.1 agc STATIC void
51 1.1 agc verify_session(session_t * sess)
52 1.1 agc {
53 1.1 agc generic_entry_t *curr, *next;
54 1.1 agc int nosess = 0;
55 1.1 agc
56 1.1 agc for (curr = sess->connections.tqh_first; curr != NULL && !nosess; curr = next) {
57 1.1 agc next = curr->link.tqe_next;
58 1.1 agc nosess = verify_connection((connection_t *) curr) == ISCSI_STATUS_INVALID_SESSION_ID;
59 1.1 agc }
60 1.1 agc
61 1.1 agc if (!nosess && sess->num_connections)
62 1.1 agc return;
63 1.1 agc
64 1.1 agc TAILQ_REMOVE(&list[SESSION_LIST].list, &sess->entry, link);
65 1.1 agc list[SESSION_LIST].num_entries--;
66 1.1 agc
67 1.1 agc while ((curr = TAILQ_FIRST(&sess->connections)) != NULL) {
68 1.1 agc TAILQ_REMOVE(&sess->connections, curr, link);
69 1.1 agc free(curr);
70 1.1 agc }
71 1.1 agc free(sess);
72 1.1 agc }
73 1.1 agc
74 1.1 agc
75 1.1 agc /*
76 1.1 agc * verify_sessions:
77 1.1 agc * Verify that all sessions in the list still exist.
78 1.1 agc */
79 1.1 agc
80 1.1 agc void
81 1.1 agc verify_sessions(void)
82 1.1 agc {
83 1.1 agc generic_entry_t *curr, *next;
84 1.1 agc
85 1.1 agc for (curr = list[SESSION_LIST].list.tqh_first; curr != NULL; curr = next) {
86 1.1 agc next = curr->link.tqe_next;
87 1.1 agc verify_session((session_t *) curr);
88 1.1 agc }
89 1.1 agc }
90 1.1 agc
91 1.1 agc #endif
92 1.1 agc
93 1.1 agc /* -------------------------------------------------------------------------- */
94 1.1 agc
95 1.1 agc /*
96 1.1 agc * find_id:
97 1.1 agc * Find a list element by ID.
98 1.1 agc *
99 1.1 agc * Parameter: the list head and the ID to search for
100 1.1 agc *
101 1.1 agc * Returns: The pointer to the element (or NULL if not found)
102 1.1 agc */
103 1.1 agc
104 1.1 agc generic_entry_t *
105 1.1 agc find_id(generic_list_t * head, uint32_t id)
106 1.1 agc {
107 1.1 agc generic_entry_t *curr;
108 1.1 agc
109 1.1 agc if (!id)
110 1.1 agc return NULL;
111 1.1 agc
112 1.1 agc TAILQ_FOREACH(curr, head, link)
113 1.1 agc if (curr->sid.id == (int)id)
114 1.1 agc break;
115 1.1 agc
116 1.1 agc return curr;
117 1.1 agc }
118 1.1 agc
119 1.1 agc /*
120 1.1 agc * find_name:
121 1.1 agc * Find a list entry by name.
122 1.1 agc *
123 1.1 agc * Parameter: the list head and the symbolic name to search for
124 1.1 agc *
125 1.1 agc * Returns: The pointer to the entry (or NULL if not found)
126 1.1 agc */
127 1.1 agc
128 1.1 agc generic_entry_t *
129 1.1 agc find_name(generic_list_t * head, uint8_t * name)
130 1.1 agc {
131 1.1 agc generic_entry_t *curr;
132 1.1 agc
133 1.1 agc if (!*name)
134 1.1 agc return NULL;
135 1.1 agc
136 1.1 agc TAILQ_FOREACH(curr, head, link)
137 1.1 agc if (strcmp((char *)curr->sid.name, (char *)name) == 0)
138 1.1 agc break;
139 1.1 agc
140 1.1 agc return curr;
141 1.1 agc }
142 1.1 agc
143 1.1 agc
144 1.1 agc /*
145 1.1 agc * find_sym_id:
146 1.1 agc * Find a list entry by name or numeric id.
147 1.1 agc *
148 1.1 agc * Parameter: the list head and the symbolic id to search for
149 1.1 agc *
150 1.1 agc * Returns: The pointer to the entry (or NULL if not found)
151 1.1 agc */
152 1.1 agc
153 1.1 agc generic_entry_t *
154 1.1 agc find_sym_id(generic_list_t * head, iscsid_sym_id_t * sid)
155 1.1 agc {
156 1.1 agc
157 1.1 agc if (sid->id != 0)
158 1.1 agc return find_id(head, sid->id);
159 1.1 agc
160 1.1 agc return (sid->name[0]) ? find_name(head, sid->name) : NULL;
161 1.1 agc }
162 1.1 agc
163 1.1 agc
164 1.1 agc /*
165 1.1 agc * get_id:
166 1.1 agc * Get the numeric ID for a symbolic ID
167 1.1 agc *
168 1.1 agc * Parameter: the list head and the symbolic id
169 1.1 agc *
170 1.1 agc * Returns: The numeric ID (0 if not found)
171 1.1 agc */
172 1.1 agc
173 1.1 agc uint32_t
174 1.1 agc get_id(generic_list_t * head, iscsid_sym_id_t * sid)
175 1.1 agc {
176 1.1 agc generic_entry_t *ent;
177 1.1 agc
178 1.1 agc if (sid->id != 0)
179 1.1 agc return sid->id;
180 1.1 agc
181 1.1 agc ent = find_name(head, sid->name);
182 1.1 agc return (ent != NULL) ? ent->sid.id : 0;
183 1.1 agc }
184 1.1 agc
185 1.1 agc
186 1.1 agc /*
187 1.1 agc * find_target_name:
188 1.1 agc * Find a target by TargetName.
189 1.1 agc *
190 1.1 agc * Parameter: the target name
191 1.1 agc *
192 1.1 agc * Returns: The pointer to the target (or NULL if not found)
193 1.1 agc */
194 1.1 agc
195 1.1 agc target_t *
196 1.1 agc find_target(iscsid_list_kind_t lst, iscsid_sym_id_t * sid)
197 1.1 agc {
198 1.1 agc target_t *targ;
199 1.1 agc
200 1.1 agc if ((targ = (target_t *)find_sym_id (&list [lst].list, sid)) != NULL)
201 1.1 agc return targ;
202 1.1 agc if (lst == TARGET_LIST) {
203 1.1 agc portal_t *portal;
204 1.1 agc
205 1.1 agc if ((portal = find_portal (sid)) != NULL)
206 1.1 agc return portal->target;
207 1.1 agc }
208 1.1 agc return NULL;
209 1.1 agc }
210 1.1 agc
211 1.1 agc
212 1.1 agc /*
213 1.1 agc * find_target_name:
214 1.1 agc * Find a target by TargetName.
215 1.1 agc *
216 1.1 agc * Parameter: the target name
217 1.1 agc *
218 1.1 agc * Returns: The pointer to the target (or NULL if not found)
219 1.1 agc */
220 1.1 agc
221 1.1 agc target_t *
222 1.1 agc find_TargetName(iscsid_list_kind_t lst, uint8_t * name)
223 1.1 agc {
224 1.1 agc generic_entry_t *curr;
225 1.1 agc
226 1.1 agc if (lst == PORTAL_LIST)
227 1.1 agc lst = TARGET_LIST;
228 1.1 agc
229 1.1 agc TAILQ_FOREACH(curr, &list[lst].list, link)
230 1.1 agc if (strcmp((char *)((target_t *) curr)->TargetName, (char *)name) == 0)
231 1.1 agc break;
232 1.1 agc
233 1.1 agc DEB(10, ("Find_TagetName returns %x\n", (int) curr));
234 1.1 agc
235 1.1 agc return (target_t *) curr;
236 1.1 agc }
237 1.1 agc
238 1.1 agc
239 1.1 agc /*
240 1.1 agc * find_portal_by_addr:
241 1.1 agc * Find a Portal by Address.
242 1.1 agc *
243 1.1 agc * Parameter: the associated target, and the address
244 1.1 agc *
245 1.1 agc * Returns: The pointer to the portal (or NULL if not found)
246 1.1 agc */
247 1.1 agc
248 1.1 agc portal_t *
249 1.1 agc find_portal_by_addr(target_t * target, iscsi_portal_address_t * addr)
250 1.1 agc {
251 1.1 agc generic_entry_t *curr;
252 1.1 agc
253 1.1 agc TAILQ_FOREACH(curr, &list[PORTAL_LIST].list, link) {
254 1.1 agc DEB(10, ("Find_portal_by_addr - addr %s port %d target %x\n",
255 1.1 agc ((portal_t *) curr)->addr.address,
256 1.1 agc ((portal_t *) curr)->addr.port,
257 1.1 agc (int) ((portal_t *) curr)->target));
258 1.1 agc
259 1.1 agc if (strcmp((char *)((portal_t *) curr)->addr.address, (char *)addr->address) == 0 &&
260 1.1 agc (!addr->port || ((portal_t *) curr)->addr.port == addr->port) &&
261 1.1 agc ((portal_t *) curr)->target == target)
262 1.1 agc break;
263 1.1 agc }
264 1.1 agc
265 1.1 agc DEB(10, ("Find_portal_by_addr returns %x\n", (int) curr));
266 1.1 agc return (portal_t *) curr;
267 1.1 agc }
268 1.1 agc
269 1.1 agc
270 1.1 agc /*
271 1.1 agc * find_send_target_by_addr:
272 1.1 agc * Find a Send Target by Address.
273 1.1 agc *
274 1.1 agc * Parameter: the address
275 1.1 agc *
276 1.1 agc * Returns: The pointer to the portal (or NULL if not found)
277 1.1 agc */
278 1.1 agc
279 1.1 agc send_target_t *
280 1.1 agc find_send_target_by_addr(iscsi_portal_address_t * addr)
281 1.1 agc {
282 1.1 agc generic_entry_t *curr;
283 1.1 agc
284 1.1 agc TAILQ_FOREACH(curr, &list[SEND_TARGETS_LIST].list, link) {
285 1.1 agc if (strcmp((char *)((send_target_t *) curr)->addr.address, (char *)addr->address) == 0 &&
286 1.1 agc (!addr->port || ((send_target_t *) curr)->addr.port == addr->port))
287 1.1 agc break;
288 1.1 agc }
289 1.1 agc
290 1.1 agc DEB(10, ("Find_send_target_by_addr returns %x\n", (int) curr));
291 1.1 agc return (send_target_t *) curr;
292 1.1 agc }
293 1.1 agc
294 1.1 agc
295 1.1 agc /*
296 1.1 agc * get_list:
297 1.1 agc * Handle GET_LIST request: Return the list of IDs contained in the list.
298 1.1 agc *
299 1.1 agc * Parameter:
300 1.1 agc * par The request parameters.
301 1.1 agc * prsp Pointer to address of response buffer.
302 1.1 agc * prsp_temp Will be set to TRUE if buffer was allocated, FALSE
303 1.1 agc * for static buffer.
304 1.1 agc */
305 1.1 agc
306 1.1 agc void
307 1.1 agc get_list(iscsid_get_list_req_t * par, iscsid_response_t ** prsp, int *prsp_temp)
308 1.1 agc {
309 1.1 agc iscsid_get_list_rsp_t *res;
310 1.1 agc iscsid_response_t *rsp = *prsp;
311 1.1 agc int num;
312 1.1 agc uint32_t *idp;
313 1.1 agc generic_list_t *plist;
314 1.1 agc generic_entry_t *curr;
315 1.1 agc
316 1.1 agc DEB(10, ("get_list, kind %d\n", par->list_kind));
317 1.1 agc
318 1.1 agc if (par->list_kind == SESSION_LIST)
319 1.1 agc LOCK_SESSIONS;
320 1.1 agc else if (par->list_kind >= NUM_DAEMON_LISTS) {
321 1.1 agc rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
322 1.1 agc return;
323 1.1 agc }
324 1.1 agc
325 1.1 agc plist = &list[par->list_kind].list;
326 1.1 agc num = list[par->list_kind].num_entries;
327 1.1 agc
328 1.1 agc if (!num) {
329 1.1 agc if (par->list_kind == SESSION_LIST)
330 1.1 agc UNLOCK_SESSIONS;
331 1.1 agc rsp->status = ISCSID_STATUS_LIST_EMPTY;
332 1.1 agc return;
333 1.1 agc }
334 1.1 agc
335 1.1 agc rsp = make_rsp(sizeof(iscsid_get_list_rsp_t) +
336 1.1 agc (num - 1) * sizeof(uint32_t), prsp, prsp_temp);
337 1.1 agc if (rsp == NULL) {
338 1.1 agc if (par->list_kind == SESSION_LIST)
339 1.1 agc UNLOCK_SESSIONS;
340 1.1 agc return;
341 1.1 agc }
342 1.1 agc /* copy the ID of all list entries */
343 1.1 agc res = (iscsid_get_list_rsp_t *) rsp->parameter;
344 1.1 agc res->num_entries = num;
345 1.1 agc idp = res->id;
346 1.1 agc
347 1.1 agc TAILQ_FOREACH(curr, plist, link)
348 1.1 agc * idp++ = curr->sid.id;
349 1.1 agc
350 1.1 agc if (par->list_kind == SESSION_LIST)
351 1.1 agc UNLOCK_SESSIONS;
352 1.1 agc }
353 1.1 agc
354 1.1 agc
355 1.1 agc /*
356 1.1 agc * search_list:
357 1.1 agc * Handle SEARCH_LIST request: Search the given list for the string or
358 1.1 agc * address.
359 1.1 agc * Note: Not all combinations of list and search type make sense.
360 1.1 agc *
361 1.1 agc * Parameter:
362 1.1 agc * par The request parameters.
363 1.1 agc * prsp Pointer to address of response buffer.
364 1.1 agc * prsp_temp Will be set to TRUE if buffer was allocated, FALSE
365 1.1 agc * for static buffer.
366 1.1 agc */
367 1.1 agc
368 1.1 agc void
369 1.1 agc search_list(iscsid_search_list_req_t * par, iscsid_response_t ** prsp,
370 1.1 agc int *prsp_temp)
371 1.1 agc {
372 1.1 agc iscsid_response_t *rsp = *prsp;
373 1.1 agc generic_entry_t *elem = NULL;
374 1.1 agc
375 1.1 agc DEB(10, ("search_list, list_kind %d, search_kind %d\n",
376 1.1 agc par->list_kind, par->search_kind));
377 1.1 agc
378 1.1 agc if (par->list_kind == SESSION_LIST)
379 1.1 agc LOCK_SESSIONS;
380 1.1 agc else if (par->list_kind >= NUM_DAEMON_LISTS) {
381 1.1 agc rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
382 1.1 agc return;
383 1.1 agc }
384 1.1 agc
385 1.1 agc if (!list[par->list_kind].num_entries) {
386 1.1 agc if (par->list_kind == SESSION_LIST)
387 1.1 agc UNLOCK_SESSIONS;
388 1.1 agc rsp->status = ISCSID_STATUS_NOT_FOUND;
389 1.1 agc return;
390 1.1 agc }
391 1.1 agc
392 1.1 agc switch (par->search_kind) {
393 1.1 agc case FIND_ID:
394 1.1 agc elem = find_id(&list[par->list_kind].list, par->intval);
395 1.1 agc break;
396 1.1 agc
397 1.1 agc case FIND_NAME:
398 1.1 agc elem = find_name(&list[par->list_kind].list, par->strval);
399 1.1 agc break;
400 1.1 agc
401 1.1 agc case FIND_TARGET_NAME:
402 1.1 agc switch (par->list_kind) {
403 1.1 agc case TARGET_LIST:
404 1.1 agc case PORTAL_LIST:
405 1.1 agc case SEND_TARGETS_LIST:
406 1.1 agc elem = (generic_entry_t *) find_TargetName(par->list_kind,
407 1.1 agc par->strval);
408 1.1 agc break;
409 1.1 agc
410 1.1 agc case SESSION_LIST:
411 1.1 agc TAILQ_FOREACH(elem, &list[SESSION_LIST].list, link)
412 1.1 agc if (strcmp((char *)((session_t *) elem)->target.TargetName,
413 1.1 agc (char *)par->strval) == 0)
414 1.1 agc break;
415 1.1 agc break;
416 1.1 agc
417 1.1 agc default:
418 1.1 agc rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
419 1.1 agc break;
420 1.1 agc }
421 1.1 agc break;
422 1.1 agc
423 1.1 agc case FIND_ADDRESS:
424 1.1 agc switch (par->list_kind) {
425 1.1 agc case PORTAL_LIST:
426 1.1 agc TAILQ_FOREACH(elem, &list[PORTAL_LIST].list, link) {
427 1.1 agc if (strcmp((char *)((portal_t *) elem)->addr.address, (char *)par->strval) == 0 &&
428 1.1 agc (!par->intval ||
429 1.1 agc ((portal_t *) elem)->addr.port == par->intval))
430 1.1 agc break;
431 1.1 agc }
432 1.1 agc break;
433 1.1 agc
434 1.1 agc case SEND_TARGETS_LIST:
435 1.1 agc TAILQ_FOREACH(elem, &list[SEND_TARGETS_LIST].list, link) {
436 1.1 agc if (strcmp((char *)((send_target_t *) elem)->addr.address,
437 1.1 agc (char *)par->strval) == 0 &&
438 1.1 agc (!par->intval ||
439 1.1 agc ((send_target_t *) elem)->addr.port == par->intval))
440 1.1 agc break;
441 1.1 agc }
442 1.1 agc break;
443 1.1 agc
444 1.1 agc case ISNS_LIST:
445 1.1 agc TAILQ_FOREACH(elem, &list[ISNS_LIST].list, link) {
446 1.1 agc if (strcmp((char *)((isns_t *) elem)->address, (char *)par->strval) == 0 &&
447 1.1 agc (!par->intval || ((isns_t *) elem)->port == par->intval))
448 1.1 agc break;
449 1.1 agc }
450 1.1 agc break;
451 1.1 agc
452 1.1 agc default:
453 1.1 agc rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
454 1.1 agc break;
455 1.1 agc }
456 1.1 agc break;
457 1.1 agc
458 1.1 agc default:
459 1.1 agc rsp->status = ISCSID_STATUS_INVALID_PARAMETER;
460 1.1 agc return;
461 1.1 agc }
462 1.1 agc
463 1.1 agc if (elem == NULL) {
464 1.1 agc if (par->list_kind == SESSION_LIST)
465 1.1 agc UNLOCK_SESSIONS;
466 1.1 agc rsp->status = ISCSID_STATUS_NOT_FOUND;
467 1.1 agc return;
468 1.1 agc }
469 1.1 agc
470 1.1 agc rsp = make_rsp(sizeof(iscsid_sym_id_t), prsp, prsp_temp);
471 1.1 agc if (rsp == NULL) {
472 1.1 agc if (par->list_kind == SESSION_LIST)
473 1.1 agc UNLOCK_SESSIONS;
474 1.1 agc return;
475 1.1 agc }
476 1.1 agc
477 1.1 agc (void) memcpy(rsp->parameter, &elem->sid, sizeof(elem->sid));
478 1.1 agc if (par->list_kind == SESSION_LIST)
479 1.1 agc UNLOCK_SESSIONS;
480 1.1 agc }
481 1.1 agc
482 1.1 agc
483 1.1 agc /*
484 1.1 agc * get_session_list:
485 1.1 agc * Handle GET_SESSION_LIST request: Return a list of sessions complete
486 1.1 agc * with basic session info.
487 1.1 agc *
488 1.1 agc * Parameter:
489 1.1 agc * prsp Pointer to address of response buffer.
490 1.1 agc * prsp_temp Will be set to TRUE if buffer was allocated, FALSE
491 1.1 agc * for static buffer.
492 1.1 agc */
493 1.1 agc
494 1.1 agc void
495 1.1 agc get_session_list(iscsid_response_t ** prsp, int *prsp_temp)
496 1.1 agc {
497 1.1 agc iscsid_get_session_list_rsp_t *res;
498 1.1 agc iscsid_response_t *rsp = *prsp;
499 1.1 agc iscsid_session_list_entry_t *ent;
500 1.1 agc generic_list_t *plist;
501 1.1 agc generic_entry_t *curr;
502 1.1 agc session_t *sess;
503 1.1 agc connection_t *conn;
504 1.1 agc int num;
505 1.1 agc
506 1.1 agc DEB(10, ("get_session_list\n"));
507 1.1 agc
508 1.1 agc LOCK_SESSIONS;
509 1.1 agc plist = &list[SESSION_LIST].list;
510 1.1 agc num = list[SESSION_LIST].num_entries;
511 1.1 agc
512 1.1 agc if (!num) {
513 1.1 agc UNLOCK_SESSIONS;
514 1.1 agc rsp->status = ISCSID_STATUS_LIST_EMPTY;
515 1.1 agc return;
516 1.1 agc }
517 1.1 agc
518 1.1 agc rsp = make_rsp(sizeof(iscsid_get_session_list_rsp_t) +
519 1.1 agc (num - 1) * sizeof(iscsid_session_list_entry_t),
520 1.1 agc prsp, prsp_temp);
521 1.1 agc if (rsp == NULL) {
522 1.1 agc UNLOCK_SESSIONS;
523 1.1 agc return;
524 1.1 agc }
525 1.1 agc /* copy the ID of all list entries */
526 1.1 agc res = (iscsid_get_session_list_rsp_t *) rsp->parameter;
527 1.1 agc res->num_entries = num;
528 1.1 agc ent = res->session;
529 1.1 agc
530 1.1 agc TAILQ_FOREACH(curr, plist, link) {
531 1.1 agc sess = (session_t *) curr;
532 1.1 agc conn = (connection_t *) TAILQ_FIRST(&sess->connections);
533 1.1 agc
534 1.1 agc ent->session_id = sess->entry.sid;
535 1.1 agc ent->first_connection_id = conn->entry.sid.id;
536 1.1 agc ent->num_connections = sess->num_connections;
537 1.1 agc ent->portal_id = conn->portal.sid.id;
538 1.1 agc ent->initiator_id = conn->initiator_id;
539 1.1 agc ent++;
540 1.1 agc }
541 1.1 agc UNLOCK_SESSIONS;
542 1.1 agc }
543 1.1 agc
544 1.1 agc /*
545 1.1 agc * get_connection_list:
546 1.1 agc * Handle GET_CONNECTION_LIST request: Return a list of connections
547 1.1 agc * for a session.
548 1.1 agc *
549 1.1 agc * Parameter:
550 1.1 agc * prsp Pointer to address of response buffer.
551 1.1 agc * prsp_temp Will be set to TRUE if buffer was allocated, FALSE
552 1.1 agc * for static buffer.
553 1.1 agc */
554 1.1 agc
555 1.1 agc void
556 1.1 agc get_connection_list(iscsid_sym_id_t *req, iscsid_response_t **prsp,
557 1.1 agc int *prsp_temp)
558 1.1 agc {
559 1.1 agc iscsid_get_connection_list_rsp_t *res;
560 1.1 agc iscsid_response_t *rsp = *prsp;
561 1.1 agc iscsid_connection_list_entry_t *ent;
562 1.1 agc generic_entry_t *curr;
563 1.1 agc session_t *sess;
564 1.1 agc connection_t *conn;
565 1.1 agc int num;
566 1.1 agc
567 1.1 agc DEB(10, ("get_connection_list\n"));
568 1.1 agc
569 1.1 agc LOCK_SESSIONS;
570 1.1 agc if ((sess = find_session(req)) == NULL) {
571 1.1 agc UNLOCK_SESSIONS;
572 1.1 agc rsp->status = ISCSID_STATUS_INVALID_SESSION_ID;
573 1.1 agc return;
574 1.1 agc }
575 1.1 agc
576 1.1 agc num = sess->num_connections;
577 1.1 agc rsp = make_rsp(sizeof(iscsid_get_connection_list_rsp_t) +
578 1.1 agc (num - 1) * sizeof(iscsid_connection_list_entry_t),
579 1.1 agc prsp, prsp_temp);
580 1.1 agc if (rsp == NULL) {
581 1.1 agc UNLOCK_SESSIONS;
582 1.1 agc return;
583 1.1 agc }
584 1.1 agc /* copy the ID of all list entries */
585 1.1 agc res = (iscsid_get_connection_list_rsp_t *) rsp->parameter;
586 1.1 agc res->num_connections = num;
587 1.1 agc ent = res->connection;
588 1.1 agc
589 1.1 agc TAILQ_FOREACH(curr, &sess->connections, link) {
590 1.1 agc conn = (connection_t *) curr;
591 1.1 agc ent->connection_id = conn->entry.sid;
592 1.1 agc ent->target_portal_id = conn->portal.sid;
593 1.1 agc ent->target_portal = conn->portal.addr;
594 1.1 agc ent++;
595 1.1 agc }
596 1.1 agc UNLOCK_SESSIONS;
597 1.1 agc }
598 1.1 agc
599 1.1 agc
600 1.1 agc /*
601 1.1 agc * get_connection_info:
602 1.1 agc * Handle GET_CONNECTION_INFO request: Return information about a connection
603 1.1 agc *
604 1.1 agc * Parameter:
605 1.1 agc * par The request parameters.
606 1.1 agc * prsp Pointer to address of response buffer.
607 1.1 agc * prsp_temp Will be set to TRUE if buffer was allocated, FALSE
608 1.1 agc * for static buffer.
609 1.1 agc */
610 1.1 agc
611 1.1 agc void
612 1.1 agc get_connection_info(iscsid_get_connection_info_req_t * req,
613 1.1 agc iscsid_response_t ** prsp, int *prsp_temp)
614 1.1 agc {
615 1.1 agc iscsid_get_connection_info_rsp_t *res;
616 1.1 agc iscsid_response_t *rsp = *prsp;
617 1.1 agc session_t *sess;
618 1.1 agc connection_t *conn;
619 1.1 agc initiator_t *init = NULL;
620 1.1 agc
621 1.1 agc DEB(10, ("get_connection_info, session %d, connection %d\n",
622 1.1 agc req->session_id.id, req->connection_id.id));
623 1.1 agc
624 1.1 agc LOCK_SESSIONS;
625 1.1 agc if ((sess = find_session(&req->session_id)) == NULL) {
626 1.1 agc UNLOCK_SESSIONS;
627 1.1 agc rsp->status = ISCSID_STATUS_INVALID_SESSION_ID;
628 1.1 agc return;
629 1.1 agc }
630 1.1 agc if (!req->connection_id.id && !req->connection_id.name[0]) {
631 1.1 agc conn = (connection_t *) TAILQ_FIRST(&sess->connections);
632 1.1 agc } else if ((conn = find_connection(sess, &req->connection_id)) == NULL) {
633 1.1 agc UNLOCK_SESSIONS;
634 1.1 agc rsp->status = ISCSID_STATUS_INVALID_CONNECTION_ID;
635 1.1 agc return;
636 1.1 agc }
637 1.1 agc
638 1.1 agc rsp = make_rsp(sizeof(iscsid_get_connection_info_rsp_t), prsp, prsp_temp);
639 1.1 agc if (rsp == NULL) {
640 1.1 agc UNLOCK_SESSIONS;
641 1.1 agc return;
642 1.1 agc }
643 1.1 agc
644 1.1 agc if (conn->initiator_id)
645 1.1 agc init = find_initiator_id(conn->initiator_id);
646 1.1 agc
647 1.1 agc res = (iscsid_get_connection_info_rsp_t *) rsp->parameter;
648 1.1 agc
649 1.1 agc res->session_id = sess->entry.sid;
650 1.1 agc res->connection_id = conn->entry.sid;
651 1.1 agc res->target_portal_id = conn->portal.sid;
652 1.1 agc res->target_portal = conn->portal.addr;
653 1.1 agc strlcpy((char *)res->TargetName, (char *)conn->target.TargetName,
654 1.1 agc sizeof(res->TargetName));
655 1.1 agc strlcpy((char *)res->TargetAlias, (char *)conn->target.TargetAlias,
656 1.1 agc sizeof(res->TargetAlias));
657 1.1 agc if (init != NULL) {
658 1.1 agc res->initiator_id = init->entry.sid;
659 1.1 agc strlcpy((char *)res->initiator_address, (char *)init->address,
660 1.1 agc sizeof(res->initiator_address));
661 1.1 agc }
662 1.1 agc UNLOCK_SESSIONS;
663 1.1 agc }
664 1.1 agc
665 1.1 agc /* ------------------------------------------------------------------------- */
666 1.1 agc
667 1.1 agc /*
668 1.1 agc * find_initator_by_addr:
669 1.1 agc * Find an Initiator Portal by Address.
670 1.1 agc *
671 1.1 agc * Parameter: the address
672 1.1 agc *
673 1.1 agc * Returns: The pointer to the portal (or NULL if not found)
674 1.1 agc */
675 1.1 agc
676 1.1 agc STATIC initiator_t *
677 1.1 agc find_initiator_by_addr(uint8_t * addr)
678 1.1 agc {
679 1.1 agc generic_entry_t *curr;
680 1.1 agc
681 1.1 agc TAILQ_FOREACH(curr, &list[INITIATOR_LIST].list, link)
682 1.1 agc if (strcmp((char *)((initiator_t *) curr)->address, (char *)addr) == 0)
683 1.1 agc break;
684 1.1 agc
685 1.1 agc DEB(9, ("Find_initiator_by_addr returns %x\n", (int) curr));
686 1.1 agc return (initiator_t *) curr;
687 1.1 agc }
688 1.1 agc
689 1.1 agc
690 1.1 agc /*
691 1.1 agc * add_initiator_portal:
692 1.1 agc * Add an initiator portal.
693 1.1 agc *
694 1.1 agc * Parameter:
695 1.1 agc * par The request parameters.
696 1.1 agc * prsp Pointer to address of response buffer.
697 1.1 agc * prsp_temp Will be set to TRUE if buffer was allocated, FALSE
698 1.1 agc * for static buffer.
699 1.1 agc */
700 1.1 agc
701 1.1 agc void
702 1.1 agc add_initiator_portal(iscsid_add_initiator_req_t *par, iscsid_response_t **prsp,
703 1.1 agc int *prsp_temp)
704 1.1 agc {
705 1.1 agc iscsid_add_initiator_rsp_t *res;
706 1.1 agc iscsid_response_t *rsp = *prsp;
707 1.1 agc initiator_t *init;
708 1.1 agc
709 1.1 agc DEB(9, ("AddInitiatorPortal '%s' (name '%s')\n", par->address, par->name));
710 1.1 agc
711 1.1 agc if (find_initiator_by_addr(par->address) != NULL) {
712 1.1 agc rsp->status = ISCSID_STATUS_DUPLICATE_ENTRY;
713 1.1 agc return;
714 1.1 agc }
715 1.1 agc
716 1.1 agc if (find_initiator_name(par->name) != NULL) {
717 1.1 agc rsp->status = ISCSID_STATUS_DUPLICATE_NAME;
718 1.1 agc return;
719 1.1 agc }
720 1.1 agc
721 1.1 agc if ((init = calloc(1, sizeof(*init))) == NULL) {
722 1.1 agc rsp->status = ISCSID_STATUS_NO_RESOURCES;
723 1.1 agc return;
724 1.1 agc }
725 1.1 agc
726 1.1 agc DEB(9, ("AddInitiatorPortal initiator_id = %d\n", initiator_id));
727 1.1 agc
728 1.1 agc for (initiator_id++;
729 1.1 agc !initiator_id || find_initiator_id(initiator_id) != NULL;)
730 1.1 agc initiator_id++;
731 1.1 agc
732 1.1 agc init->entry.sid.id = initiator_id;
733 1.1 agc strlcpy((char *)init->entry.sid.name, (char *)par->name, sizeof(init->entry.sid.name));
734 1.1 agc strlcpy((char *)init->address, (char *)par->address, sizeof(init->address));
735 1.1 agc
736 1.1 agc rsp = make_rsp(sizeof(iscsid_add_initiator_rsp_t), prsp, prsp_temp);
737 1.1 agc if (rsp == NULL)
738 1.1 agc return;
739 1.1 agc
740 1.1 agc LOCK_SESSIONS;
741 1.1 agc TAILQ_INSERT_TAIL(&list[INITIATOR_LIST].list, &init->entry, link);
742 1.1 agc list[INITIATOR_LIST].num_entries++;
743 1.1 agc UNLOCK_SESSIONS;
744 1.1 agc
745 1.1 agc res = (iscsid_add_initiator_rsp_t *) rsp->parameter;
746 1.1 agc res->portal_id = init->entry.sid.id;
747 1.1 agc }
748 1.1 agc
749 1.1 agc
750 1.1 agc /*
751 1.1 agc * remove_initiator_portal:
752 1.1 agc * Handle REMOVE_INITIATOR request: Removes an initiator entry.
753 1.1 agc *
754 1.1 agc * Parameter:
755 1.1 agc * par The request parameter containing the ID.
756 1.1 agc *
757 1.1 agc * Returns: status
758 1.1 agc */
759 1.1 agc
760 1.1 agc uint32_t
761 1.1 agc remove_initiator_portal(iscsid_sym_id_t * par)
762 1.1 agc {
763 1.1 agc initiator_t *init;
764 1.1 agc
765 1.1 agc if ((init = find_initiator(par)) == NULL)
766 1.1 agc return ISCSID_STATUS_INVALID_INITIATOR_ID;
767 1.1 agc
768 1.1 agc LOCK_SESSIONS;
769 1.1 agc list[INITIATOR_LIST].num_entries--;
770 1.1 agc
771 1.1 agc TAILQ_REMOVE(&list[INITIATOR_LIST].list, &init->entry, link);
772 1.1 agc UNLOCK_SESSIONS;
773 1.1 agc
774 1.1 agc free(init);
775 1.1 agc
776 1.1 agc return ISCSID_STATUS_SUCCESS;
777 1.1 agc }
778 1.1 agc
779 1.1 agc
780 1.1 agc
781 1.1 agc /*
782 1.1 agc * get_initiator_portal:
783 1.1 agc * Handle GET_INITIATOR_PORTAL request: Return information about the given
784 1.1 agc * initiator portal.
785 1.1 agc *
786 1.1 agc * Parameter:
787 1.1 agc * par The request parameters.
788 1.1 agc * prsp Pointer to address of response buffer.
789 1.1 agc * prsp_temp Will be set to TRUE if buffer was allocated, FALSE
790 1.1 agc * for static buffer.
791 1.1 agc */
792 1.1 agc
793 1.1 agc void
794 1.1 agc get_initiator_portal(iscsid_sym_id_t *par, iscsid_response_t **prsp,
795 1.1 agc int *prsp_temp)
796 1.1 agc {
797 1.1 agc iscsid_get_initiator_rsp_t *res;
798 1.1 agc iscsid_response_t *rsp = *prsp;
799 1.1 agc initiator_t *init;
800 1.1 agc
801 1.1 agc DEB(10, ("get_initiator_portal, id %d (%s)\n", par->id, par->name));
802 1.1 agc
803 1.1 agc if ((init = find_initiator(par)) == NULL) {
804 1.1 agc rsp->status = ISCSID_STATUS_INVALID_INITIATOR_ID;
805 1.1 agc return;
806 1.1 agc }
807 1.1 agc
808 1.1 agc rsp = make_rsp(sizeof(iscsid_get_initiator_rsp_t), prsp, prsp_temp);
809 1.1 agc if (rsp == NULL)
810 1.1 agc return;
811 1.1 agc
812 1.1 agc res = (iscsid_get_initiator_rsp_t *) rsp->parameter;
813 1.1 agc res->portal_id = init->entry.sid;
814 1.1 agc strlcpy((char *)res->address, (char *)init->address, sizeof(res->address));
815 1.1 agc }
816 1.1 agc
817 1.1 agc
818 1.1 agc /*
819 1.1 agc * select_initiator:
820 1.1 agc * Select the initiator portal to use.
821 1.1 agc * Selects the portal with the least number of active connections.
822 1.1 agc *
823 1.1 agc * Returns:
824 1.1 agc * Pointer to the portal, NULL if no portals are defined.
825 1.1 agc *
826 1.1 agc * NOTE: Called with session list locked, so don't lock again.
827 1.1 agc */
828 1.1 agc
829 1.1 agc initiator_t *
830 1.1 agc select_initiator(void)
831 1.1 agc {
832 1.1 agc generic_entry_t *curr;
833 1.1 agc initiator_t *imin = NULL;
834 1.1 agc uint32_t ccnt = 64 * 1024; /* probably not more than 64k connections... */
835 1.1 agc
836 1.1 agc if (!list[INITIATOR_LIST].num_entries)
837 1.1 agc return NULL;
838 1.1 agc
839 1.1 agc TAILQ_FOREACH(curr, &list[INITIATOR_LIST].list, link) {
840 1.1 agc if ((((initiator_t *) curr)->active_connections < ccnt)) {
841 1.1 agc ccnt = ((initiator_t *) curr)->active_connections;
842 1.1 agc imin = (initiator_t *) curr;
843 1.1 agc }
844 1.1 agc }
845 1.1 agc return imin;
846 1.1 agc }
847 1.1 agc
848 1.1 agc /* ------------------------------------------------------------------------- */
849 1.1 agc
850 1.1 agc /*
851 1.1 agc * event_kill_session:
852 1.1 agc * Handle SESSION_TERMINATED event: Remove session and all associated
853 1.1 agc * connections.
854 1.1 agc *
855 1.1 agc * Parameter:
856 1.1 agc * sid Session ID
857 1.1 agc */
858 1.1 agc
859 1.1 agc void
860 1.1 agc event_kill_session(uint32_t sid)
861 1.1 agc {
862 1.1 agc session_t *sess;
863 1.1 agc connection_t *conn;
864 1.1 agc portal_t *portal;
865 1.1 agc initiator_t *init;
866 1.1 agc
867 1.1 agc LOCK_SESSIONS;
868 1.1 agc
869 1.1 agc sess = find_session_id(sid);
870 1.1 agc
871 1.1 agc if (sess == NULL) {
872 1.1 agc UNLOCK_SESSIONS;
873 1.1 agc return;
874 1.1 agc }
875 1.1 agc
876 1.1 agc TAILQ_REMOVE(&list[SESSION_LIST].list, &sess->entry, link);
877 1.1 agc list[SESSION_LIST].num_entries--;
878 1.1 agc
879 1.1 agc UNLOCK_SESSIONS;
880 1.1 agc
881 1.1 agc while ((conn = (connection_t *) TAILQ_FIRST(&sess->connections)) != NULL) {
882 1.1 agc TAILQ_REMOVE(&sess->connections, &conn->entry, link);
883 1.1 agc
884 1.1 agc portal = find_portal_id(conn->portal.sid.id);
885 1.1 agc if (portal != NULL)
886 1.1 agc portal->active_connections--;
887 1.1 agc
888 1.1 agc init = find_initiator_id(conn->initiator_id);
889 1.1 agc if (init != NULL)
890 1.1 agc init->active_connections--;
891 1.1 agc
892 1.1 agc free(conn);
893 1.1 agc }
894 1.1 agc free(sess);
895 1.1 agc }
896 1.1 agc
897 1.1 agc
898 1.1 agc /*
899 1.1 agc * event_kill_connection:
900 1.1 agc * Handle CONNECTION_TERMINATED event: Remove connection from session.
901 1.1 agc *
902 1.1 agc * Parameter:
903 1.1 agc * sid Session ID
904 1.1 agc * cid Connection ID
905 1.1 agc */
906 1.1 agc
907 1.1 agc void
908 1.1 agc event_kill_connection(uint32_t sid, uint32_t cid)
909 1.1 agc {
910 1.1 agc session_t *sess;
911 1.1 agc connection_t *conn;
912 1.1 agc portal_t *portal;
913 1.1 agc initiator_t *init;
914 1.1 agc
915 1.1 agc LOCK_SESSIONS;
916 1.1 agc
917 1.1 agc sess = find_session_id(sid);
918 1.1 agc if (sess == NULL) {
919 1.1 agc UNLOCK_SESSIONS;
920 1.1 agc return;
921 1.1 agc }
922 1.1 agc
923 1.1 agc conn = find_connection_id(sess, cid);
924 1.1 agc if (conn == NULL) {
925 1.1 agc UNLOCK_SESSIONS;
926 1.1 agc return;
927 1.1 agc }
928 1.1 agc
929 1.1 agc TAILQ_REMOVE(&sess->connections, &conn->entry, link);
930 1.1 agc sess->num_connections--;
931 1.1 agc
932 1.1 agc init = find_initiator_id(conn->initiator_id);
933 1.1 agc if (init != NULL)
934 1.1 agc init->active_connections--;
935 1.1 agc
936 1.1 agc UNLOCK_SESSIONS;
937 1.1 agc
938 1.1 agc portal = find_portal_id(conn->portal.sid.id);
939 1.1 agc if (portal != NULL)
940 1.1 agc portal->active_connections--;
941 1.1 agc
942 1.1 agc free(conn);
943 1.1 agc }
944