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