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