xref: /titanic_53/usr/src/cmd/fcinfo/fcinfo.c (revision 2a8164df8a5f42c8a00f10c67d7bc84f80ae9c41)
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 /*
22*2a8164dfSZhong 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 *);
53*2a8164dfSZhong Wang static int fcoeAdmCreatePortFunc(int, char **, cmdOptions_t *, void *);
54*2a8164dfSZhong Wang static int fcoeListPortsFunc(int, char **, cmdOptions_t *, void *);
55*2a8164dfSZhong Wang static int fcoeAdmDeletePortFunc(int, char **, cmdOptions_t *, void *);
56*2a8164dfSZhong Wang static int fcoeAdmCreatePortListFunc(int, char **, cmdOptions_t *, void *);
57fcf3ce44SJohn Forte static char *getExecBasename(char *);
58fcf3ce44SJohn Forte 
59fcf3ce44SJohn Forte /*
60fcf3ce44SJohn Forte  * Add new options here
61fcf3ce44SJohn Forte  *
62fcf3ce44SJohn Forte  * Optional option-arguments are not allowed by CLIP
63fcf3ce44SJohn Forte  */
64fcf3ce44SJohn Forte optionTbl_t fcinfolongOptions[] = {
65fcf3ce44SJohn Forte 	{"port", required_argument,	'p', OPTIONSTRING1},
66fcf3ce44SJohn Forte 	{"target", no_argument,		't', NULL},
67fcf3ce44SJohn Forte 	{"initiator", no_argument,	'i', NULL},
68fcf3ce44SJohn Forte 	{"linkstat", no_argument,	'l', NULL},
69fcf3ce44SJohn Forte 	{"scsi-target", no_argument,	's', NULL},
70*2a8164dfSZhong Wang 	{"fcoe", no_argument,		'e', NULL},
71fcf3ce44SJohn Forte 	{"verbose", no_argument,	'v', NULL},
72fcf3ce44SJohn Forte 	{NULL, 0, 0}
73fcf3ce44SJohn Forte };
74fcf3ce44SJohn Forte 
75fcf3ce44SJohn Forte optionTbl_t fcadmlongOptions[] = {
76fcf3ce44SJohn Forte 	{"port", required_argument,	'p', OPTIONSTRING1},
77fcf3ce44SJohn Forte 	{"node", required_argument,	'n', OPTIONSTRING2},
78fcf3ce44SJohn Forte 	{"linkstat", no_argument,	'l', NULL},
79fcf3ce44SJohn Forte 	{"scsi-target", no_argument,	's', NULL},
80*2a8164dfSZhong Wang 	{"fcoe-force-promisc", no_argument, 'f', NULL},
81*2a8164dfSZhong Wang 	{"target", no_argument,		't', 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[] = {
89*2a8164dfSZhong 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},
116*2a8164dfSZhong Wang 	{"create-fcoe-port",
117*2a8164dfSZhong Wang 	    fcoeAdmCreatePortFunc, "tpnf", "t", NULL,
118*2a8164dfSZhong Wang 		OPERAND_MANDATORY_SINGLE, "Network Interface Name"},
119*2a8164dfSZhong Wang 	{"delete-fcoe-port",
120*2a8164dfSZhong Wang 	    fcoeAdmDeletePortFunc, NULL, NULL, NULL,
121*2a8164dfSZhong Wang 		OPERAND_MANDATORY_SINGLE, "Network Interface Name"},
122*2a8164dfSZhong Wang 	{"list-fcoe-ports",
123*2a8164dfSZhong Wang 	    fcoeListPortsFunc, "t", NULL, NULL,
124*2a8164dfSZhong Wang 		OPERAND_NONE, NULL},
125*2a8164dfSZhong Wang 	{"create-fcoe-ports",
126*2a8164dfSZhong Wang 	    fcoeAdmCreatePortListFunc, "t", NULL, NULL,
127*2a8164dfSZhong Wang 		OPERAND_NONE, NULL},
128fcf3ce44SJohn Forte 	{NULL, 0, NULL, NULL, NULL, 0, NULL, NULL}
129fcf3ce44SJohn Forte };
130*2a8164dfSZhong Wang 
131fcf3ce44SJohn Forte /*
132fcf3ce44SJohn Forte  * Pass in options/arguments, rest of arguments
133fcf3ce44SJohn Forte  */
134fcf3ce44SJohn Forte /*ARGSUSED*/
135fcf3ce44SJohn Forte static int
136fcf3ce44SJohn Forte listHbaPortFunc(int objects, char *argv[], cmdOptions_t *options, void *addArgs)
137fcf3ce44SJohn Forte {
138fcf3ce44SJohn Forte 	return (fc_util_list_hbaport(objects, argv, options));
139fcf3ce44SJohn Forte }
140fcf3ce44SJohn Forte 
141fcf3ce44SJohn Forte /*
142fcf3ce44SJohn Forte  * Pass in options/arguments, rest of arguments
143fcf3ce44SJohn Forte  */
144fcf3ce44SJohn Forte /*ARGSUSED*/
145fcf3ce44SJohn Forte static int
146fcf3ce44SJohn Forte listRemotePortFunc(int objects, char *argv[], cmdOptions_t *options,
147fcf3ce44SJohn Forte     void *addArgs)
148fcf3ce44SJohn Forte {
149fcf3ce44SJohn Forte 	return (fc_util_list_remoteport(objects, argv, options));
150fcf3ce44SJohn Forte }
151fcf3ce44SJohn Forte 
152fcf3ce44SJohn Forte /*
153fcf3ce44SJohn Forte  * Pass in options/arguments, rest of arguments
154fcf3ce44SJohn Forte  */
155fcf3ce44SJohn Forte /*ARGSUSED*/
156fcf3ce44SJohn Forte static int
157fcf3ce44SJohn Forte listLogicalUnitFunc(int objects, char *argv[], cmdOptions_t *options,
158fcf3ce44SJohn Forte     void *addArgs)
159fcf3ce44SJohn Forte {
160fcf3ce44SJohn Forte 	return (fc_util_list_logicalunit(objects, argv, options));
161fcf3ce44SJohn Forte }
162fcf3ce44SJohn Forte 
163fcf3ce44SJohn Forte /*
164fcf3ce44SJohn Forte  * Pass in options/arguments, rest of arguments
165fcf3ce44SJohn Forte  */
166fcf3ce44SJohn Forte /*ARGSUSED*/
167fcf3ce44SJohn Forte static int
168fcf3ce44SJohn Forte npivCreatePortFunc(int objects, char *argv[],
169fcf3ce44SJohn Forte     cmdOptions_t *options, void *addArgs) {
170fcf3ce44SJohn Forte 	return (fc_util_create_npivport(objects, argv, options));
171fcf3ce44SJohn Forte }
172fcf3ce44SJohn Forte 
173fcf3ce44SJohn Forte static int
174fcf3ce44SJohn Forte npivCreatePortListFunc(int objects, char *argv[],
175fcf3ce44SJohn Forte     cmdOptions_t *options, void *addArgs) {
176fcf3ce44SJohn Forte 	if ((objects == 0) && addArgs && options && argv) {
177fcf3ce44SJohn Forte 		objects = 1;
178fcf3ce44SJohn Forte 	}
179fcf3ce44SJohn Forte 	return (fc_util_create_portlist());
180fcf3ce44SJohn Forte }
181fcf3ce44SJohn Forte 
182fcf3ce44SJohn Forte /*
183fcf3ce44SJohn Forte  * Pass in options/arguments, rest of arguments
184fcf3ce44SJohn Forte  */
185fcf3ce44SJohn Forte /*ARGSUSED*/
186fcf3ce44SJohn Forte static int
187fcf3ce44SJohn Forte npivDeletePortFunc(int objects, char *argv[],
188fcf3ce44SJohn Forte     cmdOptions_t *options, void *addArgs) {
189fcf3ce44SJohn Forte 	return (fc_util_delete_npivport(objects, argv, options));
190fcf3ce44SJohn Forte }
191fcf3ce44SJohn Forte 
192fcf3ce44SJohn Forte /*
193fcf3ce44SJohn Forte  * Pass in options/arguments, rest of arguments
194fcf3ce44SJohn Forte  */
195fcf3ce44SJohn Forte /*ARGSUSED*/
196fcf3ce44SJohn Forte static int
197fcf3ce44SJohn Forte npivListHbaPortFunc(int objects, char *argv[],
198fcf3ce44SJohn Forte     cmdOptions_t *options, void *addArgs) {
199fcf3ce44SJohn Forte 	return (fc_util_list_hbaport(objects, argv, options));
200fcf3ce44SJohn Forte }
201fcf3ce44SJohn Forte 
202fcf3ce44SJohn Forte /*
203fcf3ce44SJohn Forte  * Pass in options/arguments, rest of arguments
204fcf3ce44SJohn Forte  */
205fcf3ce44SJohn Forte /*ARGSUSED*/
206fcf3ce44SJohn Forte static int
207fcf3ce44SJohn Forte npivListRemotePortFunc(int objects, char *argv[],
208fcf3ce44SJohn Forte     cmdOptions_t *options, void *addArgs) {
209fcf3ce44SJohn Forte 	return (fc_util_list_remoteport(objects, argv, options));
210fcf3ce44SJohn Forte }
211fcf3ce44SJohn Forte 
212fcf3ce44SJohn Forte /*
213*2a8164dfSZhong Wang  * Pass in options/arguments, rest of arguments
214*2a8164dfSZhong Wang  */
215*2a8164dfSZhong Wang /*ARGSUSED*/
216*2a8164dfSZhong Wang static int
217*2a8164dfSZhong Wang fcoeAdmCreatePortFunc(int objects, char *argv[], cmdOptions_t *options,
218*2a8164dfSZhong Wang     void *addArgs)
219*2a8164dfSZhong Wang {
220*2a8164dfSZhong Wang 	return (fcoe_adm_create_port(objects, argv, options));
221*2a8164dfSZhong Wang }
222*2a8164dfSZhong Wang 
223*2a8164dfSZhong Wang /*
224*2a8164dfSZhong Wang  * Pass in options/arguments, rest of arguments
225*2a8164dfSZhong Wang  */
226*2a8164dfSZhong Wang /*ARGSUSED*/
227*2a8164dfSZhong Wang static int
228*2a8164dfSZhong Wang fcoeAdmDeletePortFunc(int objects, char *argv[], cmdOptions_t *options,
229*2a8164dfSZhong Wang     void *addArgs)
230*2a8164dfSZhong Wang {
231*2a8164dfSZhong Wang 	return (fcoe_adm_delete_port(objects, argv));
232*2a8164dfSZhong Wang }
233*2a8164dfSZhong Wang 
234*2a8164dfSZhong Wang /*
235*2a8164dfSZhong Wang  * Pass in options/arguments, rest of arguments
236*2a8164dfSZhong Wang  */
237*2a8164dfSZhong Wang /*ARGSUSED*/
238*2a8164dfSZhong Wang static int
239*2a8164dfSZhong Wang fcoeListPortsFunc(int objects, char *argv[], cmdOptions_t *options,
240*2a8164dfSZhong Wang     void *addArgs)
241*2a8164dfSZhong Wang {
242*2a8164dfSZhong Wang 	return (fcoe_adm_list_ports(options));
243*2a8164dfSZhong Wang }
244*2a8164dfSZhong Wang 
245*2a8164dfSZhong Wang /*
246*2a8164dfSZhong Wang  * Pass in options/arguments, rest of arguments
247*2a8164dfSZhong Wang  */
248*2a8164dfSZhong Wang /*ARGSUSED*/
249*2a8164dfSZhong Wang static int
250*2a8164dfSZhong Wang fcoeAdmCreatePortListFunc(int objects, char *argv[], cmdOptions_t *options,
251*2a8164dfSZhong Wang     void *addArgs)
252*2a8164dfSZhong Wang {
253*2a8164dfSZhong Wang 	return (fcoe_adm_create_portlist(options));
254*2a8164dfSZhong Wang }
255*2a8164dfSZhong Wang 
256*2a8164dfSZhong Wang /*
257fcf3ce44SJohn Forte  * input:
258fcf3ce44SJohn Forte  *  execFullName - exec name of program (argv[0])
259fcf3ce44SJohn Forte  *
260fcf3ce44SJohn Forte  * Returns:
261fcf3ce44SJohn Forte  *  command name portion of execFullName
262fcf3ce44SJohn Forte  */
263fcf3ce44SJohn Forte static char *
264fcf3ce44SJohn Forte getExecBasename(char *execFullname)
265fcf3ce44SJohn Forte {
266fcf3ce44SJohn Forte 	char *lastSlash, *execBasename;
267fcf3ce44SJohn Forte 
268fcf3ce44SJohn Forte 	/* guard against '/' at end of command invocation */
269fcf3ce44SJohn Forte 	for (;;) {
270fcf3ce44SJohn Forte 		lastSlash = strrchr(execFullname, '/');
271fcf3ce44SJohn Forte 		if (lastSlash == NULL) {
272fcf3ce44SJohn Forte 			execBasename = execFullname;
273fcf3ce44SJohn Forte 			break;
274fcf3ce44SJohn Forte 		} else {
275fcf3ce44SJohn Forte 			execBasename = lastSlash + 1;
276fcf3ce44SJohn Forte 			if (*execBasename == '\0') {
277fcf3ce44SJohn Forte 				*lastSlash = '\0';
278fcf3ce44SJohn Forte 				continue;
279fcf3ce44SJohn Forte 			}
280fcf3ce44SJohn Forte 			break;
281fcf3ce44SJohn Forte 		}
282fcf3ce44SJohn Forte 	}
283fcf3ce44SJohn Forte 	return (execBasename);
284fcf3ce44SJohn Forte }
285fcf3ce44SJohn Forte 
286fcf3ce44SJohn Forte /*
287fcf3ce44SJohn Forte  * main calls a parser that checks syntax of the input command against
288fcf3ce44SJohn Forte  * various rules tables.
289fcf3ce44SJohn Forte  *
290fcf3ce44SJohn Forte  * The parser provides usage feedback based upon same tables by calling
291fcf3ce44SJohn Forte  * two usage functions, usage and subUsage, handling command and subcommand
292fcf3ce44SJohn Forte  * usage respectively.
293fcf3ce44SJohn Forte  *
294fcf3ce44SJohn Forte  * The parser handles all printing of usage syntactical errors
295fcf3ce44SJohn Forte  *
296fcf3ce44SJohn Forte  * When syntax is successfully validated, the parser calls the associated
297fcf3ce44SJohn Forte  * function using the subcommands table functions.
298fcf3ce44SJohn Forte  *
299fcf3ce44SJohn Forte  * Syntax is as follows:
300fcf3ce44SJohn Forte  *	command subcommand [options] resource-type [<object>]
301fcf3ce44SJohn Forte  *
302fcf3ce44SJohn Forte  * The return value from the function is placed in funcRet
303fcf3ce44SJohn Forte  */
304fcf3ce44SJohn Forte int
305fcf3ce44SJohn Forte main(int argc, char *argv[])
306fcf3ce44SJohn Forte {
307fcf3ce44SJohn Forte 	synTables_t synTables;
308fcf3ce44SJohn Forte 	char versionString[VERSION_STRING_MAX_LEN];
309fcf3ce44SJohn Forte 	int ret;
310fcf3ce44SJohn Forte 	int funcRet;
311fcf3ce44SJohn Forte 	void *subcommandArgs = NULL;
312fcf3ce44SJohn Forte 
313*2a8164dfSZhong Wang 	(void) setlocale(LC_ALL, "");
314*2a8164dfSZhong Wang #if	!defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
315*2a8164dfSZhong Wang #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
316*2a8164dfSZhong Wang #endif
317*2a8164dfSZhong Wang 	(void) textdomain(TEXT_DOMAIN);
318*2a8164dfSZhong Wang 
319fcf3ce44SJohn Forte 	/* set global command name */
320fcf3ce44SJohn Forte 	cmdName = getExecBasename(argv[0]);
321fcf3ce44SJohn Forte 
322fcf3ce44SJohn Forte 	sprintf(versionString, "%s.%s",
323fcf3ce44SJohn Forte 	    VERSION_STRING_MAJOR, VERSION_STRING_MINOR);
324fcf3ce44SJohn Forte 	synTables.versionString = versionString;
325fcf3ce44SJohn Forte 	if (strcmp(cmdName, "fcadm") == 0) {
326fcf3ce44SJohn Forte 		synTables.longOptionTbl = &fcadmlongOptions[0];
327fcf3ce44SJohn Forte 		synTables.subCommandPropsTbl = &fcadmsubcommands[0];
328fcf3ce44SJohn Forte 	} else {
329fcf3ce44SJohn Forte 		synTables.longOptionTbl = &fcinfolongOptions[0];
330fcf3ce44SJohn Forte 		synTables.subCommandPropsTbl = &fcinfosubcommands[0];
331fcf3ce44SJohn Forte 	}
332fcf3ce44SJohn Forte 
333fcf3ce44SJohn Forte 	/* call the CLI parser */
334fcf3ce44SJohn Forte 	ret = cmdParse(argc, argv, synTables, subcommandArgs, &funcRet);
335fcf3ce44SJohn Forte 	if (ret == 1) {
336fcf3ce44SJohn Forte 		fprintf(stdout, "%s %s(1M)\n",
337fcf3ce44SJohn Forte 		    gettext("For more information, please see"), cmdName);
338fcf3ce44SJohn Forte 		return (1);
339fcf3ce44SJohn Forte 	} else if (ret == -1) {
340fcf3ce44SJohn Forte 		perror(cmdName);
341fcf3ce44SJohn Forte 		return (1);
342fcf3ce44SJohn Forte 	}
343fcf3ce44SJohn Forte 
344fcf3ce44SJohn Forte 	if (funcRet != 0) {
345fcf3ce44SJohn Forte 		return (1);
346fcf3ce44SJohn Forte 	}
347fcf3ce44SJohn Forte 	return (0);
348fcf3ce44SJohn Forte }
349