xref: /titanic_54/usr/src/cmd/fcinfo/fcinfo.c (revision 7ff836697c120cb94bd30d5c2204eb9b74718e4c)
1fcf3ce44SJohn Forte /*
2fcf3ce44SJohn Forte  * CDDL HEADER START
3fcf3ce44SJohn Forte  *
4fcf3ce44SJohn Forte  * The contents of this file are subject to the terms of the
5fcf3ce44SJohn Forte  * Common Development and Distribution License (the "License").
6fcf3ce44SJohn Forte  * You may not use this file except in compliance with the License.
7fcf3ce44SJohn Forte  *
8fcf3ce44SJohn Forte  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fcf3ce44SJohn Forte  * or http://www.opensolaris.org/os/licensing.
10fcf3ce44SJohn Forte  * See the License for the specific language governing permissions
11fcf3ce44SJohn Forte  * and limitations under the License.
12fcf3ce44SJohn Forte  *
13fcf3ce44SJohn Forte  * When distributing Covered Code, include this CDDL HEADER in each
14fcf3ce44SJohn Forte  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fcf3ce44SJohn Forte  * If applicable, add the following below this CDDL HEADER, with the
16fcf3ce44SJohn Forte  * fields enclosed by brackets "[]" replaced with your own identifying
17fcf3ce44SJohn Forte  * information: Portions Copyright [yyyy] [name of copyright owner]
18fcf3ce44SJohn Forte  *
19fcf3ce44SJohn Forte  * CDDL HEADER END
20fcf3ce44SJohn Forte  */
21fcf3ce44SJohn Forte /*
222a8164dfSZhong Wang  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fcf3ce44SJohn Forte  * Use is subject to license terms.
24fcf3ce44SJohn Forte  */
25fcf3ce44SJohn Forte 
26fcf3ce44SJohn Forte #include <fcinfo.h>
27fcf3ce44SJohn Forte 
28fcf3ce44SJohn Forte 
29fcf3ce44SJohn Forte 
30fcf3ce44SJohn Forte #define	VERSION_STRING_MAX_LEN	10
31fcf3ce44SJohn Forte /*
32fcf3ce44SJohn Forte  * Version number:
33fcf3ce44SJohn Forte  *  MAJOR - This should only change when there is an incompatible change made
34fcf3ce44SJohn Forte  *  to the interfaces or the output.
35fcf3ce44SJohn Forte  *
36fcf3ce44SJohn Forte  *  MINOR - This should change whenever there is a new command or new feature
37fcf3ce44SJohn Forte  *  with no incompatible change.
38fcf3ce44SJohn Forte  */
39fcf3ce44SJohn Forte #define	VERSION_STRING_MAJOR	    "1"
40fcf3ce44SJohn Forte #define	VERSION_STRING_MINOR	    "0"
41fcf3ce44SJohn Forte 
42fcf3ce44SJohn Forte #define	OPTIONSTRING1		"HBA Port WWN"
43fcf3ce44SJohn Forte #define	OPTIONSTRING2		"HBA Node WWN"
44fcf3ce44SJohn Forte /* forward declarations */
45fcf3ce44SJohn Forte static int listHbaPortFunc(int, char **, cmdOptions_t *, void *);
46fcf3ce44SJohn Forte static int listRemotePortFunc(int, char **, cmdOptions_t *, void *);
47fcf3ce44SJohn Forte static int listLogicalUnitFunc(int, char **, cmdOptions_t *, void *);
48fcf3ce44SJohn Forte static int npivCreatePortFunc(int, char **, cmdOptions_t *, void *);
49fcf3ce44SJohn Forte static int npivDeletePortFunc(int, char **, cmdOptions_t *, void *);
50fcf3ce44SJohn Forte static int npivCreatePortListFunc(int, char **, cmdOptions_t *, void *);
51fcf3ce44SJohn Forte static int npivListHbaPortFunc(int, char **, cmdOptions_t *, void *);
52fcf3ce44SJohn Forte static int npivListRemotePortFunc(int, char **, cmdOptions_t *, void *);
532a8164dfSZhong Wang static int fcoeAdmCreatePortFunc(int, char **, cmdOptions_t *, void *);
542a8164dfSZhong Wang static int fcoeListPortsFunc(int, char **, cmdOptions_t *, void *);
552a8164dfSZhong Wang static int fcoeAdmDeletePortFunc(int, char **, cmdOptions_t *, void *);
56fcf3ce44SJohn Forte static char *getExecBasename(char *);
57fcf3ce44SJohn Forte 
58fcf3ce44SJohn Forte /*
59fcf3ce44SJohn Forte  * Add new options here
60fcf3ce44SJohn Forte  *
61fcf3ce44SJohn Forte  * Optional option-arguments are not allowed by CLIP
62fcf3ce44SJohn Forte  */
63fcf3ce44SJohn Forte optionTbl_t fcinfolongOptions[] = {
64fcf3ce44SJohn Forte 	{"port", required_argument,	'p', OPTIONSTRING1},
65fcf3ce44SJohn Forte 	{"target", no_argument,		't', NULL},
66fcf3ce44SJohn Forte 	{"initiator", no_argument,	'i', NULL},
67fcf3ce44SJohn Forte 	{"linkstat", no_argument,	'l', NULL},
68fcf3ce44SJohn Forte 	{"scsi-target", no_argument,	's', NULL},
692a8164dfSZhong Wang 	{"fcoe", no_argument,		'e', NULL},
70fcf3ce44SJohn Forte 	{"verbose", no_argument,	'v', NULL},
71fcf3ce44SJohn Forte 	{NULL, 0, 0}
72fcf3ce44SJohn Forte };
73fcf3ce44SJohn Forte 
74fcf3ce44SJohn Forte optionTbl_t fcadmlongOptions[] = {
75fcf3ce44SJohn Forte 	{"port", required_argument,	'p', OPTIONSTRING1},
76fcf3ce44SJohn Forte 	{"node", required_argument,	'n', OPTIONSTRING2},
77fcf3ce44SJohn Forte 	{"linkstat", no_argument,	'l', NULL},
78fcf3ce44SJohn Forte 	{"scsi-target", no_argument,	's', NULL},
792a8164dfSZhong Wang 	{"fcoe-force-promisc", no_argument, 'f', NULL},
802a8164dfSZhong Wang 	{"target", no_argument,		't', NULL},
81*7ff83669SZhong Wang 	{"initiator", no_argument,	'i', NULL},
82fcf3ce44SJohn Forte 	{NULL, 0, 0}
83fcf3ce44SJohn Forte };
84fcf3ce44SJohn Forte 
85fcf3ce44SJohn Forte /*
86fcf3ce44SJohn Forte  * Add new subcommands here
87fcf3ce44SJohn Forte  */
88fcf3ce44SJohn Forte subCommandProps_t fcinfosubcommands[] = {
892a8164dfSZhong Wang 	{"hba-port", listHbaPortFunc, "itel", NULL, NULL,
90fcf3ce44SJohn Forte 		OPERAND_OPTIONAL_MULTIPLE, "WWN"},
91fcf3ce44SJohn Forte 	{"remote-port", listRemotePortFunc, "lsp", "p", NULL,
92fcf3ce44SJohn Forte 		OPERAND_OPTIONAL_MULTIPLE, "WWN"},
93fcf3ce44SJohn Forte 	{"logical-unit", listLogicalUnitFunc, "v", NULL, NULL,
94fcf3ce44SJohn Forte 		OPERAND_OPTIONAL_MULTIPLE, "OS Device Path"},
95fcf3ce44SJohn Forte 	{"lu", listLogicalUnitFunc, "v", NULL, NULL,
96fcf3ce44SJohn Forte 		OPERAND_OPTIONAL_MULTIPLE, "OS Device Path"},
97fcf3ce44SJohn Forte 	{NULL, 0, NULL, NULL, NULL, 0, NULL, NULL}
98fcf3ce44SJohn Forte };
99fcf3ce44SJohn Forte 
100fcf3ce44SJohn Forte subCommandProps_t fcadmsubcommands[] = {
101fcf3ce44SJohn Forte 	{"create-npiv-port",
102fcf3ce44SJohn Forte 	    npivCreatePortFunc, "pn", NULL, NULL,
103fcf3ce44SJohn Forte 	    OPERAND_MANDATORY_SINGLE,  "WWN"},
104fcf3ce44SJohn Forte 	{"delete-npiv-port",
105fcf3ce44SJohn Forte 	    npivDeletePortFunc, "p", "p", NULL,
106fcf3ce44SJohn Forte 	    OPERAND_MANDATORY_SINGLE,  "WWN"},
107fcf3ce44SJohn Forte 	{"hba-port",
108fcf3ce44SJohn Forte 	    npivListHbaPortFunc, "l", NULL, NULL,
109fcf3ce44SJohn Forte 	    OPERAND_OPTIONAL_MULTIPLE, "WWN"},
110fcf3ce44SJohn Forte 	{"remote-port",
111fcf3ce44SJohn Forte 	    npivListRemotePortFunc, "psl", "p", NULL,
112fcf3ce44SJohn Forte 	    OPERAND_OPTIONAL_MULTIPLE, "WWN"},
113fcf3ce44SJohn Forte 	{"create-port-list",
114fcf3ce44SJohn Forte 	    npivCreatePortListFunc, NULL, NULL, NULL,
115fcf3ce44SJohn Forte 	    OPERAND_NONE, NULL},
1162a8164dfSZhong Wang 	{"create-fcoe-port",
117*7ff83669SZhong Wang 	    fcoeAdmCreatePortFunc, "itpnf", NULL, NULL,
1182a8164dfSZhong Wang 		OPERAND_MANDATORY_SINGLE, "Network Interface Name"},
1192a8164dfSZhong Wang 	{"delete-fcoe-port",
1202a8164dfSZhong Wang 	    fcoeAdmDeletePortFunc, NULL, NULL, NULL,
1212a8164dfSZhong Wang 		OPERAND_MANDATORY_SINGLE, "Network Interface Name"},
1222a8164dfSZhong Wang 	{"list-fcoe-ports",
123*7ff83669SZhong Wang 	    fcoeListPortsFunc, "it", NULL, NULL,
1242a8164dfSZhong Wang 		OPERAND_NONE, NULL},
125fcf3ce44SJohn Forte 	{NULL, 0, NULL, NULL, NULL, 0, NULL, NULL}
126fcf3ce44SJohn Forte };
1272a8164dfSZhong Wang 
128fcf3ce44SJohn Forte /*
129fcf3ce44SJohn Forte  * Pass in options/arguments, rest of arguments
130fcf3ce44SJohn Forte  */
131fcf3ce44SJohn Forte /*ARGSUSED*/
132fcf3ce44SJohn Forte static int
133fcf3ce44SJohn Forte listHbaPortFunc(int objects, char *argv[], cmdOptions_t *options, void *addArgs)
134fcf3ce44SJohn Forte {
135fcf3ce44SJohn Forte 	return (fc_util_list_hbaport(objects, argv, options));
136fcf3ce44SJohn Forte }
137fcf3ce44SJohn Forte 
138fcf3ce44SJohn Forte /*
139fcf3ce44SJohn Forte  * Pass in options/arguments, rest of arguments
140fcf3ce44SJohn Forte  */
141fcf3ce44SJohn Forte /*ARGSUSED*/
142fcf3ce44SJohn Forte static int
143fcf3ce44SJohn Forte listRemotePortFunc(int objects, char *argv[], cmdOptions_t *options,
144fcf3ce44SJohn Forte     void *addArgs)
145fcf3ce44SJohn Forte {
146fcf3ce44SJohn Forte 	return (fc_util_list_remoteport(objects, argv, options));
147fcf3ce44SJohn Forte }
148fcf3ce44SJohn Forte 
149fcf3ce44SJohn Forte /*
150fcf3ce44SJohn Forte  * Pass in options/arguments, rest of arguments
151fcf3ce44SJohn Forte  */
152fcf3ce44SJohn Forte /*ARGSUSED*/
153fcf3ce44SJohn Forte static int
154fcf3ce44SJohn Forte listLogicalUnitFunc(int objects, char *argv[], cmdOptions_t *options,
155fcf3ce44SJohn Forte     void *addArgs)
156fcf3ce44SJohn Forte {
157fcf3ce44SJohn Forte 	return (fc_util_list_logicalunit(objects, argv, options));
158fcf3ce44SJohn Forte }
159fcf3ce44SJohn Forte 
160fcf3ce44SJohn Forte /*
161fcf3ce44SJohn Forte  * Pass in options/arguments, rest of arguments
162fcf3ce44SJohn Forte  */
163fcf3ce44SJohn Forte /*ARGSUSED*/
164fcf3ce44SJohn Forte static int
165fcf3ce44SJohn Forte npivCreatePortFunc(int objects, char *argv[],
166fcf3ce44SJohn Forte     cmdOptions_t *options, void *addArgs) {
167fcf3ce44SJohn Forte 	return (fc_util_create_npivport(objects, argv, options));
168fcf3ce44SJohn Forte }
169fcf3ce44SJohn Forte 
170fcf3ce44SJohn Forte static int
171fcf3ce44SJohn Forte npivCreatePortListFunc(int objects, char *argv[],
172fcf3ce44SJohn Forte     cmdOptions_t *options, void *addArgs) {
173fcf3ce44SJohn Forte 	if ((objects == 0) && addArgs && options && argv) {
174fcf3ce44SJohn Forte 		objects = 1;
175fcf3ce44SJohn Forte 	}
176fcf3ce44SJohn Forte 	return (fc_util_create_portlist());
177fcf3ce44SJohn Forte }
178fcf3ce44SJohn Forte 
179fcf3ce44SJohn Forte /*
180fcf3ce44SJohn Forte  * Pass in options/arguments, rest of arguments
181fcf3ce44SJohn Forte  */
182fcf3ce44SJohn Forte /*ARGSUSED*/
183fcf3ce44SJohn Forte static int
184fcf3ce44SJohn Forte npivDeletePortFunc(int objects, char *argv[],
185fcf3ce44SJohn Forte     cmdOptions_t *options, void *addArgs) {
186fcf3ce44SJohn Forte 	return (fc_util_delete_npivport(objects, argv, options));
187fcf3ce44SJohn Forte }
188fcf3ce44SJohn Forte 
189fcf3ce44SJohn Forte /*
190fcf3ce44SJohn Forte  * Pass in options/arguments, rest of arguments
191fcf3ce44SJohn Forte  */
192fcf3ce44SJohn Forte /*ARGSUSED*/
193fcf3ce44SJohn Forte static int
194fcf3ce44SJohn Forte npivListHbaPortFunc(int objects, char *argv[],
195fcf3ce44SJohn Forte     cmdOptions_t *options, void *addArgs) {
196fcf3ce44SJohn Forte 	return (fc_util_list_hbaport(objects, argv, options));
197fcf3ce44SJohn Forte }
198fcf3ce44SJohn Forte 
199fcf3ce44SJohn Forte /*
200fcf3ce44SJohn Forte  * Pass in options/arguments, rest of arguments
201fcf3ce44SJohn Forte  */
202fcf3ce44SJohn Forte /*ARGSUSED*/
203fcf3ce44SJohn Forte static int
204fcf3ce44SJohn Forte npivListRemotePortFunc(int objects, char *argv[],
205fcf3ce44SJohn Forte     cmdOptions_t *options, void *addArgs) {
206fcf3ce44SJohn Forte 	return (fc_util_list_remoteport(objects, argv, options));
207fcf3ce44SJohn Forte }
208fcf3ce44SJohn Forte 
209fcf3ce44SJohn Forte /*
2102a8164dfSZhong Wang  * Pass in options/arguments, rest of arguments
2112a8164dfSZhong Wang  */
2122a8164dfSZhong Wang /*ARGSUSED*/
2132a8164dfSZhong Wang static int
2142a8164dfSZhong Wang fcoeAdmCreatePortFunc(int objects, char *argv[], cmdOptions_t *options,
2152a8164dfSZhong Wang     void *addArgs)
2162a8164dfSZhong Wang {
2172a8164dfSZhong Wang 	return (fcoe_adm_create_port(objects, argv, options));
2182a8164dfSZhong Wang }
2192a8164dfSZhong Wang 
2202a8164dfSZhong Wang /*
2212a8164dfSZhong Wang  * Pass in options/arguments, rest of arguments
2222a8164dfSZhong Wang  */
2232a8164dfSZhong Wang /*ARGSUSED*/
2242a8164dfSZhong Wang static int
2252a8164dfSZhong Wang fcoeAdmDeletePortFunc(int objects, char *argv[], cmdOptions_t *options,
2262a8164dfSZhong Wang     void *addArgs)
2272a8164dfSZhong Wang {
2282a8164dfSZhong Wang 	return (fcoe_adm_delete_port(objects, argv));
2292a8164dfSZhong Wang }
2302a8164dfSZhong Wang 
2312a8164dfSZhong Wang /*
2322a8164dfSZhong Wang  * Pass in options/arguments, rest of arguments
2332a8164dfSZhong Wang  */
2342a8164dfSZhong Wang /*ARGSUSED*/
2352a8164dfSZhong Wang static int
2362a8164dfSZhong Wang fcoeListPortsFunc(int objects, char *argv[], cmdOptions_t *options,
2372a8164dfSZhong Wang     void *addArgs)
2382a8164dfSZhong Wang {
2392a8164dfSZhong Wang 	return (fcoe_adm_list_ports(options));
2402a8164dfSZhong Wang }
2412a8164dfSZhong Wang 
2422a8164dfSZhong Wang /*
243fcf3ce44SJohn Forte  * input:
244fcf3ce44SJohn Forte  *  execFullName - exec name of program (argv[0])
245fcf3ce44SJohn Forte  *
246fcf3ce44SJohn Forte  * Returns:
247fcf3ce44SJohn Forte  *  command name portion of execFullName
248fcf3ce44SJohn Forte  */
249fcf3ce44SJohn Forte static char *
250fcf3ce44SJohn Forte getExecBasename(char *execFullname)
251fcf3ce44SJohn Forte {
252fcf3ce44SJohn Forte 	char *lastSlash, *execBasename;
253fcf3ce44SJohn Forte 
254fcf3ce44SJohn Forte 	/* guard against '/' at end of command invocation */
255fcf3ce44SJohn Forte 	for (;;) {
256fcf3ce44SJohn Forte 		lastSlash = strrchr(execFullname, '/');
257fcf3ce44SJohn Forte 		if (lastSlash == NULL) {
258fcf3ce44SJohn Forte 			execBasename = execFullname;
259fcf3ce44SJohn Forte 			break;
260fcf3ce44SJohn Forte 		} else {
261fcf3ce44SJohn Forte 			execBasename = lastSlash + 1;
262fcf3ce44SJohn Forte 			if (*execBasename == '\0') {
263fcf3ce44SJohn Forte 				*lastSlash = '\0';
264fcf3ce44SJohn Forte 				continue;
265fcf3ce44SJohn Forte 			}
266fcf3ce44SJohn Forte 			break;
267fcf3ce44SJohn Forte 		}
268fcf3ce44SJohn Forte 	}
269fcf3ce44SJohn Forte 	return (execBasename);
270fcf3ce44SJohn Forte }
271fcf3ce44SJohn Forte 
272fcf3ce44SJohn Forte /*
273fcf3ce44SJohn Forte  * main calls a parser that checks syntax of the input command against
274fcf3ce44SJohn Forte  * various rules tables.
275fcf3ce44SJohn Forte  *
276fcf3ce44SJohn Forte  * The parser provides usage feedback based upon same tables by calling
277fcf3ce44SJohn Forte  * two usage functions, usage and subUsage, handling command and subcommand
278fcf3ce44SJohn Forte  * usage respectively.
279fcf3ce44SJohn Forte  *
280fcf3ce44SJohn Forte  * The parser handles all printing of usage syntactical errors
281fcf3ce44SJohn Forte  *
282fcf3ce44SJohn Forte  * When syntax is successfully validated, the parser calls the associated
283fcf3ce44SJohn Forte  * function using the subcommands table functions.
284fcf3ce44SJohn Forte  *
285fcf3ce44SJohn Forte  * Syntax is as follows:
286fcf3ce44SJohn Forte  *	command subcommand [options] resource-type [<object>]
287fcf3ce44SJohn Forte  *
288fcf3ce44SJohn Forte  * The return value from the function is placed in funcRet
289fcf3ce44SJohn Forte  */
290fcf3ce44SJohn Forte int
291fcf3ce44SJohn Forte main(int argc, char *argv[])
292fcf3ce44SJohn Forte {
293fcf3ce44SJohn Forte 	synTables_t synTables;
294fcf3ce44SJohn Forte 	char versionString[VERSION_STRING_MAX_LEN];
295fcf3ce44SJohn Forte 	int ret;
296fcf3ce44SJohn Forte 	int funcRet;
297fcf3ce44SJohn Forte 	void *subcommandArgs = NULL;
298fcf3ce44SJohn Forte 
2992a8164dfSZhong Wang 	(void) setlocale(LC_ALL, "");
3002a8164dfSZhong Wang #if	!defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
3012a8164dfSZhong Wang #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
3022a8164dfSZhong Wang #endif
3032a8164dfSZhong Wang 	(void) textdomain(TEXT_DOMAIN);
3042a8164dfSZhong Wang 
305fcf3ce44SJohn Forte 	/* set global command name */
306fcf3ce44SJohn Forte 	cmdName = getExecBasename(argv[0]);
307fcf3ce44SJohn Forte 
308fcf3ce44SJohn Forte 	sprintf(versionString, "%s.%s",
309fcf3ce44SJohn Forte 	    VERSION_STRING_MAJOR, VERSION_STRING_MINOR);
310fcf3ce44SJohn Forte 	synTables.versionString = versionString;
311fcf3ce44SJohn Forte 	if (strcmp(cmdName, "fcadm") == 0) {
312fcf3ce44SJohn Forte 		synTables.longOptionTbl = &fcadmlongOptions[0];
313fcf3ce44SJohn Forte 		synTables.subCommandPropsTbl = &fcadmsubcommands[0];
314fcf3ce44SJohn Forte 	} else {
315fcf3ce44SJohn Forte 		synTables.longOptionTbl = &fcinfolongOptions[0];
316fcf3ce44SJohn Forte 		synTables.subCommandPropsTbl = &fcinfosubcommands[0];
317fcf3ce44SJohn Forte 	}
318fcf3ce44SJohn Forte 
319fcf3ce44SJohn Forte 	/* call the CLI parser */
320fcf3ce44SJohn Forte 	ret = cmdParse(argc, argv, synTables, subcommandArgs, &funcRet);
321fcf3ce44SJohn Forte 	if (ret == 1) {
322fcf3ce44SJohn Forte 		fprintf(stdout, "%s %s(1M)\n",
323fcf3ce44SJohn Forte 		    gettext("For more information, please see"), cmdName);
324fcf3ce44SJohn Forte 		return (1);
325fcf3ce44SJohn Forte 	} else if (ret == -1) {
326fcf3ce44SJohn Forte 		perror(cmdName);
327fcf3ce44SJohn Forte 		return (1);
328fcf3ce44SJohn Forte 	}
329fcf3ce44SJohn Forte 
330fcf3ce44SJohn Forte 	if (funcRet != 0) {
331fcf3ce44SJohn Forte 		return (1);
332fcf3ce44SJohn Forte 	}
333fcf3ce44SJohn Forte 	return (0);
334fcf3ce44SJohn Forte }
335