dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / isns / isnsd / qry.c
blob70bd92a308243db42e80cf736d00878680d3d629
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #include "isns_server.h"
32 #include "isns_func.h"
33 #include "isns_msgq.h"
34 #include "isns_htab.h"
35 #include "isns_cache.h"
36 #include "isns_obj.h"
37 #include "isns_dd.h"
38 #include "isns_pdu.h"
39 #include "isns_qry.h"
42 * external variables
44 extern const int NUM_OF_ATTRS[MAX_OBJ_TYPE_FOR_SIZE];
45 extern const int UID_ATTR_INDEX[MAX_OBJ_TYPE_FOR_SIZE];
46 extern const int NUM_OF_CHILD[MAX_OBJ_TYPE];
47 extern const int TYPE_OF_CHILD[MAX_OBJ_TYPE][MAX_CHILD_TYPE];
50 * global variables
52 const int TAG_RANGE[MAX_OBJ_TYPE][3] = {
53 { 0, 0 },
54 { ENTITY_KEY, LAST_TAG_ENTITY, ENTITY_END },
55 { ISCSI_KEY, LAST_TAG_ISCSI, ISCSI_END },
56 { PORTAL_KEY1, LAST_TAG_PORTAL, PORTAL_END },
57 { PG_KEY1, LAST_TAG_PG, PG_END },
58 { DD_KEY, LAST_TAG_DD, DD_END },
59 { DDS_KEY, LAST_TAG_DDS, DDS_END }
63 * local variables
65 typedef int (*qry_func_t)(lookup_ctrl_t *);
67 /* Edge functions of each adjacent object */
68 static int qry_c2e(lookup_ctrl_t *);
69 static int qry_ds2m(lookup_ctrl_t *);
70 static int qry_slf(lookup_ctrl_t *);
71 static int qry_e2i(lookup_ctrl_t *);
72 static int qry_e2p(lookup_ctrl_t *);
73 static int qry_e2g(lookup_ctrl_t *);
74 static int qry_i2g(lookup_ctrl_t *);
75 static int qry_i2d(lookup_ctrl_t *);
76 static int qry_p2g(lookup_ctrl_t *);
77 static int qry_g2i(lookup_ctrl_t *);
78 static int qry_g2p(lookup_ctrl_t *);
79 static int qry_d2s(lookup_ctrl_t *);
81 /* The directed cyclic graph of query procedure. */
82 /* __|____e_________i_________p_________g_________d_________s____ */
83 /* e | qry_slf...qry_e2i...qry_e2p...qry_e2g...NULL......NULL.... */
84 /* i | qry_c2e...qry_slf...NULL......qry_i2g...qry_i2d...NULL.... */
85 /* p | qry_c2e...NULL......qry_slf...qry_p2g...NULL......NULL.... */
86 /* g | qry_c2e...qry_g2i...qry_g2p...qry_slf...NULL......NULL.... */
87 /* d | NULL......qry_ds2m..NULL......NULL......qry_slf...qry_d2s. */
88 /* s | NULL......NULL......NULL......NULL......qry_ds2m..qry_slf. */
90 /* The type of spanning tree of query graph. */
91 typedef struct adjvex {
92 qry_func_t f;
93 isns_type_t t;
94 struct adjvex const *v;
95 } adjvex_t;
97 /* The solid edges in the spanning tree. */
98 static const adjvex_t v_slf = { &qry_slf, 0, NULL };
99 static const adjvex_t v_c2e = { &qry_c2e, OBJ_ENTITY, NULL };
100 static const adjvex_t v_e2i = { &qry_e2i, OBJ_ISCSI, NULL };
101 static const adjvex_t v_e2p = { &qry_e2p, OBJ_PORTAL, NULL };
102 static const adjvex_t v_e2g = { &qry_e2g, OBJ_PG, NULL };
103 static const adjvex_t v_i2g = { &qry_i2g, OBJ_PG, NULL };
104 static const adjvex_t v_i2d = { &qry_i2d, OBJ_DD, NULL };
105 static const adjvex_t v_p2g = { &qry_p2g, OBJ_PG, NULL };
106 static const adjvex_t v_g2i = { &qry_g2i, OBJ_ISCSI, NULL };
107 static const adjvex_t v_g2p = { &qry_g2p, OBJ_PORTAL, NULL };
108 static const adjvex_t v_d2s = { &qry_d2s, OBJ_DDS, NULL };
109 static const adjvex_t v_d2i = { &qry_ds2m, OBJ_ISCSI, NULL };
110 static const adjvex_t v_s2d = { &qry_ds2m, OBJ_DD, NULL };
112 /* The virtual edges in the spanning tree. */
113 static const adjvex_t v_i2p = { &qry_i2g, OBJ_PG, &v_g2p };
114 static const adjvex_t v_i2s = { &qry_i2d, OBJ_DD, &v_d2s };
116 static const adjvex_t v_g2d = { &qry_g2i, OBJ_ISCSI, &v_i2d };
117 static const adjvex_t v_g2s = { &qry_g2i, OBJ_ISCSI, &v_i2s };
119 static const adjvex_t v_p2i = { &qry_p2g, OBJ_PG, &v_g2i };
120 static const adjvex_t v_p2d = { &qry_p2g, OBJ_PG, &v_g2d };
121 static const adjvex_t v_p2s = { &qry_p2g, OBJ_PG, &v_g2s };
123 static const adjvex_t v_e2d = { &qry_e2i, OBJ_ISCSI, &v_i2d };
124 static const adjvex_t v_e2s = { &qry_e2i, OBJ_ISCSI, &v_i2s };
126 static const adjvex_t v_d2e = { &qry_ds2m, OBJ_ISCSI, &v_c2e };
127 static const adjvex_t v_d2p = { &qry_ds2m, OBJ_ISCSI, &v_i2p };
128 static const adjvex_t v_d2g = { &qry_ds2m, OBJ_ISCSI, &v_i2g };
130 static const adjvex_t v_s2e = { &qry_ds2m, OBJ_DD, &v_d2e };
131 static const adjvex_t v_s2i = { &qry_ds2m, OBJ_DD, &v_d2i };
132 static const adjvex_t v_s2p = { &qry_ds2m, OBJ_DD, &v_d2p };
133 static const adjvex_t v_s2g = { &qry_ds2m, OBJ_DD, &v_d2g };
135 /* the vector of query graph */
136 static const adjvex_t *qry_puzzle[MAX_OBJ_TYPE][MAX_OBJ_TYPE] = {
137 { NULL },
138 { NULL, &v_slf, &v_e2i, &v_e2p, &v_e2g, &v_e2d, &v_e2s },
139 { NULL, &v_c2e, &v_slf, &v_i2p, &v_i2g, &v_i2d, &v_i2s },
140 { NULL, &v_c2e, &v_p2i, &v_slf, &v_p2g, &v_p2d, &v_p2s },
141 { NULL, &v_c2e, &v_g2i, &v_g2p, &v_slf, &v_g2d, &v_g2s },
142 { NULL, &v_d2e, &v_d2i, &v_d2p, &v_d2g, &v_slf, &v_d2s },
143 { NULL, &v_s2e, &v_s2i, &v_s2p, &v_s2g, &v_s2d, &v_slf }
146 static int
147 cb_qry_parent_uid(
148 void *p1,
149 /* LINTED E_FUNC_ARG_UNUSED */
150 void *p2
153 uint32_t puid = get_parent_uid((isns_obj_t *)p1);
154 return ((int)puid);
157 static int
158 cb_qry_child_uids(
159 void *p1,
160 void *p2
163 isns_obj_t *obj = (isns_obj_t *)p1;
164 lookup_ctrl_t *lcp = (lookup_ctrl_t *)p2;
165 isns_type_t type = lcp->data[1].ui;
166 uint32_t *uidp = get_child_t(obj, type);
167 uint32_t num = 0;
168 uint32_t *p;
170 if (uidp != NULL && *uidp > 0) {
171 num = *uidp;
172 p = malloc(num * sizeof (*p));
173 if (p != NULL) {
174 uidp ++;
175 (void) memcpy(p, uidp, num * sizeof (*p));
176 lcp->id[2] = num;
177 lcp->data[2].ptr = (uchar_t *)p;
178 } else {
179 return (ISNS_RSP_INTERNAL_ERROR);
183 return (0);
186 static int
187 e2c(
188 lookup_ctrl_t *lcp,
189 isns_type_t type
192 int ec = 0;
194 uint32_t uid = lcp->curr_uid; /* last child */
195 uint32_t num_of_child;
196 uint32_t *uids;
198 uint32_t tmp_uid = 0;
200 /* the first times of query */
201 if (uid == 0) {
202 lcp->data[1].ui = type;
203 ec = cache_lookup(lcp, NULL, cb_qry_child_uids);
206 num_of_child = lcp->id[2];
207 uids = (uint32_t *)lcp->data[2].ptr;
209 while (num_of_child > 0) {
210 if (*uids > uid) {
211 tmp_uid = *uids;
212 break;
214 uids ++;
215 num_of_child --;
218 uid = tmp_uid;
220 /* no more child, clean up memory */
221 if (uid == 0) {
222 lcp->data[1].ui = 0;
223 lcp->id[2] = 0;
224 lcp->data[2].ptr = NULL;
226 /* free up the memory */
227 free(uids);
230 /* save it for returning and querying next uid */
231 lcp->curr_uid = uid;
233 return (ec);
236 static int
237 qry_c2e(
238 lookup_ctrl_t *lcp
241 uint32_t uid;
243 /* child object has only one parent */
244 if (lcp->curr_uid == 0) {
245 uid = (uint32_t)cache_lookup(lcp, NULL,
246 cb_qry_parent_uid);
247 } else {
248 uid = 0;
251 /* save the result for returnning */
252 lcp->curr_uid = uid;
254 return (0);
257 static int
258 qry_ds2m(
259 lookup_ctrl_t *lcp
262 int ec = 0;
264 uint32_t uid = lcp->curr_uid; /* last member */
265 isns_type_t type = lcp->type;
266 uint32_t ds_id = lcp->data[0].ui;
268 uint32_t tmp_uid;
270 uint32_t n;
271 bmp_t *p;
273 /* the first times of query */
274 if (uid == 0) {
275 ec = (type == OBJ_DD) ?
276 get_dd_matrix(ds_id, &p, &n) :
277 get_dds_matrix(ds_id, &p, &n);
278 lcp->id[1] = n;
279 lcp->data[1].ptr = (uchar_t *)p;
280 } else {
281 n = lcp->id[1];
282 p = (bmp_t *)lcp->data[1].ptr;
285 FOR_EACH_MEMBER(p, n, tmp_uid, {
286 if (tmp_uid > uid) {
287 lcp->curr_uid = tmp_uid;
288 return (ec);
292 /* no more member, clean up memory */
293 lcp->id[1] = 0;
294 lcp->data[1].ptr = NULL;
296 /* free up the matrix */
297 free(p);
299 lcp->curr_uid = 0;
301 return (ec);
304 static int
305 qry_slf(
306 lookup_ctrl_t *lcp
309 uint32_t uid;
311 if (lcp->curr_uid == 0) {
312 uid = lcp->data[0].ui;
313 } else {
314 uid = 0;
317 lcp->curr_uid = uid;
319 return (0);
322 static int
323 qry_e2i(
324 lookup_ctrl_t *lcp
327 return (e2c(lcp, OBJ_ISCSI));
330 static int
331 qry_e2p(
332 lookup_ctrl_t *lcp
335 return (e2c(lcp, OBJ_PORTAL));
338 static int
339 qry_e2g(
340 lookup_ctrl_t *lcp
343 uint32_t uid = lcp->curr_uid; /* last pg */
345 htab_t *htab = cache_get_htab(OBJ_PG);
347 lookup_ctrl_t lc;
348 uint32_t puid;
350 SET_UID_LCP(&lc, OBJ_PG, 0);
352 /* this is a shortcut */
353 FOR_EACH_ITEM(htab, uid, {
354 lc.data[0].ui = uid;
355 puid = (uint32_t)cache_lookup(&lc, NULL,
356 cb_qry_parent_uid);
357 if (puid == lcp->data[0].ui) {
358 /* keep the current uid */
359 lcp->curr_uid = uid;
360 return (0);
364 lcp->curr_uid = 0;
366 return (0);
369 static int
370 qry_i2g(
371 lookup_ctrl_t *lcp
374 int ec = 0;
376 uint32_t uid = lcp->curr_uid; /* last pg */
377 lookup_ctrl_t lc;
379 /* the first times of query */
380 if (uid == 0) {
381 lcp->id[1] = ISNS_ISCSI_NAME_ATTR_ID;
382 ec = cache_lookup(lcp, NULL, cb_clone_attrs);
385 if (lcp->data[1].ptr != NULL) {
386 /* pg lookup */
387 lc.curr_uid = uid;
388 lc.type = OBJ_PG;
389 lc.id[0] = ATTR_INDEX_PG(ISNS_PG_ISCSI_NAME_ATTR_ID);
390 lc.op[0] = OP_STRING;
391 lc.data[0].ptr = lcp->data[1].ptr;
392 lc.op[1] = 0;
394 uid = is_obj_there(&lc);
395 } else {
396 uid = 0;
399 /* no more pg, update lcp with pg object */
400 if (uid == 0) {
401 lcp->id[1] = 0;
403 /* clean up the memory */
404 if (lcp->data[1].ptr != NULL) {
405 free(lcp->data[1].ptr);
406 /* reset it */
407 lcp->data[1].ptr = NULL;
411 /* save it for returning and querying next pg */
412 lcp->curr_uid = uid;
414 return (ec);
417 static int
418 qry_i2d(
419 lookup_ctrl_t *lcp
422 uint32_t dd_id = lcp->curr_uid; /* last dd_id */
423 uint32_t uid = lcp->data[0].ui;
425 dd_id = get_dd_id(uid, dd_id);
427 /* save it for returning and getting next dd */
428 lcp->curr_uid = dd_id;
430 return (0);
433 static int
434 qry_p2g(
435 lookup_ctrl_t *lcp
438 int ec = 0;
440 uint32_t uid = lcp->curr_uid; /* last pg */
441 lookup_ctrl_t lc;
443 /* the first time of query */
444 if (uid == 0) {
445 /* use 1&2 for the portal ip address & port */
446 lcp->id[1] = ISNS_PORTAL_IP_ADDR_ATTR_ID;
447 lcp->id[2] = ISNS_PORTAL_PORT_ATTR_ID;
448 ec = cache_lookup(lcp, NULL, cb_clone_attrs);
451 if (lcp->data[1].ip != NULL) {
452 /* pg lookup */
453 lc.curr_uid = uid;
454 lc.type = OBJ_PG;
455 lc.id[0] = ATTR_INDEX_PG(ISNS_PG_PORTAL_IP_ADDR_ATTR_ID);
456 lc.op[0] = OP_MEMORY_IP6;
457 lc.data[0].ip = lcp->data[1].ip;
458 lc.id[1] = ATTR_INDEX_PG(ISNS_PG_PORTAL_PORT_ATTR_ID);
459 lc.op[1] = OP_INTEGER;
460 lc.data[1].ui = lcp->data[2].ui;
461 lc.op[2] = 0;
463 uid = is_obj_there(&lc);
464 } else {
465 uid = 0;
468 /* no more pg, clean up memory */
469 if (uid == 0) {
470 lcp->id[1] = 0;
471 lcp->id[2] = 0;
473 /* clean up the memory */
474 if (lcp->data[1].ip != NULL) {
475 free(lcp->data[1].ip);
476 /* reset it */
477 lcp->data[1].ip = NULL;
479 lcp->data[2].ui = 0;
482 /* save it for returning and next query */
483 lcp->curr_uid = uid;
485 return (ec);
488 static int
489 qry_g2i(
490 lookup_ctrl_t *lcp
493 int ec = 0;
495 uint32_t uid = lcp->curr_uid; /* last node */
496 lookup_ctrl_t lc;
498 /* the first time of query */
499 if (uid == 0) {
500 /* use slot 1 for the storage node name */
501 lcp->id[1] = ISNS_PG_ISCSI_NAME_ATTR_ID;
502 ec = cache_lookup(lcp, NULL, cb_clone_attrs);
504 if (lcp->data[1].ptr != NULL) {
505 /* iscsi node lookup */
506 lc.curr_uid = uid;
507 lc.type = OBJ_ISCSI;
508 lc.id[0] = ATTR_INDEX_ISCSI(ISNS_ISCSI_NAME_ATTR_ID);
509 lc.op[0] = OP_STRING;
510 lc.data[0].ptr = lcp->data[1].ptr;
511 lc.op[1] = 0;
513 uid = is_obj_there(&lc);
515 /* no longer need it, clean it up */
516 free(lcp->data[1].ptr);
517 lcp->data[1].ptr = NULL;
519 /* no longer need it, reset it */
520 lcp->id[1] = 0;
521 } else {
522 /* one pg has maximum number of one storage node */
523 uid = 0;
526 /* save it for returning and next query */
527 lcp->curr_uid = uid;
529 return (ec);
532 static int
533 qry_g2p(
534 lookup_ctrl_t *lcp
537 int ec = 0;
539 uint32_t uid = lcp->curr_uid; /* last portal */
540 lookup_ctrl_t lc;
542 /* the first times of query */
543 if (uid == 0) {
544 /* use 1&2 for the portal ip addr and port */
545 lcp->id[1] = ISNS_PG_PORTAL_IP_ADDR_ATTR_ID;
546 lcp->id[2] = ISNS_PG_PORTAL_PORT_ATTR_ID;
547 ec = cache_lookup(lcp, NULL, cb_clone_attrs);
549 if (lcp->data[1].ip != NULL) {
550 /* portal lookup */
551 lc.curr_uid = uid;
552 lc.type = OBJ_PORTAL;
553 lc.id[0] = ATTR_INDEX_PORTAL(
554 ISNS_PORTAL_IP_ADDR_ATTR_ID);
555 lc.op[0] = OP_MEMORY_IP6;
556 lc.data[0].ip = lcp->data[1].ip;
557 lc.id[1] = ATTR_INDEX_PORTAL(
558 ISNS_PORTAL_PORT_ATTR_ID);
559 lc.op[1] = OP_INTEGER;
560 lc.data[1].ui = lcp->data[2].ui;
561 lc.op[2] = 0;
563 uid = is_obj_there(&lc);
565 /* no longer need it, reset it */
566 free(lcp->data[1].ip);
567 lcp->data[1].ip = NULL;
569 /* no longer need it, reset it */
570 lcp->id[1] = 0;
571 lcp->id[2] = 0;
572 lcp->data[2].ui = 0;
573 } else {
574 /* one pg has maximum number of one portal */
575 uid = 0;
578 /* save it for returning and next query */
579 lcp->curr_uid = uid;
581 return (ec);
584 static int
585 qry_d2s(
586 lookup_ctrl_t *lcp
589 uint32_t dds_id = lcp->curr_uid; /* last dds */
590 uint32_t dd_id = lcp->data[0].ui;
592 dds_id = get_dds_id(dd_id, dds_id);
594 /* save it for returning and for getting next dds */
595 lcp->curr_uid = dds_id;
597 return (0);
601 validate_qry_key(
602 isns_type_t type,
603 isns_tlv_t *key,
604 uint16_t key_len,
605 isns_attr_t *attrs
608 int ec = 0;
610 uint32_t tag;
611 uint32_t min_tag, max_tag;
613 isns_attr_t *attr;
615 min_tag = TAG_RANGE[type][0];
616 max_tag = TAG_RANGE[type][2];
618 while (key_len != 0 && ec == 0) {
619 tag = key->attr_id;
620 if (tag < min_tag || tag > max_tag) {
621 ec = ISNS_RSP_MSG_FORMAT_ERROR;
622 } else if (key->attr_len > 0 && attrs != NULL) {
623 attr = &attrs[tag - min_tag]; /* ATTR_INDEX_xxx */
624 ec = extract_attr(attr, key, 0);
625 if (ec == ISNS_RSP_INVALID_REGIS) {
626 ec = ISNS_RSP_MSG_FORMAT_ERROR;
629 NEXT_TLV(key, key_len);
632 return (ec);
635 static lookup_method_t
636 get_op_method(
637 uint32_t tag
640 lookup_method_t method = 0;
642 switch (tag) {
643 /* OP_STRING */
644 case ISNS_EID_ATTR_ID:
645 case ISNS_PORTAL_NAME_ATTR_ID:
646 case ISNS_ISCSI_ALIAS_ATTR_ID:
647 case ISNS_DD_SET_NAME_ATTR_ID:
648 case ISNS_DD_NAME_ATTR_ID:
649 case ISNS_ISCSI_NAME_ATTR_ID:
650 case ISNS_PG_ISCSI_NAME_ATTR_ID:
651 case ISNS_ISCSI_AUTH_METHOD_ATTR_ID:
652 method = OP_STRING;
653 break;
654 /* OP_MEMORY_IP6 */
655 case ISNS_MGMT_IP_ADDR_ATTR_ID:
656 case ISNS_PORTAL_IP_ADDR_ATTR_ID:
657 case ISNS_PG_PORTAL_IP_ADDR_ATTR_ID:
658 method = OP_MEMORY_IP6;
659 break;
660 /* OP_INTEGER */
661 case ISNS_ENTITY_PROTOCOL_ATTR_ID:
662 case ISNS_VERSION_RANGE_ATTR_ID:
663 case ISNS_ENTITY_REG_PERIOD_ATTR_ID:
664 case ISNS_ENTITY_INDEX_ATTR_ID:
665 case ISNS_PORTAL_PORT_ATTR_ID:
666 case ISNS_ESI_INTERVAL_ATTR_ID:
667 case ISNS_ESI_PORT_ATTR_ID:
668 case ISNS_PORTAL_INDEX_ATTR_ID:
669 case ISNS_SCN_PORT_ATTR_ID:
670 case ISNS_ISCSI_NODE_TYPE_ATTR_ID:
671 case ISNS_ISCSI_SCN_BITMAP_ATTR_ID:
672 case ISNS_ISCSI_NODE_INDEX_ATTR_ID:
673 case ISNS_PG_PORTAL_PORT_ATTR_ID:
674 case ISNS_PG_TAG_ATTR_ID:
675 case ISNS_PG_INDEX_ATTR_ID:
676 case ISNS_DD_SET_ID_ATTR_ID:
677 case ISNS_DD_SET_STATUS_ATTR_ID:
678 case ISNS_DD_ID_ATTR_ID:
679 /* all other attrs */
680 default:
681 method = OP_INTEGER;
682 break;
685 return (method);
688 static int
689 cb_attrs_match(
690 void *p1,
691 void *p2
694 isns_obj_t *obj = (isns_obj_t *)p1;
695 isns_attr_t *attrs = (isns_attr_t *)
696 ((lookup_ctrl_t *)p2)->data[1].ptr;
698 lookup_ctrl_t lc;
699 int match = 1; /* 0: not match, otherwise: match */
701 int i;
703 lc.op[1] = 0;
705 for (i = 0; match != 0 && i < NUM_OF_ATTRS[obj->type]; i++) {
706 if (attrs->tag != 0 && attrs->len > 0) {
707 lc.id[0] = i;
708 lc.op[0] = get_op_method(attrs->tag);
709 lc.data[0].ptr = attrs->value.ptr;
710 match = key_cmp(&lc, obj) == 0 ? 1 : 0;
712 attrs ++;
715 return (match);
718 static int
719 attrs_match(
720 isns_type_t type,
721 uint32_t uid,
722 isns_attr_t *attrs
725 int match; /* 0: not match, otherwise: match */
726 lookup_ctrl_t lc;
728 SET_UID_LCP(&lc, type, uid);
730 lc.data[1].ptr = (uchar_t *)attrs;
732 match = cache_lookup(&lc, NULL, cb_attrs_match);
734 return (match);
737 static int
738 insert_uid(
739 uint32_t **pp,
740 uint32_t *np,
741 uint32_t *sp,
742 uint32_t uid
745 int ec = 0;
747 uint32_t *p = *pp;
748 uint32_t n = *np;
749 uint32_t s = *sp;
751 uint32_t u;
752 uint32_t *t;
754 /* check for duplication */
755 if (n > 0 && uid <= p[n - 1]) {
756 while (n-- > 0) {
757 if (p[n] == uid) {
758 return (0);
761 n = *np;
762 u = p[n - 1];
763 p[n - 1] = uid;
764 uid = u;
768 if (s == n) {
769 s = (s == 0) ? 8 : s * 2;
770 t = reallocarray(p, s, sizeof (uint32_t));
771 if (t != NULL) {
772 p = t;
773 *pp = p;
774 *sp = s;
775 } else {
776 ec = ISNS_RSP_INTERNAL_ERROR;
780 if (ec == 0) {
781 p[n ++] = uid;
782 *np = n;
785 return (ec);
788 static int
789 qry_and_match(
790 uint32_t **obj_uids,
791 uint32_t *num_of_objs,
792 uint32_t *size,
793 isns_type_t type,
794 uint32_t src_uid,
795 isns_type_t src_type,
796 isns_attr_t *attrs
799 int ec = 0;
801 lookup_ctrl_t lc = { 0 }; /* !!! need to be empty */
802 uint32_t uid;
804 const adjvex_t *vex;
806 /* circular list */
807 uint32_t *p[2], n[2], s[2];
808 int i, j;
810 uint32_t *p1, n1;
811 uint32_t *p2, n2, s2;
812 isns_type_t t;
814 /* initialize the circular list */
815 i = 0;
816 j = 1;
818 p[i] = *obj_uids;
819 n[i] = *num_of_objs;
820 s[i] = *size;
822 p[j] = malloc(8 * sizeof (uint32_t));
823 p[j][0] = src_uid;
824 n[j] = 1;
825 s[j] = 8;
827 /* initial object type of being queried */
828 t = src_type;
830 vex = qry_puzzle[src_type][type];
832 do {
833 /* shift one on the circular list */
834 i = (i + 1) & 1;
835 j = (j + 1) & 1;
837 p1 = p[i]; n1 = n[i];
838 p2 = p[j]; n2 = n[j]; s2 = s[j];
840 /* prepare lookup control */
841 lc.type = t;
842 lc.id[0] = UID_ATTR_INDEX[t];
843 lc.op[0] = OP_INTEGER;
845 /* result object type */
846 t = vex->t;
848 FOR_EACH_OBJS(p1, n1, uid, {
849 /* start query */
850 lc.data[0].ui = uid;
851 ec = vex->f(&lc);
852 uid = lc.curr_uid;
853 while (ec == 0 && uid != 0) {
854 if (attrs == NULL ||
855 attrs_match(type, uid, attrs) != 0) {
856 ec = insert_uid(&p2, &n2, &s2, uid);
858 if (ec == 0) {
859 ec = vex->f(&lc);
860 uid = lc.curr_uid;
861 } else {
862 n1 = n2 = 0; /* force break */
866 if (ec == 0) {
867 vex = vex->v;
868 } else {
869 vex = NULL; /* force break */
871 /* push back */
872 p[j] = p2; n[j] = n2; s[j] = s2;
873 /* reset the number of objects */
874 n[i] = 0;
875 } while (vex != NULL);
877 /* clean up the memory */
878 free(p1);
879 if (ec != 0) {
880 free(p2);
881 p2 = NULL;
882 n2 = 0;
883 s2 = 0;
886 *obj_uids = p2;
887 *num_of_objs = n2;
888 *size = s2;
890 return (ec);
894 get_qry_keys(
895 bmp_t *nodes_bmp,
896 uint32_t num_of_nodes,
897 isns_type_t *type,
898 isns_tlv_t *key,
899 uint16_t key_len,
900 uint32_t **obj_uids,
901 uint32_t *num_of_objs
904 int ec = 0;
905 union {
906 isns_obj_t o;
907 isns_entity_t e;
908 isns_iscsi_t i;
909 isns_portal_t p;
910 isns_pg_t g;
911 isns_dd_t d;
912 isns_dds_t s;
913 } an_obj = { 0 };
914 isns_attr_t *attrs;
916 htab_t *htab;
917 uint32_t node_uid;
919 uint32_t size;
921 *obj_uids = NULL;
922 *num_of_objs = 0;
923 size = 0;
925 /* get the object type identified by the key */
926 *type = TLV2TYPE(key);
927 if (*type == 0) {
928 return (ISNS_RSP_INVALID_QRY);
931 attrs = &an_obj.o.attrs[0];
932 /* validate the Message Key */
933 ec = validate_qry_key(*type, key, key_len, attrs);
934 if (ec != 0) {
935 return (ec);
938 if (nodes_bmp != NULL) {
939 FOR_EACH_MEMBER(nodes_bmp, num_of_nodes, node_uid, {
940 ec = qry_and_match(
941 obj_uids, num_of_objs, &size, *type,
942 node_uid, OBJ_ISCSI, attrs);
943 if (ec != 0) {
944 return (ec);
947 } else {
948 node_uid = 0;
949 htab = cache_get_htab(OBJ_ISCSI);
950 FOR_EACH_ITEM(htab, node_uid, {
951 ec = qry_and_match(
952 obj_uids, num_of_objs, &size, *type,
953 node_uid, OBJ_ISCSI, attrs);
954 if (ec != 0) {
955 return (ec);
960 return (ec);
964 get_qry_ops(
965 uint32_t uid,
966 isns_type_t src_type,
967 isns_type_t op_type,
968 uint32_t **op_uids,
969 uint32_t *num_of_ops,
970 uint32_t *size
973 int ec = 0;
975 *num_of_ops = 0;
977 ec = qry_and_match(
978 op_uids, num_of_ops, size, op_type,
979 uid, src_type, NULL);
981 return (ec);
985 get_qry_ops2(
986 uint32_t *nodes_bmp,
987 uint32_t num_of_nodes,
988 isns_type_t op_type,
989 uint32_t **op_uids,
990 uint32_t *num_of_ops,
991 uint32_t *size
994 int ec = 0;
996 uint32_t node_uid;
998 htab_t *htab;
1000 *num_of_ops = 0;
1002 if (nodes_bmp != NULL) {
1003 FOR_EACH_MEMBER(nodes_bmp, num_of_nodes, node_uid, {
1004 ec = qry_and_match(
1005 op_uids, num_of_ops, size, op_type,
1006 node_uid, OBJ_ISCSI, NULL);
1007 if (ec != 0) {
1008 return (ec);
1011 } else {
1012 node_uid = 0;
1013 htab = cache_get_htab(OBJ_ISCSI);
1014 FOR_EACH_ITEM(htab, node_uid, {
1015 ec = qry_and_match(
1016 op_uids, num_of_ops, size, op_type,
1017 node_uid, OBJ_ISCSI, NULL);
1018 if (ec != 0) {
1019 return (ec);
1024 return (ec);
1027 uint32_t
1028 get_next_obj(
1029 isns_tlv_t *tlv,
1030 uint32_t tlv_len,
1031 isns_type_t type,
1032 uint32_t *uids,
1033 uint32_t num
1036 lookup_ctrl_t lc;
1038 uint32_t tag;
1039 uint8_t *value;
1041 uint32_t old = 0;
1042 uint32_t min = 0;
1043 uint32_t uid, diff;
1044 uint32_t pre_diff = 0xFFFFFFFF;
1046 lc.curr_uid = 0;
1047 lc.type = type;
1048 lc.op[1] = 0;
1049 lc.op[2] = 0;
1051 if (tlv_len > 8) {
1052 tag = tlv->attr_id;
1053 value = tlv->attr_value;
1054 switch (tag) {
1055 case ISNS_EID_ATTR_ID:
1056 lc.id[0] = ATTR_INDEX_ENTITY(ISNS_EID_ATTR_ID);
1057 lc.op[0] = OP_STRING;
1058 lc.data[0].ptr = (uchar_t *)value;
1059 break;
1060 case ISNS_ISCSI_NAME_ATTR_ID:
1061 lc.id[0] = ATTR_INDEX_ISCSI(ISNS_ISCSI_NAME_ATTR_ID);
1062 lc.op[0] = OP_STRING;
1063 lc.data[0].ptr = (uchar_t *)value;
1064 break;
1065 case ISNS_ISCSI_NODE_INDEX_ATTR_ID:
1066 lc.id[0] = ATTR_INDEX_ISCSI(
1067 ISNS_ISCSI_NODE_INDEX_ATTR_ID);
1068 lc.op[0] = OP_INTEGER;
1069 lc.data[0].ui = ntohl(*(uint32_t *)value);
1070 break;
1071 case ISNS_PORTAL_IP_ADDR_ATTR_ID:
1072 lc.id[0] = ATTR_INDEX_PORTAL(
1073 ISNS_PORTAL_IP_ADDR_ATTR_ID);
1074 lc.op[0] = OP_MEMORY_IP6;
1075 lc.data[0].ip = (in6_addr_t *)value;
1076 NEXT_TLV(tlv, tlv_len);
1077 if (tlv_len > 8 &&
1078 ((tag = tlv->attr_id) ==
1079 ISNS_PORTAL_PORT_ATTR_ID)) {
1080 value = tlv->attr_value;
1081 lc.id[1] = ATTR_INDEX_PORTAL(
1082 ISNS_PORTAL_PORT_ATTR_ID);
1083 lc.op[1] = OP_INTEGER;
1084 lc.data[1].ui = ntohl(*(uint32_t *)value);
1085 } else {
1086 return (0);
1088 break;
1089 case ISNS_PORTAL_INDEX_ATTR_ID:
1090 lc.id[0] = ATTR_INDEX_PORTAL(ISNS_PORTAL_INDEX_ATTR_ID);
1091 lc.op[0] = OP_INTEGER;
1092 lc.data[0].ui = ntohl(*(uint32_t *)value);
1093 break;
1094 case ISNS_PG_INDEX_ATTR_ID:
1095 lc.id[0] = ATTR_INDEX_PG(ISNS_PG_INDEX_ATTR_ID);
1096 lc.op[0] = OP_INTEGER;
1097 lc.data[0].ui = ntohl(*(uint32_t *)value);
1098 break;
1099 default:
1100 return (0);
1103 old = is_obj_there(&lc);
1104 if (old == 0) {
1105 return (0);
1109 while (num > 0) {
1110 uid = uids[-- num];
1111 if (uid > old) {
1112 diff = uid - old;
1113 if (diff < pre_diff) {
1114 min = uid;
1115 pre_diff = diff;
1120 return (min);
1123 static int
1124 cb_qry_rsp(
1125 void *p1,
1126 void *p2
1129 int ec = 0;
1131 isns_obj_t *obj = (isns_obj_t *)p1;
1132 lookup_ctrl_t *lcp = (lookup_ctrl_t *)p2;
1134 uint16_t tlv_len = lcp->id[1];
1135 isns_tlv_t *tlv = (isns_tlv_t *)lcp->data[1].ptr;
1136 conn_arg_t *conn = (conn_arg_t *)lcp->data[2].ptr;
1138 isns_type_t type = obj->type;
1139 uint32_t min_tag = TAG_RANGE[type][0];
1140 uint32_t mid_tag = TAG_RANGE[type][1];
1141 uint32_t max_tag = TAG_RANGE[type][2];
1143 isns_attr_t *attr;
1144 uint32_t tag;
1145 uint32_t id;
1146 uint32_t len;
1147 void *value;
1149 isns_pdu_t *rsp = conn->out_packet.pdu;
1150 size_t pl = conn->out_packet.pl;
1151 size_t sz = conn->out_packet.sz;
1153 do {
1154 if (tlv->attr_len == 0) {
1155 tag = tlv->attr_id;
1156 if (tag <= mid_tag) {
1157 id = ATTR_INDEX(tag, type);
1158 attr = &obj->attrs[id];
1159 len = attr->len;
1160 value = (void *)attr->value.ptr;
1161 ec = pdu_add_tlv(&rsp, &pl, &sz,
1162 tag, len, value, 0);
1165 NEXT_TLV(tlv, tlv_len);
1166 } while (ec == 0 &&
1167 tlv_len >= 8 &&
1168 tlv->attr_id >= min_tag &&
1169 tlv->attr_id <= max_tag);
1171 conn->out_packet.pdu = rsp;
1172 conn->out_packet.pl = pl;
1173 conn->out_packet.sz = sz;
1175 return (ec);
1179 get_qry_attrs(
1180 uint32_t uid,
1181 isns_type_t type,
1182 isns_tlv_t *tlv,
1183 uint16_t tlv_len,
1184 conn_arg_t *conn
1187 int ec = 0;
1189 lookup_ctrl_t lc;
1191 SET_UID_LCP(&lc, type, uid);
1193 lc.id[1] = tlv_len;
1194 lc.data[1].ptr = (uchar_t *)tlv;
1195 lc.data[2].ptr = (uchar_t *)conn;
1197 ec = cache_lookup(&lc, NULL, cb_qry_rsp);
1199 return (ec);
1203 get_qry_attrs1(
1204 uint32_t uid,
1205 isns_type_t type,
1206 isns_tlv_t *tlv,
1207 uint16_t tlv_len,
1208 conn_arg_t *conn
1211 isns_tlv_t *tmp = tlv;
1212 uint32_t tmp_len = tlv_len;
1214 /* clear the length of all of tlv */
1215 while (tmp_len > 8) {
1216 tmp->attr_len = 0;
1217 NEXT_TLV(tmp, tmp_len);
1220 return (get_qry_attrs(uid, type, tlv, tlv_len, conn));