Merge commit 'b1e7e97d3b60469b243b3b2e22c7d8cbd11c7c90'
[unleashed.git] / usr / src / cmd / isns / isnsd / dump.c
blob1eda3a5696ae552cb78c0741cb79d4357b0638c9
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 /* This file is getting large unexpectly, a lex & yacc */
28 /* implementation is expected. */
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39 #include <fcntl.h>
40 #include <unistd.h>
41 #include <pthread.h>
43 #include "isns_server.h"
44 #include "isns_htab.h"
45 #include "isns_msgq.h"
46 #include "isns_obj.h"
47 #include "isns_func.h"
48 #include "isns_dd.h"
49 #include "isns_cache.h"
50 #include "isns_pdu.h"
52 #ifdef DEBUG
54 * external variables
56 extern const int NUM_OF_CHILD[MAX_OBJ_TYPE];
57 extern const int TYPE_OF_CHILD[MAX_OBJ_TYPE][MAX_CHILD_TYPE];
58 extern const int UID_ATTR_INDEX[MAX_OBJ_TYPE_FOR_SIZE];
59 extern const int NUM_OF_REF[MAX_OBJ_TYPE_FOR_SIZE];
61 extern lookup_ctrl_t *setup_ddid_lcp(lookup_ctrl_t *, uint32_t);
62 extern lookup_ctrl_t *setup_ddsid_lcp(lookup_ctrl_t *, uint32_t);
65 * global variables
67 int verbose_mc = 0;
68 int verbose_tc = 0;
69 int verbose_lock = 0;
70 int verbose_net = 0;
71 int verbose_parser = 0;
74 * local variables
76 static void print_entity(char *, isns_obj_t *);
77 static void print_iscsi(char *, isns_obj_t *);
78 static void print_portal(char *, isns_obj_t *);
79 static void print_pg(char *, isns_obj_t *);
80 static void print_dd(char *, isns_obj_t *);
81 static void print_dds(char *, isns_obj_t *);
82 static void (*const print_func[MAX_OBJ_TYPE])(char *, isns_obj_t *) = {
83 NULL,
84 &print_entity,
85 &print_iscsi,
86 &print_portal,
87 &print_pg,
88 &print_dd,
89 &print_dds
91 static int run_cmd(char *);
93 typedef struct {
94 uint16_t func_id;
95 char *fname;
96 } isnsp_fnames_t;
97 isnsp_fnames_t fnames[] = {
98 { ISNS_DEV_ATTR_REG, "DevAttrReg" },
99 { ISNS_DEV_ATTR_QRY, "DevAttrQry" },
100 { ISNS_DEV_GET_NEXT, "DevGetNext" },
101 { ISNS_DEV_DEREG, "DevDereg" },
102 { ISNS_SCN_REG, "SCNReg" },
103 { ISNS_SCN_DEREG, "SCNDereg" },
104 { ISNS_DD_REG, "DDReg" },
105 { ISNS_DD_DEREG, "DDDereg" },
106 { ISNS_DDS_REG, "DDSReg" },
107 { ISNS_DDS_DEREG, "DDSDereg" },
108 { ISNS_SCN, "SCN" },
109 { ISNS_ESI, "ESI" },
110 { ISNS_HEARTBEAT, "Heartbeat" },
111 { ISNS_DEV_ATTR_REG_RSP, "DevAttrRegRsp" },
112 { ISNS_DEV_ATTR_QRY_RSP, "DevAttrQryRsp" },
113 { ISNS_DEV_GET_NEXT_RSP, "DevGetNextRsp" },
114 { ISNS_DEV_DEREG_RSP, "DevDeregRsp" },
115 { ISNS_SCN_REG_RSP, "SCNRegRsp" },
116 { ISNS_SCN_DEREG_RSP, "SCNDeregRsp" },
117 { ISNS_SCN_RSP, "SCNRsp" },
118 { ISNS_ESI_RSP, "ESIRsp" },
119 { 0xFFFF, "Unknown" } };
121 static char *
122 get_func_name(
123 uint16_t id
126 int i = 0;
127 isnsp_fnames_t *fp = &fnames[i ++];
128 while (fp->func_id != 0xFFFF) {
129 if (fp->func_id == id) {
130 return (fp->fname);
132 fp = &fnames[i ++];
135 return ("UNKNOWN");
138 static char *
139 get_tlv_tag_name(
140 uint32_t tag
143 switch (tag) {
144 case ISNS_DELIMITER_ATTR_ID:
145 return ("Delimiter");
146 case ISNS_EID_ATTR_ID:
147 return ("Entity Identifier");
148 case ISNS_ENTITY_PROTOCOL_ATTR_ID:
149 return ("Entity Protocol");
150 case ISNS_ENTITY_REG_PERIOD_ATTR_ID:
151 return ("Registration Period");
152 case ISNS_TIMESTAMP_ATTR_ID:
153 return ("Timestamp");
154 case ISNS_PORTAL_IP_ADDR_ATTR_ID:
155 return ("Portal IP Address");
156 case ISNS_PORTAL_PORT_ATTR_ID:
157 return ("Portal TCP/UDP Port");
158 case ISNS_PORTAL_NAME_ATTR_ID:
159 return ("Portal Symbolic Name");
160 case ISNS_ESI_INTERVAL_ATTR_ID:
161 return ("ESI Interval");
162 case ISNS_ESI_PORT_ATTR_ID:
163 return ("ESI Port");
164 case ISNS_SCN_PORT_ATTR_ID:
165 return ("SCN Port");
166 case ISNS_PORTAL_SEC_BMP_ATTR_ID:
167 return ("Portal Security Bitmap");
168 case ISNS_ISCSI_NAME_ATTR_ID:
169 return ("iSCSI Name");
170 case ISNS_ISCSI_NODE_TYPE_ATTR_ID:
171 return ("iSCSI Node Type");
172 case ISNS_ISCSI_ALIAS_ATTR_ID:
173 return ("iSCSI Alias");
174 case ISNS_ISCSI_AUTH_METHOD_ATTR_ID:
175 return ("iSCSI Auth Method");
176 case ISNS_ISCSI_SCN_BITMAP_ATTR_ID:
177 return ("iSCSI SCN Bitmap");
178 case ISNS_PG_ISCSI_NAME_ATTR_ID:
179 return ("PG iSCSI Name");
180 case ISNS_PG_PORTAL_IP_ADDR_ATTR_ID:
181 return ("PG Portal IP Addr");
182 case ISNS_PG_PORTAL_PORT_ATTR_ID:
183 return ("PG Portal TCP/UDP Port");
184 case ISNS_PG_TAG_ATTR_ID:
185 return ("PG Tag (PGT)");
186 case ISNS_PG_INDEX_ATTR_ID:
187 return ("PG Index");
188 case ISNS_DD_NAME_ATTR_ID:
189 return ("DD Name");
190 case ISNS_DD_ID_ATTR_ID:
191 return ("DD Index");
192 case ISNS_DD_ISCSI_INDEX_ATTR_ID:
193 return ("DD ISCSI Node Index");
194 case ISNS_DD_ISCSI_NAME_ATTR_ID:
195 return ("DD ISCSI Node Name");
196 case ISNS_DD_SET_NAME_ATTR_ID:
197 return ("DDS Name");
198 case ISNS_DD_SET_ID_ATTR_ID:
199 return ("DDS Index");
200 case ISNS_DD_SET_STATUS_ATTR_ID:
201 return ("DDS Status");
202 default:
203 return ("Unknown");
207 static void
208 dump_pdu(
209 isns_pdu_t *pdu,
210 int flag
213 short ver, id, len, flags, xid, seq;
215 uint8_t *payload = pdu->payload;
216 isns_resp_t *resp;
218 /* convert the data */
219 if (flag) {
220 ver = ntohs(pdu->version);
221 id = ntohs(pdu->func_id);
222 len = ntohs(pdu->payload_len);
223 flags = ntohs(pdu->flags) & 0xFFFF;
224 xid = ntohs(pdu->xid);
225 seq = ntohs(pdu->seq);
226 } else {
227 ver = pdu->version;
228 id = pdu->func_id;
229 len = pdu->payload_len;
230 flags = pdu->flags & 0xFFFF;
231 xid = pdu->xid;
232 seq = pdu->seq;
235 /* print the pdu header */
236 printf("iSNSP Version: %d\n", ver);
237 printf("Function ID: %s\n", get_func_name(id));
238 printf("PDU Length: %d\n", len);
239 printf("Flags: %x\n", flags);
240 printf(" %d... .... .... .... : ISNS_FLAG_CLIENT\n",
241 ((flags & ISNS_FLAG_CLIENT) == 0) ? 0 : 1);
242 printf(" .%d.. .... .... .... : ISNS_FLAG_SERVER\n",
243 ((flags & ISNS_FLAG_SERVER) == 0) ? 0 : 1);
244 printf(" ..%d. .... .... .... : ISNS_FLAG_AUTH_BLK_PRESENTED\n",
245 ((flags & ISNS_FLAG_AUTH_BLK_PRESENTED) == 0) ? 0 : 1);
246 printf(" ...%d .... .... .... : ISNS_FLAG_REPLACE_REG\n",
247 ((flags & ISNS_FLAG_REPLACE_REG) == 0) ? 0 : 1);
248 printf(" .... %d... .... .... : ISNS_FLAG_LAST_PDU\n",
249 ((flags & ISNS_FLAG_LAST_PDU) == 0) ? 0 : 1);
250 printf(" .... .%d.. .... .... : ISNS_FLAG_FIRST_PDU\n",
251 ((flags & ISNS_FLAG_FIRST_PDU) == 0) ? 0 : 1);
252 printf("Transaction ID: %d\n", xid);
253 printf("Sequence ID: %d\n", seq);
255 printf("Payload: ...\n");
256 if (id & ISNS_RSP_MASK) {
257 resp = (isns_resp_t *)payload;
258 printf(" ErrorCode: %d\n", ntohl(resp->status));
259 len -= 4;
260 payload += 4;
263 /* print the payload */
264 while (len > 0) {
265 isns_tlv_t *tlvp;
266 int t, l;
267 uint8_t *v;
268 char *s;
269 int i;
270 in6_addr_t *ip;
271 char pbuff[256] = { 0 };
273 tlvp = (isns_tlv_t *)payload;
275 /* convert the data */
276 t = ntohl(tlvp->attr_id);
277 l = ntohl(tlvp->attr_len);
278 v = &(tlvp->attr_value[0]);
280 /* print payload */
281 if (l > 0) {
282 printf("%s: ", get_tlv_tag_name(t));
283 switch (t) {
284 case ISNS_EID_ATTR_ID:
285 case ISNS_ISCSI_NAME_ATTR_ID:
286 case ISNS_ISCSI_ALIAS_ATTR_ID:
287 case ISNS_ISCSI_AUTH_METHOD_ATTR_ID:
288 case ISNS_PG_ISCSI_NAME_ATTR_ID:
289 case ISNS_DD_NAME_ATTR_ID:
290 case ISNS_DD_SET_NAME_ATTR_ID:
291 s = (char *)v;
292 printf("%s\n", s);
293 break;
294 case ISNS_ENTITY_PROTOCOL_ATTR_ID:
295 i = ntohl(*(uint32_t *)v);
296 printf("%s (%d)\n",
297 ((i == 1) ? "No Protocol" :
298 ((i == 2) ? "iSCSI" :
299 ((i == 3) ? "iFCP" :
300 "Others"))),
302 break;
303 case ISNS_PORTAL_IP_ADDR_ATTR_ID:
304 case ISNS_PG_PORTAL_IP_ADDR_ATTR_ID:
305 ip = (in6_addr_t *)v;
306 inet_ntop(AF_INET6, (void *)ip,
307 pbuff, sizeof (pbuff));
308 printf("%s\n", pbuff);
309 break;
310 case ISNS_PORTAL_PORT_ATTR_ID:
311 case ISNS_ESI_PORT_ATTR_ID:
312 case ISNS_SCN_PORT_ATTR_ID:
313 i = ntohl(*(uint32_t *)v);
314 printf("%d\n", (i & 0x0000FFFF));
315 printf(" .... .... %d... .... : "
316 "0=TCP\n",
317 ((i & 0x10000) == 0) ? 0 : 1);
318 break;
319 case ISNS_ISCSI_NODE_TYPE_ATTR_ID:
320 i = ntohl(*(uint32_t *)v);
321 printf("0x%x\t", i);
322 if (i & ISNS_CONTROL_NODE_TYPE) {
323 printf("Control ");
325 if (i & ISNS_INITIATOR_NODE_TYPE) {
326 printf("Initiator ");
328 if (i & ISNS_TARGET_NODE_TYPE) {
329 printf("Target ");
331 printf("\n");
332 break;
333 case ISNS_PG_TAG_ATTR_ID:
334 default:
335 i = ntohl(*(uint32_t *)v);
336 printf("%d\n", i);
337 break;
339 printf(" Attribute Tag: %s (%d)\n",
340 get_tlv_tag_name(t), t);
341 printf(" Attribute Length: %d\n", l);
342 } else {
343 printf("%s: (%d)\n", get_tlv_tag_name(t), t);
346 len -= (sizeof (uint32_t) * 2 + l);
347 payload += (sizeof (uint32_t) * 2 + l);
351 void
352 dump_pdu1(
353 isns_pdu_t *pdu
356 if (verbose_net) {
357 printf("### PDU RECEIVED ###\n");
358 dump_pdu(pdu, 0);
362 void
363 dump_pdu2(
364 isns_pdu_t *pdu
367 if (verbose_net) {
368 printf("### PDU SENT ###\n");
369 dump_pdu(pdu, 1);
373 void
374 dump_db(
377 #if 0
378 isns_list_t *list, *lista, *listb;
379 isns_dds_t *dds;
380 isns_dd_t *dd;
381 isns_iscsi2_t *iscsi2;
383 printf("### DUMP DATABASE ###\n");
384 /* dump dds(s) */
385 list = dds_list;
386 while (list != NULL) {
387 dds = list->obj.dds;
388 printf("[DDS:%d]%s(%s)\n", dds->id, dds->name,
389 dds->status ? "enabled" : "disabled");
390 lista = dds->dd_list;
391 /* dd(s) that belong to this dds */
392 while (lista != NULL) {
393 dd = lista->obj.dd;
394 printf("\t[DD:%d]%s\n", dd->id, dd->name);
395 lista = lista->next;
397 list = list->next;
399 /* dump dd(s) */
400 list = dd_list;
401 while (list != NULL) {
402 dd = list->obj.dd;
403 printf("[DD:%d]%s\n", dd->id, dd->name);
404 /* dds(s) this dd belongs to */
405 lista = dd->dds_list;
406 while (lista != NULL) {
407 dds = lista->obj.dds;
408 printf("\t[DDS:%d]%s\n", dds->id, dds->name);
409 lista = lista->next;
411 /* node(s) that this dd have */
412 listb = dd->iscsi_list;
413 while (listb != NULL) {
414 iscsi2 = listb->obj.iscsi2;
415 printf("\t[ISCSI:%d]%s\n", iscsi2->id, iscsi2->name);
416 listb = listb->next;
418 list = list->next;
420 /* dump node(s) */
421 list = iscsi_list;
422 while (list != NULL) {
423 iscsi2 = list->obj.iscsi2;
424 printf("[ISCSI:%d]%s\n", iscsi2->id, iscsi2->name);
425 lista = iscsi2->dd_list;
426 /* dd(s) that this node belongs to */
427 while (lista != NULL) {
428 dd = lista->obj.dd;
429 printf("\t[DD:%d]%s\n", dd->id, dd->name);
430 lista = lista->next;
432 list = list->next;
434 #endif
437 static void
438 test_cli_help(
441 printf("list - list all of storage node.\n");
442 printf("list dd [id] - list all of dd or one with member.\n");
443 printf("list dds [id] - list all of dd-set or one with member.\n");
445 printf("\n");
446 printf("new dd <name> - create a dd with name.\n");
447 printf("new dds <name> - create a dd-set with name.\n");
448 printf("new ddn <id> <name> - create a dd with id and name.\n");
449 printf("new ddsn <id> <name> - create a dd-set with id and name.\n");
450 printf("del dd <id> - delete a dd.\n");
451 printf("del dds <id> - delete a dd-set.\n");
453 printf("\n");
454 printf("add dd <dd_id> <node_name> - add a node to dd.\n");
455 printf("add ddn <dd_id> <node_id> - add a node to dd.\n");
456 printf("add ddsn <dds_id> <dd_id> - add a dd to dd-set.\n");
457 printf("remove dd <dd_id> <node_name> - remove a node from dd.\n");
458 printf("remove ddn <dd_id> <node_id> - remove a node from dd.\n");
459 printf("remove ddsn <dds_id> <dd_id> - remove a dd from dd-set.\n");
461 printf("\n");
462 printf("enable <dds_id> - enable a dd-set.\n");
463 printf("disable <dds_id> - disable a dd-set.\n");
465 printf("\n");
466 printf("file <f> - loading command from a file.\n");
467 printf("pause - suspend batch until enter key is pressed.\n");
469 printf("help - print this help.\n");
470 printf("quit - stop iSNS server and quit.\n");
473 static enum {
474 CMD_LIST, CMD_LISTNE, CMD_LISTP, CMD_LISTPG,
475 CMD_LISTDD, CMD_LISTDDS, CMD_LISTDDN, CMD_LISTDDSN,
476 CMD_NEWDD, CMD_NEWDDS, CMD_NEWDDN, CMD_NEWDDSN,
477 CMD_DELDD, CMD_DELDDS,
478 CMD_ENABLE, CMD_DISABLE,
479 CMD_ADDDD, CMD_ADDDDN, CMD_ADDDDSN,
480 CMD_REMDD, CMD_REMDDN, CMD_REMDDSN,
481 CMD_VIEW,
482 CMD_FILE, CMD_PAUSE,
483 CMD_HELP,
484 CMD_VERBOSE_MEMORY, CMD_VERBOSE_NET,
485 CMD_VERBOSE_PARSER, CMD_VERBOSE_TIME,
486 CMD_VERBOSE_LOCK,
487 CMD_QUIT,
488 CMD_NONE, CMD_INVALID
491 static int
492 getcmd(
493 int *argc, int *argv, char *cmd
496 int j = 0;
497 char tmp[256] = { 0 };
498 *argc = 0;
499 while (*cmd == ' ') cmd ++;
501 if (*cmd == 0) {
502 return (CMD_NONE);
503 } else if (*cmd == '?') {
504 return (CMD_HELP);
507 /* list, list dd, list dds, list dd 0 */
508 if (strncmp(cmd, "list ", 5) == 0) {
509 cmd += 5;
510 while (*cmd == ' ') cmd ++;
511 if (*cmd == 0) {
512 return (CMD_LIST);
513 } else if (*cmd == 'p') {
514 cmd ++;
515 while (*cmd == ' ') cmd ++;
516 if (*cmd == 0) {
517 return (CMD_LISTP);
519 } else if (*cmd == 'g') {
520 cmd ++;
521 while (*cmd == ' ') cmd ++;
522 if (*cmd == 0) {
523 return (CMD_LISTPG);
525 } else if (*cmd == 'e') {
526 cmd ++;
527 while (*cmd == ' ') cmd ++;
528 if (*cmd == 0) {
529 return (CMD_LISTNE);
531 } else if (strncmp(cmd, "dds ", 4) == 0) {
532 cmd += 4;
533 while (*cmd == ' ') cmd ++;
534 if (*cmd == 0) {
535 return (CMD_LISTDDS);
537 j = 0;
538 while (*cmd >= '0' && *cmd <= '9') {
539 tmp[j++] = *cmd ++;
541 tmp[j] = 0;
542 while (*cmd == ' ') cmd ++;
543 if (*cmd == 0 && j > 0) {
544 argv[(*argc)++] = atoi(tmp);
545 return (CMD_LISTDDSN);
547 } else if (strncmp(cmd, "dd ", 3) == 0) {
548 cmd += 3;
549 while (*cmd == ' ') cmd ++;
550 if (*cmd == 0) {
551 return (CMD_LISTDD);
553 j = 0;
554 while (*cmd >= '0' && *cmd <= '9') {
555 tmp[j++] = *cmd ++;
557 tmp[j] = 0;
558 while (*cmd == ' ') cmd ++;
559 if (*cmd == 0 && j > 0) {
560 argv[(*argc)++] = atoi(tmp);
561 return (CMD_LISTDDN);
564 return (CMD_INVALID);
567 /* view 0 */
568 if (strncmp(cmd, "view ", 5) == 0) {
569 cmd += 5;
570 while (*cmd == ' ') cmd ++;
571 j = 0;
572 while (*cmd >= '0' && *cmd <= '9') {
573 tmp[j++] = *cmd ++;
575 tmp[j] = 0;
576 while (*cmd == ' ') cmd ++;
577 if (*cmd == 0 && j > 0) {
578 argv[(*argc)++] = atoi(tmp);
579 return (CMD_VIEW);
581 return (CMD_INVALID);
584 /* add dd name */
585 /* add ddn/ddsn id id */
586 if (strncmp(cmd, "add ", 4) == 0) {
587 int addcmd = CMD_INVALID;
588 cmd += 4;
589 while (*cmd == ' ') cmd ++;
590 if (strncmp(cmd, "dd ", 3) == 0) {
591 cmd += 3;
592 addcmd = CMD_ADDDD;
593 } else if (strncmp(cmd, "ddn ", 4) == 0) {
594 cmd += 4;
595 addcmd = CMD_ADDDDN;
596 } else if (strncmp(cmd, "ddsn ", 5) == 0) {
597 cmd += 5;
598 addcmd = CMD_ADDDDSN;
599 } else {
600 return (CMD_INVALID);
602 while (*cmd == ' ') cmd ++;
603 j = 0;
604 while (*cmd >= '0' && *cmd <= '9') {
605 tmp[j++] = *cmd ++;
607 tmp[j] = 0;
608 if (j > 0) {
609 argv[(*argc)++] = atoi(tmp);
610 } else {
611 return (CMD_INVALID);
613 while (*cmd == ' ') cmd ++;
614 if (*cmd != 0) {
615 switch (addcmd) {
616 case CMD_ADDDDN:
617 case CMD_ADDDDSN:
618 j = 0;
619 while (*cmd >= '0' && *cmd <= '9') {
620 tmp[j++] = *cmd ++;
622 tmp[j] = 0;
623 while (*cmd == ' ') cmd ++;
624 if (*cmd == 0 && j > 0) {
625 argv[(*argc)++] = atoi(tmp);
626 } else {
627 return (CMD_INVALID);
629 break;
630 case CMD_ADDDD:
631 j = strlen(cmd);
632 while (j > 0) {
633 /* get rid of trail blank space */
634 if (cmd[j - 1] == ' ') {
635 cmd[--j] = 0;
636 } else {
637 break;
640 if (j > 0) {
641 cmd[j] = 0;
642 argv[(*argc)++] = (int)cmd;
643 } else {
644 return (CMD_INVALID);
646 break;
648 return (addcmd);
650 return (CMD_INVALID);
653 /* remove dd name */
654 /* remove ddn/ddsn id id */
655 if (strncmp(cmd, "remove ", 7) == 0) {
656 int rmcmd = CMD_INVALID;
657 cmd += 7;
658 while (*cmd == ' ') cmd ++;
659 if (strncmp(cmd, "dd ", 3) == 0) {
660 cmd += 3;
661 while (*cmd == ' ') cmd ++;
662 rmcmd = CMD_REMDD;
663 } else if (strncmp(cmd, "ddn ", 4) == 0) {
664 cmd += 4;
665 while (*cmd == ' ') cmd ++;
666 rmcmd = CMD_REMDDN;
667 } else if (strncmp(cmd, "ddsn ", 5) == 0) {
668 cmd += 5;
669 while (*cmd == ' ') cmd ++;
670 rmcmd = CMD_REMDDSN;
671 } else {
672 return (CMD_INVALID);
674 j = 0;
675 while (*cmd >= '0' && *cmd <= '9') {
676 tmp[j++] = *cmd ++;
678 tmp[j] = 0;
679 if (j > 0) {
680 argv[(*argc)++] = atoi(tmp);
681 } else {
682 return (CMD_INVALID);
684 while (*cmd == ' ') cmd ++;
685 if (*cmd != 0) {
686 switch (rmcmd) {
687 case CMD_REMDDN:
688 case CMD_REMDDSN:
689 j = 0;
690 while (*cmd >= '0' && *cmd <= '9') {
691 tmp[j++] = *cmd ++;
693 tmp[j] = 0;
694 while (*cmd == ' ') cmd ++;
695 if (*cmd == 0 && j > 0) {
696 argv[(*argc)++] = atoi(tmp);
697 } else {
698 return (CMD_INVALID);
700 break;
701 case CMD_REMDD:
702 j = strlen(cmd);
703 while (j > 0) {
704 /* get rid of trail blank space */
705 if (cmd[j - 1] == ' ') {
706 cmd[--j] = 0;
707 } else {
708 break;
711 if (j > 0) {
712 cmd[j] = 0;
713 argv[(*argc)++] = (int)cmd;
714 } else {
715 return (CMD_INVALID);
717 break;
719 return (rmcmd);
721 return (CMD_INVALID);
724 /* new dd, new dds */
725 if (strncmp(cmd, "new ", 4) == 0) {
726 int newcmd = CMD_INVALID;
727 cmd += 4;
728 while (*cmd == ' ') cmd ++;
729 if (strncmp(cmd, "dd ", 3) == 0) {
730 cmd += 3;
731 newcmd = CMD_NEWDD;
732 } else if (strncmp(cmd, "dds ", 4) == 0) {
733 cmd += 4;
734 newcmd = CMD_NEWDDS;
735 } else if (strncmp(cmd, "ddn ", 4) == 0) {
736 cmd += 4;
737 newcmd = CMD_NEWDDN;
738 } else if (strncmp(cmd, "ddsn ", 5) == 0) {
739 cmd += 5;
740 newcmd = CMD_NEWDDSN;
742 if (newcmd != CMD_INVALID) {
743 while (*cmd == ' ') cmd ++;
744 if (*cmd == 0) {
745 return (newcmd);
747 switch (newcmd) {
748 case CMD_NEWDDN:
749 case CMD_NEWDDSN:
750 j = 0;
751 while (*cmd >= '0' && *cmd <= '9') {
752 tmp[j++] = *cmd ++;
754 tmp[j] = 0;
755 if (*cmd == ' ' && j > 0) {
756 argv[(*argc)++] = atoi(tmp);
757 } else {
758 return (CMD_INVALID);
760 case CMD_NEWDD:
761 case CMD_NEWDDS:
762 while (*cmd == ' ') cmd ++;
763 if (*cmd != 0) {
764 j = strlen(cmd);
765 } else {
766 j = 0;
768 while (j > 0) {
769 /* get rid of trail blank space */
770 if (cmd[j - 1] == ' ') {
771 cmd[--j] = 0;
772 } else {
773 break;
776 if (j > 0) {
777 cmd[j] = 0;
778 argv[(*argc)++] = (int)cmd;
781 return (newcmd);
783 return (CMD_INVALID);
786 /* del dd, del dds, disable 0 */
787 if (strncmp(cmd, "del ", 4) == 0) {
788 int delcmd = CMD_INVALID;
789 cmd += 4;
790 while (*cmd == ' ') cmd ++;
791 if (strncmp(cmd, "dds ", 4) == 0) {
792 cmd += 4;
793 delcmd = CMD_DELDDS;
794 } else if (strncmp(cmd, "dd ", 3) == 0) {
795 cmd += 3;
796 delcmd = CMD_DELDD;
798 if (delcmd != CMD_INVALID) {
799 while (*cmd == ' ') cmd ++;
800 j = 0;
801 while (*cmd >= '0' && *cmd <= '9') {
802 tmp[j++] = *cmd ++;
804 tmp[j] = 0;
805 while (*cmd == ' ') cmd ++;
806 if (*cmd == 0 && j > 0) {
807 argv[(*argc)++] = atoi(tmp);
808 return (delcmd);
811 return (CMD_INVALID);
814 /* enable 0 */
815 if (strncmp(cmd, "enable ", 7) == 0) {
816 cmd += 7;
817 while (*cmd == ' ') cmd ++;
818 j = 0;
819 while (*cmd >= '0' && *cmd <= '9') {
820 tmp[j++] = *cmd ++;
822 tmp[j] = 0;
823 while (*cmd == ' ') cmd ++;
824 if (*cmd == 0 && j > 0) {
825 argv[(*argc)++] = atoi(tmp);
826 return (CMD_ENABLE);
828 return (CMD_INVALID);
831 /* disable 0 */
832 if (strncmp(cmd, "disable ", 8) == 0) {
833 cmd += 8;
834 while (*cmd == ' ') cmd ++;
835 j = 0;
836 while (*cmd >= '0' && *cmd <= '9') {
837 tmp[j++] = *cmd ++;
839 tmp[j] = 0;
840 while (*cmd == ' ') cmd ++;
841 if (*cmd == 0 && j > 0) {
842 argv[(*argc)++] = atoi(tmp);
843 return (CMD_DISABLE);
845 return (CMD_INVALID);
848 /* file */
849 if (strncmp(cmd, "file ", 5) == 0) {
850 cmd += 5;
851 while (*cmd == ' ') cmd ++;
852 if (*cmd != 0) {
853 j = strlen(cmd);
854 } else {
855 j = 0;
857 while (j > 0) {
858 /* get rid of trail blank space */
859 if (cmd[j - 1] == ' ') {
860 cmd[--j] = 0;
861 } else {
862 break;
865 if (j > 0) {
866 cmd[j] = 0;
867 argv[(*argc)++] = (int)cmd;
868 return (CMD_FILE);
870 return (CMD_INVALID);
873 /* pause */
874 if (strncmp(cmd, "pause ", 6) == 0) {
875 cmd += 6;
876 while (*cmd == ' ') cmd ++;
877 if (*cmd == 0) {
878 return (CMD_PAUSE);
880 return (CMD_INVALID);
883 /* help */
884 if (strncmp(cmd, "help ", 5) == 0) {
885 cmd += 5;
886 while (*cmd == ' ') cmd ++;
887 if (*cmd == 0) {
888 return (CMD_HELP);
890 return (CMD_INVALID);
893 /* verbose */
894 if (strncmp(cmd, "verbose ", 8) == 0) {
895 cmd += 8;
896 while (*cmd == ' ') cmd ++;
897 if (*cmd == 0) {
898 return (CMD_VERBOSE_PARSER);
899 } else if (*cmd == 'm') {
900 cmd ++;
901 while (*cmd == ' ') cmd ++;
902 if (*cmd == 0) {
903 return (CMD_VERBOSE_MEMORY);
905 } else if (*cmd == 'n') {
906 cmd ++;
907 while (*cmd == ' ') cmd ++;
908 if (*cmd == 0) {
909 return (CMD_VERBOSE_NET);
911 } else if (*cmd == 'p') {
912 cmd ++;
913 while (*cmd == ' ') cmd ++;
914 if (*cmd == 0) {
915 return (CMD_VERBOSE_PARSER);
917 } else if (*cmd == 't') {
918 cmd ++;
919 while (*cmd == ' ') cmd ++;
920 if (*cmd == 0) {
921 return (CMD_VERBOSE_TIME);
923 } else if (*cmd == 'l') {
924 cmd ++;
925 while (*cmd == ' ') cmd ++;
926 if (*cmd == 0) {
927 return (CMD_VERBOSE_LOCK);
930 return (CMD_INVALID);
933 /* quit */
934 if (strncmp(cmd, "quit ", 5) == 0) {
935 cmd += 5;
936 while (*cmd == ' ') cmd ++;
937 if (*cmd == 0) {
938 return (CMD_QUIT);
940 return (CMD_INVALID);
943 return (CMD_INVALID);
946 static void
947 print_entity(
948 char *ident,
949 isns_obj_t *obj
952 uint32_t uid;
953 uchar_t *eid;
954 uint32_t *cuid;
955 int i, num;
957 eid = obj->attrs[
958 ATTR_INDEX_ENTITY(ISNS_EID_ATTR_ID)].value.ptr;
959 uid = get_obj_uid(obj);
961 if (ident != NULL) {
962 printf("%s%d\t%s\n", ident, uid, (const char *)eid);
963 } else {
964 printf("%d\t%s\n", uid, (const char *)eid);
967 i = 0;
968 while (i < NUM_OF_CHILD[obj->type]) {
969 cuid = get_child_n(obj, i);
970 if (ident != NULL) {
971 printf("%s\t%s%d:", "child", i);
972 } else {
973 printf("\t%s%d:", "child", i);
975 if (cuid != NULL) {
976 num = *cuid ++;
977 } else {
978 num = 0;
980 while (num > 0) {
981 printf("\t%d", *cuid ++);
982 num --;
984 printf("\n");
985 i ++;
989 static void
990 print_iscsi(
991 char *ident,
992 isns_obj_t *obj
995 uchar_t *name = obj->attrs[ATTR_INDEX_ISCSI(ISNS_ISCSI_NAME_ATTR_ID)]
996 .value.ptr;
997 uchar_t *alias = obj->attrs[ATTR_INDEX_ISCSI(ISNS_ISCSI_ALIAS_ATTR_ID)]
998 .value.ptr;
999 uint32_t type = obj->attrs[
1000 ATTR_INDEX_ISCSI(ISNS_ISCSI_NODE_TYPE_ATTR_ID)].value.ui;
1001 uint32_t uid = get_obj_uid(obj);
1002 uint32_t puid = get_parent_uid(obj);
1004 if (!alias) {
1005 alias = (uchar_t *)"-";
1008 if (ident != NULL) {
1009 printf("%s%d[%d]\t%s\n", ident,
1010 uid, puid, (const char *)name);
1011 printf("%s\t%s", ident, alias);
1012 } else {
1013 printf("%d[%d]\t%s\n",
1014 uid, puid, (const char *)name);
1015 printf("\t%s", alias);
1017 if (IS_TYPE_TARGET(type)) {
1018 printf("\tTarget");
1020 if (IS_TYPE_INITIATOR(type)) {
1021 printf("\tInitiator");
1023 if (IS_TYPE_CONTROL(type)) {
1024 printf("\tControl");
1026 if (IS_TYPE_UNKNOWN(type)) {
1027 printf("\t-");
1029 printf("\n");
1032 static void
1033 print_portal(
1034 char *ident,
1035 isns_obj_t *obj
1038 char pbuff[256] = { 0 };
1039 in6_addr_t *ip = obj->attrs[
1040 ATTR_INDEX_PORTAL(ISNS_PORTAL_IP_ADDR_ATTR_ID)].value.ip;
1041 uint32_t port = obj->attrs[
1042 ATTR_INDEX_PORTAL(ISNS_PORTAL_PORT_ATTR_ID)].value.ui;
1043 uint32_t uid = get_obj_uid(obj);
1044 uint32_t puid = get_parent_uid(obj);
1046 inet_ntop(AF_INET6, (void *)ip, pbuff, sizeof (pbuff));
1047 if (ident != NULL) {
1048 printf("%s%d[%d]\t%s:%d", ident,
1049 uid, puid, pbuff, PORT_NUMBER(port));
1050 } else {
1051 printf("%d[%d]\t%s:%d",
1052 uid, puid, pbuff, PORT_NUMBER(port));
1054 printf(" %s\n", IS_PORT_UDP(port) ? "UDP" : "TCP");
1057 static void
1058 print_pg(
1059 char *ident,
1060 isns_obj_t *obj
1063 uint32_t ref;
1064 int i;
1066 char pbuff[256] = { 0 };
1067 uchar_t *name = obj->attrs[ATTR_INDEX_PG(ISNS_PG_ISCSI_NAME_ATTR_ID)]
1068 .value.ptr;
1069 in6_addr_t *ip = obj->attrs[
1070 ATTR_INDEX_PG(ISNS_PG_PORTAL_IP_ADDR_ATTR_ID)].value.ip;
1071 uint32_t port = obj->attrs[
1072 ATTR_INDEX_PG(ISNS_PG_PORTAL_PORT_ATTR_ID)].value.ui;
1073 uint32_t tag = obj->attrs[
1074 ATTR_INDEX_PG(ISNS_PG_TAG_ATTR_ID)].value.ui;
1075 uint32_t uid = get_obj_uid(obj);
1076 uint32_t puid = get_parent_uid(obj);
1078 inet_ntop(AF_INET6, (void *)ip, pbuff, sizeof (pbuff));
1079 if (ident != NULL) {
1080 printf("%s%d[%d]\t[%d] %s\n", ident,
1081 uid, puid, tag, (const char *)name);
1082 printf("%s\t%s:%d", ident, pbuff, PORT_NUMBER(port));
1083 } else {
1084 printf("%d[%d]\t[%d] %s\n",
1085 uid, puid, tag, (const char *)name);
1086 printf("\t%s:%d", pbuff, PORT_NUMBER(port));
1088 printf(" %s\n", IS_PORT_UDP(port) ? "UDP" : "TCP");
1090 if (NUM_OF_REF[obj->type] > 0) {
1091 if (ident != NULL) {
1092 printf("%s\t%s:", "ref");
1093 } else {
1094 printf("\t%s:", "ref");
1097 i = 0;
1098 while (i < NUM_OF_REF[obj->type]) {
1099 ref = get_ref_n(obj, i);
1100 printf("\t%d", ref);
1101 i ++;
1103 if (i > 0) {
1104 printf("\n");
1108 static void
1109 print_dd(
1110 char *ident,
1111 isns_obj_t *obj
1114 uchar_t *name = obj->attrs[ATTR_INDEX_DD(ISNS_DD_NAME_ATTR_ID)]
1115 .value.ptr;
1116 uint32_t uid = obj->attrs[UID_ATTR_INDEX[OBJ_DD]].value.ui;
1118 if (ident != NULL) {
1119 printf("%s%d\t%s\n", ident, uid, (const char *)name);
1120 } else {
1121 printf("%d\t%s\n", uid, (const char *)name);
1125 static void
1126 print_dds(
1127 char *ident,
1128 isns_obj_t *obj
1131 uchar_t *name = obj->attrs[ATTR_INDEX_DDS(
1132 ISNS_DD_SET_NAME_ATTR_ID)].value.ptr;
1133 uint32_t uid = obj->attrs[UID_ATTR_INDEX[OBJ_DDS]].value.ui;
1134 uint32_t enabled = obj->attrs[ATTR_INDEX_DDS(
1135 ISNS_DD_SET_STATUS_ATTR_ID)].value.ui;
1137 if (ident != NULL) {
1138 printf("%s%d\t%s\t\t(%s)\n", ident, uid,
1139 (const char *)name, enabled ? "enabled" : "disabled");
1140 } else {
1141 printf("%d\t%s\t\t(%s)\n", uid,
1142 (const char *)name, enabled ? "enabled" : "disabled");
1146 void
1147 print_object(
1148 char *ident,
1149 isns_obj_t *obj
1152 print_func[obj->type](ident, obj);
1155 /*ARGSUSED*/
1156 static int
1157 cb_print_obj_n(
1158 void *p1,
1159 void *p2
1162 isns_obj_t *obj = (isns_obj_t *)p1;
1163 print_func[obj->type](NULL, obj);
1165 return (0);
1168 static void
1169 list_pg(
1172 cache_dump_htab(OBJ_PG);
1175 static void
1176 list_portal(
1179 cache_dump_htab(OBJ_PORTAL);
1182 static void
1183 list_node(
1186 cache_dump_htab(OBJ_ISCSI);
1189 static void
1190 list_entity(
1193 cache_dump_htab(OBJ_ENTITY);
1196 static void
1197 list_dd(
1200 cache_dump_htab(OBJ_DD);
1203 static void
1204 list_ddn(
1205 uint32_t uid
1208 lookup_ctrl_t lc;
1210 bmp_t *p;
1211 uint32_t n;
1213 if (uid != 0) {
1214 setup_ddid_lcp(&lc, uid);
1215 cache_lookup(&lc, &uid, cb_print_obj_n);
1218 if (uid != 0) {
1219 printf("--------------------------------\n");
1220 get_dd_matrix(uid, &p, &n);
1221 SET_UID_LCP(&lc, OBJ_ISCSI, 0);
1222 FOR_EACH_MEMBER(p, n, uid, {
1223 lc.data[0].ui = uid;
1224 cache_lookup(&lc, NULL, cb_print_obj_n);
1226 free(p);
1227 } else {
1228 printf("no such dd.\n");
1232 static void
1233 list_ddsn(
1234 uint32_t uid
1237 lookup_ctrl_t lc;
1239 bmp_t *p;
1240 uint32_t n;
1242 if (uid != 0) {
1243 setup_ddsid_lcp(&lc, uid);
1244 cache_lookup(&lc, &uid, cb_print_obj_n);
1247 if (uid != 0) {
1248 printf("--------------------------------\n");
1249 get_dds_matrix(uid, &p, &n);
1250 SET_UID_LCP(&lc, OBJ_DD, 0);
1251 FOR_EACH_MEMBER(p, n, uid, {
1252 lc.data[0].ui = uid;
1253 cache_lookup(&lc, NULL, cb_print_obj_n);
1255 free(p);
1256 } else {
1257 printf("no such dd-set.\n");
1261 static void
1262 list_dds(
1265 cache_dump_htab(OBJ_DDS);
1268 static void
1269 new_dd_dds(
1270 int cmd_id,
1271 int argc,
1272 int *argv
1275 uint32_t buff[256];
1276 isns_pdu_t *pdu = (isns_pdu_t *)buff;
1277 uint8_t *payload = &pdu->payload[0];
1278 uint16_t payload_len = 0;
1279 isns_tlv_t *tlv;
1281 int len = 0;
1282 uint32_t uid = 0;
1283 char *name;
1285 conn_arg_t conn;
1287 pdu->version = ISNSP_VERSION;
1289 /* source attribute */
1290 tlv = (isns_tlv_t *)payload;
1291 tlv->attr_id = htonl(ISNS_ISCSI_NAME_ATTR_ID);
1292 tlv->attr_len = htonl(32);
1293 strcpy((char *)tlv->attr_value, "i am a control node.");
1294 payload += 8 + 32;
1295 payload_len += 8 + 32;
1297 /* key attributes */
1299 /* delimiter */
1300 tlv = (isns_tlv_t *)payload;
1301 tlv->attr_id = htonl(ISNS_DELIMITER_ATTR_ID);
1302 tlv->attr_len = htonl(0);
1303 payload += 8 + 0;
1304 payload_len += 8 + 0;
1306 /* operating attributes */
1307 switch (cmd_id) {
1308 case CMD_NEWDD:
1309 pdu->func_id = ISNS_DD_REG;
1310 if (argc == 1) {
1311 name = (char *)argv[0];
1312 len = strlen(name) + 1;
1313 len += 4 - (len % 4);
1315 tlv = (isns_tlv_t *)payload;
1316 tlv->attr_id = htonl(ISNS_DD_NAME_ATTR_ID);
1317 tlv->attr_len = htonl(len);
1318 if (len > 0) {
1319 strcpy((char *)tlv->attr_value, name);
1321 payload_len += 8 + len;
1322 break;
1323 case CMD_NEWDDS:
1324 pdu->func_id = ISNS_DDS_REG;
1325 if (argc == 1) {
1326 name = (char *)argv[0];
1327 len = strlen(name) + 1;
1328 len += 4 - (len % 4);
1330 tlv = (isns_tlv_t *)payload;
1331 tlv->attr_id = htonl(ISNS_DD_SET_NAME_ATTR_ID);
1332 tlv->attr_len = htonl(len);
1333 if (len > 0) {
1334 strcpy((char *)tlv->attr_value, name);
1336 payload_len += 8 + len;
1337 break;
1338 case CMD_NEWDDN:
1339 pdu->func_id = ISNS_DD_REG;
1340 switch (argc) {
1341 case 2:
1342 name = (char *)argv[1];
1343 len = strlen(name) + 1;
1344 len += 4 - (len % 4);
1345 case 1:
1346 uid = argv[0];
1348 tlv = (isns_tlv_t *)payload;
1349 tlv->attr_id = htonl(ISNS_DD_NAME_ATTR_ID);
1350 tlv->attr_len = htonl(len);
1351 if (len > 0) {
1352 strcpy((char *)tlv->attr_value, name);
1354 payload += 8 + len;
1355 payload_len += 8 + len;
1356 if (uid > 0) {
1357 tlv = (isns_tlv_t *)payload;
1358 tlv->attr_id = htonl(ISNS_DD_ID_ATTR_ID);
1359 tlv->attr_len = htonl(4);
1360 *(uint32_t *)tlv->attr_value = htonl(uid);
1361 payload_len += 8 + 4;
1363 break;
1364 case CMD_NEWDDSN:
1365 pdu->func_id = ISNS_DDS_REG;
1366 switch (argc) {
1367 case 2:
1368 name = (char *)argv[1];
1369 len = strlen(name) + 1;
1370 len += 4 - (len % 4);
1371 case 1:
1372 uid = argv[0];
1374 tlv = (isns_tlv_t *)payload;
1375 tlv->attr_id = htonl(ISNS_DD_SET_NAME_ATTR_ID);
1376 tlv->attr_len = htonl(len);
1377 if (len > 0) {
1378 strcpy((char *)tlv->attr_value, name);
1380 payload_len += 8 + len;
1381 payload += 8 + len;
1382 if (uid > 0) {
1383 tlv = (isns_tlv_t *)payload;
1384 tlv->attr_id = htonl(ISNS_DD_SET_ID_ATTR_ID);
1385 tlv->attr_len = htonl(4);
1386 *(uint32_t *)tlv->attr_value = htonl(uid);
1387 payload_len += 8 + 4;
1389 break;
1390 default:
1391 break;
1394 pdu->payload_len = payload_len;
1396 dump_pdu1(pdu);
1398 conn.in_packet.pdu = pdu;
1399 conn.out_packet.pdu = NULL;
1400 conn.out_packet.sz = 0;
1402 if (packet_split_verify(&conn) == 0) {
1403 cache_lock(conn.lock);
1404 conn.handler(&conn);
1405 conn.ec = cache_unlock(conn.lock, conn.ec);
1408 if (conn.out_packet.pdu != NULL) {
1409 pdu_update_code(conn.out_packet.pdu,
1410 &conn.out_packet.pl, conn.ec);
1411 dump_pdu2(conn.out_packet.pdu);
1412 free(conn.out_packet.pdu);
1413 } else if (conn.ec != 0) {
1414 printf("operation failed[%d].\n", conn.ec);
1418 static void
1419 del_dd_dds(
1420 int cmd_id,
1421 int uid
1424 uint32_t buff[256];
1425 isns_pdu_t *pdu = (isns_pdu_t *)buff;
1426 uint8_t *payload = &pdu->payload[0];
1427 uint16_t payload_len = 0;
1428 isns_tlv_t *tlv;
1430 uint32_t tag;
1432 conn_arg_t conn;
1434 if (uid == 0) {
1435 return;
1438 pdu->version = ISNSP_VERSION;
1440 if (cmd_id == CMD_DELDD) {
1441 tag = ISNS_DD_ID_ATTR_ID;
1442 pdu->func_id = ISNS_DD_DEREG;
1443 } else {
1444 tag = ISNS_DD_SET_ID_ATTR_ID;
1445 pdu->func_id = ISNS_DDS_DEREG;
1448 /* source attribute */
1449 tlv = (isns_tlv_t *)payload;
1450 tlv->attr_id = htonl(ISNS_ISCSI_NAME_ATTR_ID);
1451 tlv->attr_len = htonl(32);
1452 strcpy((char *)tlv->attr_value, "i am a control node.");
1453 payload_len += 8 + 32;
1454 payload += 8 + 32;
1456 /* key attributes */
1457 tlv = (isns_tlv_t *)payload;
1458 tlv->attr_id = htonl(tag);
1459 tlv->attr_len = htonl(4);
1460 *(uint32_t *)tlv->attr_value = htonl(uid);
1461 payload_len += 8 + 4;
1462 payload += 8 + 4;
1464 /* delimiter */
1465 tlv = (isns_tlv_t *)payload;
1466 tlv->attr_id = htonl(ISNS_DELIMITER_ATTR_ID);
1467 tlv->attr_len = htonl(0);
1468 payload_len += 8 + 0;
1469 payload += 8 + 0;
1471 /* operating attributes */
1473 pdu->payload_len = payload_len;
1475 dump_pdu1(pdu);
1477 conn.in_packet.pdu = pdu;
1478 conn.out_packet.pdu = NULL;
1479 conn.out_packet.sz = 0;
1481 if (packet_split_verify(&conn) == 0) {
1482 cache_lock(conn.lock);
1483 conn.handler(&conn);
1484 conn.ec = cache_unlock(conn.lock, conn.ec);
1487 if (conn.out_packet.pdu != NULL) {
1488 pdu_update_code(conn.out_packet.pdu,
1489 &conn.out_packet.pl, conn.ec);
1490 dump_pdu2(conn.out_packet.pdu);
1491 free(conn.out_packet.pdu);
1492 } else if (conn.ec != 0) {
1493 printf("operation failed[%d].\n", conn.ec);
1497 static void
1498 update_dds(
1499 int cmd_id,
1500 int uid
1503 uint32_t buff[256];
1504 isns_pdu_t *pdu = (isns_pdu_t *)buff;
1505 uint8_t *payload = &pdu->payload[0];
1506 uint16_t payload_len = 0;
1507 isns_tlv_t *tlv;
1509 conn_arg_t conn;
1511 if (uid == 0) {
1512 return;
1515 pdu->version = ISNSP_VERSION;
1517 pdu->func_id = ISNS_DDS_REG;
1519 /* source attribute */
1520 tlv = (isns_tlv_t *)payload;
1521 tlv->attr_id = htonl(ISNS_ISCSI_NAME_ATTR_ID);
1522 tlv->attr_len = htonl(32);
1523 strcpy((char *)tlv->attr_value, "i am a control node.");
1524 payload_len += 8 + 32;
1525 payload += 8 + 32;
1527 /* key attributes */
1528 tlv = (isns_tlv_t *)payload;
1529 tlv->attr_id = htonl(ISNS_DD_SET_ID_ATTR_ID);
1530 tlv->attr_len = htonl(4);
1531 *(uint32_t *)tlv->attr_value = htonl(uid);
1532 payload_len += 8 + 4;
1533 payload += 8 + 4;
1535 /* delimiter */
1536 tlv = (isns_tlv_t *)payload;
1537 tlv->attr_id = htonl(ISNS_DELIMITER_ATTR_ID);
1538 tlv->attr_len = htonl(0);
1539 payload_len += 8 + 0;
1540 payload += 8 + 0;
1542 /* operating attributes */
1543 tlv = (isns_tlv_t *)payload;
1544 tlv->attr_id = htonl(ISNS_DD_SET_STATUS_ATTR_ID);
1545 tlv->attr_len = htonl(4);
1546 if (cmd_id == CMD_ENABLE) {
1547 *(uint32_t *)tlv->attr_value = htonl(1);
1548 } else {
1549 *(uint32_t *)tlv->attr_value = htonl(0);
1551 payload_len += 8 + 4;
1553 pdu->payload_len = payload_len;
1555 dump_pdu1(pdu);
1557 conn.in_packet.pdu = pdu;
1558 conn.out_packet.pdu = NULL;
1559 conn.out_packet.sz = 0;
1561 if (packet_split_verify(&conn) == 0) {
1562 cache_lock(conn.lock);
1563 conn.handler(&conn);
1564 conn.ec = cache_unlock(conn.lock, conn.ec);
1567 if (conn.out_packet.pdu != NULL) {
1568 pdu_update_code(conn.out_packet.pdu,
1569 &conn.out_packet.pl, conn.ec);
1570 dump_pdu2(conn.out_packet.pdu);
1571 free(conn.out_packet.pdu);
1572 } else if (conn.ec != 0) {
1573 printf("operation failed[%d].\n", conn.ec);
1577 static void
1578 update_member(
1579 int cmd_id,
1580 int *argv
1583 uint32_t buff[256];
1584 isns_pdu_t *pdu = (isns_pdu_t *)buff;
1585 uint8_t *payload = &pdu->payload[0];
1586 uint16_t payload_len = 0;
1587 isns_tlv_t *tlv;
1588 uint32_t key_tag, op_tag, op_len;
1590 uint32_t uid = argv[0];
1591 uint32_t m_id;
1592 char *m_name;
1594 conn_arg_t conn;
1596 if (uid == 0) {
1597 printf("operation failed.\n");
1598 return;
1601 pdu->version = ISNSP_VERSION;
1603 switch (cmd_id) {
1604 case CMD_ADDDD:
1605 case CMD_ADDDDN:
1606 pdu->func_id = ISNS_DD_REG;
1607 break;
1608 case CMD_REMDD:
1609 case CMD_REMDDN:
1610 pdu->func_id = ISNS_DD_DEREG;
1611 break;
1612 case CMD_ADDDDSN:
1613 pdu->func_id = ISNS_DDS_REG;
1614 break;
1615 case CMD_REMDDSN:
1616 pdu->func_id = ISNS_DDS_DEREG;
1617 break;
1619 switch (cmd_id) {
1620 case CMD_ADDDD:
1621 case CMD_REMDD:
1622 key_tag = ISNS_DD_ID_ATTR_ID;
1623 op_tag = ISNS_DD_ISCSI_NAME_ATTR_ID;
1624 m_name = (char *)argv[1];
1625 op_len = strlen(m_name);
1626 op_len += 4 - (op_len % 4);
1627 break;
1628 case CMD_ADDDDN:
1629 case CMD_REMDDN:
1630 key_tag = ISNS_DD_ID_ATTR_ID;
1631 op_tag = ISNS_DD_ISCSI_INDEX_ATTR_ID;
1632 m_id = argv[1];
1633 op_len = 4;
1634 break;
1635 case CMD_ADDDDSN:
1636 case CMD_REMDDSN:
1637 key_tag = ISNS_DD_SET_ID_ATTR_ID;
1638 op_tag = ISNS_DD_ID_ATTR_ID;
1639 m_id = argv[1];
1640 op_len = 4;
1641 break;
1644 /* source attribute */
1645 tlv = (isns_tlv_t *)payload;
1646 tlv->attr_id = htonl(ISNS_ISCSI_NAME_ATTR_ID);
1647 tlv->attr_len = htonl(32);
1648 strcpy((char *)tlv->attr_value, "i am a control node.");
1649 payload_len += 8 + 32;
1650 payload += 8 + 32;
1652 /* key attributes */
1653 tlv = (isns_tlv_t *)payload;
1654 tlv->attr_id = htonl(key_tag);
1655 tlv->attr_len = htonl(4);
1656 *(uint32_t *)tlv->attr_value = htonl(uid);
1657 payload_len += 8 + 4;
1658 payload += 8 + 4;
1660 /* delimiter */
1661 tlv = (isns_tlv_t *)payload;
1662 tlv->attr_id = htonl(ISNS_DELIMITER_ATTR_ID);
1663 tlv->attr_len = htonl(0);
1664 payload_len += 8 + 0;
1665 payload += 8 + 0;
1667 /* operating attributes */
1668 tlv = (isns_tlv_t *)payload;
1669 tlv->attr_id = htonl(op_tag);
1670 tlv->attr_len = htonl(op_len);
1671 switch (cmd_id) {
1672 case CMD_ADDDD:
1673 case CMD_REMDD:
1674 strcpy((char *)tlv->attr_value, m_name);
1675 break;
1676 case CMD_ADDDDN:
1677 case CMD_ADDDDSN:
1678 case CMD_REMDDN:
1679 case CMD_REMDDSN:
1680 *(uint32_t *)tlv->attr_value = htonl(m_id);
1681 break;
1683 payload_len += 8 + op_len;
1685 pdu->payload_len = payload_len;
1687 dump_pdu1(pdu);
1689 conn.in_packet.pdu = pdu;
1690 conn.out_packet.pdu = NULL;
1691 conn.out_packet.sz = 0;
1693 if (packet_split_verify(&conn) == 0) {
1694 cache_lock(conn.lock);
1695 conn.handler(&conn);
1696 conn.ec = cache_unlock(conn.lock, conn.ec);
1699 if (conn.out_packet.pdu != NULL) {
1700 pdu_update_code(conn.out_packet.pdu,
1701 &conn.out_packet.pl, conn.ec);
1702 dump_pdu2(conn.out_packet.pdu);
1703 free(conn.out_packet.pdu);
1704 } else if (conn.ec != 0) {
1705 printf("operation failed[%d].\n", conn.ec);
1709 static void
1710 cmd_file(
1711 char *file
1714 char i = 0, ch, cmd[256];
1715 FILE *f = fopen(file, "r");
1716 if (f != NULL) {
1717 while ((ch = fgetc(f)) != 0 && ch != EOF) {
1718 if (ch == '\t') {
1719 cmd[i++] = ' ';
1720 } else if (ch != '\n') {
1721 cmd[i++] = ch;
1722 } else {
1723 cmd[i ++] = ' ';
1724 cmd[i] = 0;
1725 i = 0;
1726 printf("%s\n", cmd);
1727 if (run_cmd(cmd) != 0) {
1728 break;
1732 fclose(f);
1733 } else {
1734 printf("Cannot open file %s.\n", file);
1738 static int
1739 run_cmd(
1740 char *cmd
1743 int argc, argv[32];
1744 int cmd_id;
1745 cmd_id = getcmd(&argc, argv, cmd);
1746 switch (cmd_id) {
1747 case CMD_LIST:
1748 list_node();
1749 break;
1750 case CMD_LISTNE:
1751 list_entity();
1752 break;
1753 case CMD_LISTP:
1754 list_portal();
1755 break;
1756 case CMD_LISTPG:
1757 list_pg();
1758 break;
1759 case CMD_LISTDD:
1760 list_dd();
1761 break;
1762 case CMD_LISTDDS:
1763 list_dds();
1764 break;
1765 case CMD_LISTDDN:
1766 list_ddn(argv[0]);
1767 break;
1768 case CMD_LISTDDSN:
1769 list_ddsn(argv[0]);
1770 break;
1771 case CMD_NEWDD:
1772 case CMD_NEWDDS:
1773 case CMD_NEWDDN:
1774 case CMD_NEWDDSN:
1775 new_dd_dds(cmd_id, argc, argv);
1776 break;
1777 case CMD_DELDD:
1778 case CMD_DELDDS:
1779 del_dd_dds(cmd_id, argv[0]);
1780 break;
1781 case CMD_ENABLE:
1782 case CMD_DISABLE:
1783 update_dds(cmd_id, argv[0]);
1784 break;
1785 case CMD_ADDDD:
1786 case CMD_ADDDDN:
1787 case CMD_ADDDDSN:
1788 case CMD_REMDD:
1789 case CMD_REMDDN:
1790 case CMD_REMDDSN:
1791 update_member(cmd_id, argv);
1792 break;
1793 case CMD_PAUSE:
1794 printf("Press enter to continue...");
1795 getchar();
1796 break;
1797 case CMD_FILE:
1798 cmd_file((char *)argv[0]);
1799 break;
1800 case CMD_HELP:
1801 test_cli_help();
1802 break;
1803 case CMD_VERBOSE_MEMORY:
1804 verbose_mc = !verbose_mc;
1805 break;
1806 case CMD_VERBOSE_NET:
1807 verbose_net = !verbose_net;
1808 break;
1809 case CMD_VERBOSE_TIME:
1810 verbose_tc = !verbose_tc;
1811 break;
1812 case CMD_VERBOSE_LOCK:
1813 verbose_lock = !verbose_lock;
1814 break;
1815 case CMD_VERBOSE_PARSER:
1816 verbose_parser = !verbose_parser;
1817 break;
1818 case CMD_QUIT:
1819 /* clean up cli */
1820 /* notify sys control */
1821 shutdown_server();
1822 return (1);
1823 case CMD_NONE:
1824 break;
1825 default:
1826 printf("invalid command\n");
1827 break;
1829 if (cmd_id != CMD_NONE) {
1830 printf("\n>");
1831 } else {
1832 printf(">");
1834 return (0);
1837 /*ARGSUSED*/
1838 void *cli_test(void *arg) {
1839 char i = 0, ch, cmd[256];
1841 printf("iSNS Server test CLI.\n");
1842 printf("Copyright 2007 Sun Microsystems, Inc.\n");
1844 printf("\n>");
1845 while ((ch = getchar()) != 0 && ch != EOF) {
1846 if (ch == '\t') {
1847 cmd[i++] = ' ';
1848 } else if (ch != '\n') {
1849 cmd[i++] = ch;
1850 } else {
1851 cmd[i ++] = ' ';
1852 cmd[i] = 0;
1853 i = 0;
1854 if (run_cmd(cmd) != 0) {
1855 break;
1860 return (NULL);
1862 #endif