xref: /titanic_44/usr/src/cmd/fcinfo/fcinfo.c (revision a79493184c9332129c9c91500069322f6f3fafdd)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <fcinfo.h>
27 
28 
29 
30 #define	VERSION_STRING_MAX_LEN	10
31 /*
32  * Version number:
33  *  MAJOR - This should only change when there is an incompatible change made
34  *  to the interfaces or the output.
35  *
36  *  MINOR - This should change whenever there is a new command or new feature
37  *  with no incompatible change.
38  */
39 #define	VERSION_STRING_MAJOR	    "1"
40 #define	VERSION_STRING_MINOR	    "0"
41 
42 #define	OPTIONSTRING1		"HBA Port WWN"
43 #define	OPTIONSTRING2		"HBA Node WWN"
44 /* forward declarations */
45 static int listHbaPortFunc(int, char **, cmdOptions_t *, void *);
46 static int listRemotePortFunc(int, char **, cmdOptions_t *, void *);
47 static int listLogicalUnitFunc(int, char **, cmdOptions_t *, void *);
48 static int npivCreatePortFunc(int, char **, cmdOptions_t *, void *);
49 static int npivDeletePortFunc(int, char **, cmdOptions_t *, void *);
50 static int npivCreatePortListFunc(int, char **, cmdOptions_t *, void *);
51 static int npivListHbaPortFunc(int, char **, cmdOptions_t *, void *);
52 static int npivListRemotePortFunc(int, char **, cmdOptions_t *, void *);
53 static int fcoeAdmCreatePortFunc(int, char **, cmdOptions_t *, void *);
54 static int fcoeListPortsFunc(int, char **, cmdOptions_t *, void *);
55 static int fcoeAdmDeletePortFunc(int, char **, cmdOptions_t *, void *);
56 static int fcadmForceLipFunc(int, char **, cmdOptions_t *, void *);
57 static char *getExecBasename(char *);
58 
59 /*
60  * Add new options here
61  *
62  * Optional option-arguments are not allowed by CLIP
63  */
64 optionTbl_t fcinfolongOptions[] = {
65 	{"port", required_argument,	'p', OPTIONSTRING1},
66 	{"target", no_argument,		't', NULL},
67 	{"initiator", no_argument,	'i', NULL},
68 	{"linkstat", no_argument,	'l', NULL},
69 	{"scsi-target", no_argument,	's', NULL},
70 	{"fcoe", no_argument,		'e', NULL},
71 	{"verbose", no_argument,	'v', NULL},
72 	{NULL, 0, 0}
73 };
74 
75 optionTbl_t fcadmlongOptions[] = {
76 	{"port", required_argument,	'p', OPTIONSTRING1},
77 	{"node", required_argument,	'n', OPTIONSTRING2},
78 	{"linkstat", no_argument,	'l', NULL},
79 	{"scsi-target", no_argument,	's', NULL},
80 	{"fcoe-force-promisc", no_argument, 'f', NULL},
81 	{"target", no_argument,		't', NULL},
82 	{"initiator", no_argument,	'i', NULL},
83 	{NULL, 0, 0}
84 };
85 
86 /*
87  * Add new subcommands here
88  */
89 subCommandProps_t fcinfosubcommands[] = {
90 	{"hba-port", listHbaPortFunc, "itel", NULL, NULL,
91 		OPERAND_OPTIONAL_MULTIPLE, "WWN"},
92 	{"remote-port", listRemotePortFunc, "lsp", "p", NULL,
93 		OPERAND_OPTIONAL_MULTIPLE, "WWN"},
94 	{"logical-unit", listLogicalUnitFunc, "v", NULL, NULL,
95 		OPERAND_OPTIONAL_MULTIPLE, "OS Device Path"},
96 	{"lu", listLogicalUnitFunc, "v", NULL, NULL,
97 		OPERAND_OPTIONAL_MULTIPLE, "OS Device Path"},
98 	{NULL, 0, NULL, NULL, NULL, 0, NULL, NULL}
99 };
100 
101 subCommandProps_t fcadmsubcommands[] = {
102 	{"create-npiv-port",
103 	    npivCreatePortFunc, "pn", NULL, NULL,
104 	    OPERAND_MANDATORY_SINGLE,  "WWN"},
105 	{"delete-npiv-port",
106 	    npivDeletePortFunc, "p", "p", NULL,
107 	    OPERAND_MANDATORY_SINGLE,  "WWN"},
108 	{"hba-port",
109 	    npivListHbaPortFunc, "l", NULL, NULL,
110 	    OPERAND_OPTIONAL_MULTIPLE, "WWN"},
111 	{"remote-port",
112 	    npivListRemotePortFunc, "psl", "p", NULL,
113 	    OPERAND_OPTIONAL_MULTIPLE, "WWN"},
114 	{"create-port-list",
115 	    npivCreatePortListFunc, NULL, NULL, NULL,
116 	    OPERAND_NONE, NULL},
117 	{"create-fcoe-port",
118 	    fcoeAdmCreatePortFunc, "itpnf", NULL, NULL,
119 		OPERAND_MANDATORY_SINGLE, "Network Interface Name"},
120 	{"delete-fcoe-port",
121 	    fcoeAdmDeletePortFunc, NULL, NULL, NULL,
122 		OPERAND_MANDATORY_SINGLE, "Network Interface Name"},
123 	{"list-fcoe-ports",
124 	    fcoeListPortsFunc, "it", NULL, NULL,
125 		OPERAND_NONE, NULL},
126 	{"force-lip",
127 	    fcadmForceLipFunc, NULL, NULL, NULL,
128 		OPERAND_MANDATORY_SINGLE, "WWN"},
129 	{NULL, 0, NULL, NULL, NULL, 0, NULL, NULL}
130 };
131 
132 /*
133  * Pass in options/arguments, rest of arguments
134  */
135 /*ARGSUSED*/
136 static int
listHbaPortFunc(int objects,char * argv[],cmdOptions_t * options,void * addArgs)137 listHbaPortFunc(int objects, char *argv[], cmdOptions_t *options, void *addArgs)
138 {
139 	return (fc_util_list_hbaport(objects, argv, options));
140 }
141 
142 /*
143  * Pass in options/arguments, rest of arguments
144  */
145 /*ARGSUSED*/
146 static int
listRemotePortFunc(int objects,char * argv[],cmdOptions_t * options,void * addArgs)147 listRemotePortFunc(int objects, char *argv[], cmdOptions_t *options,
148     void *addArgs)
149 {
150 	return (fc_util_list_remoteport(objects, argv, options));
151 }
152 
153 /*
154  * Pass in options/arguments, rest of arguments
155  */
156 /*ARGSUSED*/
157 static int
listLogicalUnitFunc(int objects,char * argv[],cmdOptions_t * options,void * addArgs)158 listLogicalUnitFunc(int objects, char *argv[], cmdOptions_t *options,
159     void *addArgs)
160 {
161 	return (fc_util_list_logicalunit(objects, argv, options));
162 }
163 
164 /*
165  * Pass in options/arguments, rest of arguments
166  */
167 /*ARGSUSED*/
168 static int
npivCreatePortFunc(int objects,char * argv[],cmdOptions_t * options,void * addArgs)169 npivCreatePortFunc(int objects, char *argv[],
170     cmdOptions_t *options, void *addArgs) {
171 	return (fc_util_create_npivport(objects, argv, options));
172 }
173 
174 static int
npivCreatePortListFunc(int objects,char * argv[],cmdOptions_t * options,void * addArgs)175 npivCreatePortListFunc(int objects, char *argv[],
176     cmdOptions_t *options, void *addArgs) {
177 	if ((objects == 0) && addArgs && options && argv) {
178 		objects = 1;
179 	}
180 	return (fc_util_create_portlist());
181 }
182 
183 /*
184  * Pass in options/arguments, rest of arguments
185  */
186 /*ARGSUSED*/
187 static int
npivDeletePortFunc(int objects,char * argv[],cmdOptions_t * options,void * addArgs)188 npivDeletePortFunc(int objects, char *argv[],
189     cmdOptions_t *options, void *addArgs) {
190 	return (fc_util_delete_npivport(objects, argv, options));
191 }
192 
193 /*
194  * Pass in options/arguments, rest of arguments
195  */
196 /*ARGSUSED*/
197 static int
npivListHbaPortFunc(int objects,char * argv[],cmdOptions_t * options,void * addArgs)198 npivListHbaPortFunc(int objects, char *argv[],
199     cmdOptions_t *options, void *addArgs) {
200 	return (fc_util_list_hbaport(objects, argv, options));
201 }
202 
203 /*
204  * Pass in options/arguments, rest of arguments
205  */
206 /*ARGSUSED*/
207 static int
npivListRemotePortFunc(int objects,char * argv[],cmdOptions_t * options,void * addArgs)208 npivListRemotePortFunc(int objects, char *argv[],
209     cmdOptions_t *options, void *addArgs) {
210 	return (fc_util_list_remoteport(objects, argv, options));
211 }
212 
213 /*
214  * Pass in options/arguments, rest of arguments
215  */
216 /*ARGSUSED*/
217 static int
fcoeAdmCreatePortFunc(int objects,char * argv[],cmdOptions_t * options,void * addArgs)218 fcoeAdmCreatePortFunc(int objects, char *argv[], cmdOptions_t *options,
219     void *addArgs)
220 {
221 	return (fcoe_adm_create_port(objects, argv, options));
222 }
223 
224 /*
225  * Pass in options/arguments, rest of arguments
226  */
227 /*ARGSUSED*/
228 static int
fcoeAdmDeletePortFunc(int objects,char * argv[],cmdOptions_t * options,void * addArgs)229 fcoeAdmDeletePortFunc(int objects, char *argv[], cmdOptions_t *options,
230     void *addArgs)
231 {
232 	return (fcoe_adm_delete_port(objects, argv));
233 }
234 
235 /*
236  * Pass in options/arguments, rest of arguments
237  */
238 /*ARGSUSED*/
239 static int
fcoeListPortsFunc(int objects,char * argv[],cmdOptions_t * options,void * addArgs)240 fcoeListPortsFunc(int objects, char *argv[], cmdOptions_t *options,
241     void *addArgs)
242 {
243 	return (fcoe_adm_list_ports(options));
244 }
245 
246 /*
247  * Pass in options/arguments, rest of arguments
248  */
249 /*ARGSUSED*/
250 static int
fcadmForceLipFunc(int objects,char * argv[],cmdOptions_t * options,void * addArgs)251 fcadmForceLipFunc(int objects, char *argv[], cmdOptions_t *options,
252     void *addArgs)
253 {
254 	return (fc_util_force_lip(objects, argv));
255 }
256 
257 /*
258  * input:
259  *  execFullName - exec name of program (argv[0])
260  *
261  * Returns:
262  *  command name portion of execFullName
263  */
264 static char *
getExecBasename(char * execFullname)265 getExecBasename(char *execFullname)
266 {
267 	char *lastSlash, *execBasename;
268 
269 	/* guard against '/' at end of command invocation */
270 	for (;;) {
271 		lastSlash = strrchr(execFullname, '/');
272 		if (lastSlash == NULL) {
273 			execBasename = execFullname;
274 			break;
275 		} else {
276 			execBasename = lastSlash + 1;
277 			if (*execBasename == '\0') {
278 				*lastSlash = '\0';
279 				continue;
280 			}
281 			break;
282 		}
283 	}
284 	return (execBasename);
285 }
286 
287 /*
288  * main calls a parser that checks syntax of the input command against
289  * various rules tables.
290  *
291  * The parser provides usage feedback based upon same tables by calling
292  * two usage functions, usage and subUsage, handling command and subcommand
293  * usage respectively.
294  *
295  * The parser handles all printing of usage syntactical errors
296  *
297  * When syntax is successfully validated, the parser calls the associated
298  * function using the subcommands table functions.
299  *
300  * Syntax is as follows:
301  *	command subcommand [options] resource-type [<object>]
302  *
303  * The return value from the function is placed in funcRet
304  */
305 int
main(int argc,char * argv[])306 main(int argc, char *argv[])
307 {
308 	synTables_t synTables;
309 	char versionString[VERSION_STRING_MAX_LEN];
310 	int ret;
311 	int funcRet;
312 	void *subcommandArgs = NULL;
313 
314 	(void) setlocale(LC_ALL, "");
315 #if	!defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
316 #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
317 #endif
318 	(void) textdomain(TEXT_DOMAIN);
319 
320 	/* set global command name */
321 	cmdName = getExecBasename(argv[0]);
322 
323 	sprintf(versionString, "%s.%s",
324 	    VERSION_STRING_MAJOR, VERSION_STRING_MINOR);
325 	synTables.versionString = versionString;
326 	if (strcmp(cmdName, "fcadm") == 0) {
327 		synTables.longOptionTbl = &fcadmlongOptions[0];
328 		synTables.subCommandPropsTbl = &fcadmsubcommands[0];
329 	} else {
330 		synTables.longOptionTbl = &fcinfolongOptions[0];
331 		synTables.subCommandPropsTbl = &fcinfosubcommands[0];
332 	}
333 
334 	/* call the CLI parser */
335 	ret = cmdParse(argc, argv, synTables, subcommandArgs, &funcRet);
336 	if (ret == 1) {
337 		fprintf(stdout, "%s %s(1M)\n",
338 		    gettext("For more information, please see"), cmdName);
339 		return (1);
340 	} else if (ret == -1) {
341 		perror(cmdName);
342 		return (1);
343 	}
344 
345 	if (funcRet != 0) {
346 		return (1);
347 	}
348 	return (0);
349 }
350