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