[ofa-general] [PATCH] iblinkinfo: convert to ibdiag_process_opts

Ira Weiny weiny2 at llnl.gov
Wed Jun 17 10:10:24 PDT 2009


From: Ira Weiny <weiny2 at llnl.gov>
Date: Thu, 14 May 2009 17:23:22 -0700
Subject: [PATCH] iblinkinfo: convert to ibdiag_process_opts


Signed-off-by: Ira Weiny <weiny2 at llnl.gov>
---
 infiniband-diags/src/iblinkinfo.c |  190 +++++++++++++-----------------------
 1 files changed, 69 insertions(+), 121 deletions(-)

diff --git a/infiniband-diags/src/iblinkinfo.c b/infiniband-diags/src/iblinkinfo.c
index d422a2a..ae6da27 100644
--- a/infiniband-diags/src/iblinkinfo.c
+++ b/infiniband-diags/src/iblinkinfo.c
@@ -51,12 +51,16 @@
 #include <complib/cl_nodenamemap.h>
 #include <infiniband/ibnetdisc.h>
 
-char *argv0 = "iblinkinfotest";
+#include "ibdiag_common.h"
 
 static char *node_name_map_file = NULL;
 static nn_map_t *node_name_map = NULL;
 
-static int timeout_ms = 500;
+static uint64_t guid = 0;
+static char *guid_str = NULL;
+static char *dr_path = NULL;
+static char *from = NULL;
+static int hops = 0;
 
 static int down_links_only = 0;
 static int line_mode = 0;
@@ -223,140 +227,84 @@ print_switch(ibnd_node_t *node, void *user_data)
 	free(remap);
 }
 
-void
-usage(void)
+static int process_opt(void *context, int ch, char *optarg)
 {
-	fprintf(stderr,
-		"Usage: %s [-hclp -S <guid> -D <direct route> -C <ca_name> -P <ca_port>]\n"
-		"   Report link speed and connection for each port of each switch which is active\n"
-		"   -h This help message\n"
-		"   -S <guid> output only the node specified by guid\n"
-		"   -D <direct route> print only node specified by <direct route>\n"
-		"   -f <dr_path> specify node to start \"from\"\n"
-		"   -n <hops> Number of hops to include away from specified node\n"
-		"   -d print only down links\n"
-		"   -l (line mode) print all information for each link on each line\n"
-		"   -p print additional switch settings (PktLifeTime,HoqLife,VLStallCount)\n"
-
-
-		"   -t <timeout_ms> timeout for any single fabric query\n"
-		"   -s show progress during scan\n"
-		"   --node-name-map <map_file> use specified node name map\n"
-
-		"   -C <ca_name> use selected Channel Adaptor name for queries\n"
-		"   -P <ca_port> use selected channel adaptor port for queries\n"
-		"   -g print port guids instead of node guids\n"
-		"   --debug print debug messages\n"
-		"   -R (this option is obsolete and does nothing)\n"
-		,
-			argv0);
-	exit(-1);
+	switch (ch) {
+	case 1:
+		node_name_map_file = strdup(optarg);
+		break;
+	case 'S':
+		guid_str = optarg;
+		guid = (uint64_t)strtoull(guid_str, 0, 0);
+		break;
+	case 'D':
+		dr_path = strdup(optarg);
+		break;
+	case 'f':
+		from = strdup(optarg);
+		break;
+	case 'n':
+		hops = (int)strtol(optarg, NULL, 0);
+		break;
+	case 'd':
+		down_links_only = 1;
+		break;
+	case 'l':
+		line_mode = 1;
+		break;
+	case 'p':
+		add_sw_settings = 1;
+		break;
+	case 'g':
+		print_port_guids = 1;
+		break;
+	case 'R': /* nop */
+		break;
+	default:
+		return -1;
+	}
+
+	return 0;
 }
 
+
 int
 main(int argc, char **argv)
 {
+
 	int rc = 0;
-	char *ca = 0;
-	int ca_port = 0;
 	ibnd_fabric_t *fabric = NULL;
-	uint64_t guid = 0;
-	char *guid_str = NULL;
-	char *dr_path = NULL;
-	char *from = NULL;
-	int hops = 0;
-	ib_portid_t port_id = {0};
-
 	struct ibmad_port *ibmad_port;
+	ib_portid_t port_id = {0};
 	int mgmt_classes[3] = {IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS};
 
-	static char const str_opts[] = "S:D:n:C:P:t:sldgphuf:R";
-	static const struct option long_opts[] = {
-		{ "S", 1, 0, 'S'},
-		{ "D", 1, 0, 'D'},
-		{ "num-hops", 1, 0, 'n'},
-		{ "down-links-only", 0, 0, 'd'},
-		{ "line-mode", 0, 0, 'l'},
-		{ "ca-name", 1, 0, 'C'},
-		{ "ca-port", 1, 0, 'P'},
-		{ "timeout", 1, 0, 't'},
-		{ "show", 0, 0, 's'},
-		{ "print-port-guids", 0, 0, 'g'},
-		{ "print-additional", 0, 0, 'p'},
-		{ "help", 0, 0, 'h'},
-		{ "usage", 0, 0, 'u'},
-		{ "node-name-map", 1, 0, 1},
-		{ "debug", 0, 0, 2},
-		{ "compat", 0, 0, 3},
-		{ "from", 1, 0, 'f'},
-		{ "R", 0, 0, 'R'},
+	const struct ibdiag_opt opts[] = {
+		{ "node-name-map", 1, 1, "<file>", "node name map file" },
+		{ "switch", 'S', 1, "<switch_guid>", "query only <switch_guid> (hex format)"},
+		{ "Direct", 'D', 1, "<dr_path>", "query only node specified by <dr_path>"},
+		{ "from", 'f', 1, "<from>", "specify node to start \"<from>\""},
+		{ "hops", 'n', 1, "<hops>", "Number of hops to include away from specified node"},
+		{ "down", 'd', 0, NULL, "print only down links"},
+		{ "line", 'l', 0, NULL, "(line mode) print all information for each link on a single line"},
+		{ "additional", 'p', 0, NULL, "print additional switch settings (PktLifeTime, HoqLife, VLStallCount)"},
+		{ "portguids", 'g', 0, NULL, "print port guids instead of node guids"},
+		{ "GNDN", 'R', 0, NULL, "(This option is obsolete and does nothing)"},
 		{ 0 }
 	};
+	char usage_args[] = "";
+
+	ibdiag_process_opts(argc, argv, "sDLG", "SDfndlpgR", opts, process_opt,
+			    usage_args, NULL);
 
-	argv0 = argv[0];
-
-	while (1) {
-		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
-		if ( ch == -1 )
-			break;
-		switch(ch) {
-		case 1:
-			node_name_map_file = strdup(optarg);
-			break;
-		case 2:
-			ibnd_debug(1);
-			break;
-		case 'f':
-			from = strdup(optarg);
-			break;
-		case 'C':
-			ca = strdup(optarg);
-			break;
-		case 'P':
-			ca_port = strtoul(optarg, 0, 0);
-			break;
-		case 'D':
-			dr_path = strdup(optarg);
-			break;
-		case 'n':
-			hops = (int)strtol(optarg, NULL, 0);
-			break;
-		case 'd':
-			down_links_only = 1;
-			break;
-		case 'l':
-			line_mode = 1;
-			break;
-		case 't':
-			timeout_ms = strtoul(optarg, 0, 0);
-			break;
-		case 's':
-			ibnd_show_progress(1);
-			break;
-		case 'g':
-			print_port_guids = 1;
-			break;
-		case 'S':
-			guid_str = optarg;
-			guid = (uint64_t)strtoull(guid_str, 0, 0);
-			break;
-		case 'p':
-			add_sw_settings = 1;
-			break;
-		case 'R':
-			/* GNDN */
-			break;
-		default:
-			usage();
-			break;
-		}
-	}
 	argc -= optind;
 	argv += optind;
 
-	ibmad_port = mad_rpc_open_port(ca, ca_port, mgmt_classes, 3);
+	if (ibverbose)
+		ibnd_debug(1);
+
+	ibmad_port = mad_rpc_open_port(ibd_ca, ibd_ca_port, mgmt_classes, 3);
 	if (!ibmad_port) {
-		fprintf(stderr, "Failed to open %s port %d", ca, ca_port);
+		fprintf(stderr, "Failed to open %s port %d", ibd_ca, ibd_ca_port);
 		exit(1);
 	}
 
@@ -365,7 +313,7 @@ main(int argc, char **argv)
 	if (from) {
 		/* only scan part of the fabric */
 		str2drpath(&(port_id.drpath), from, 0, 0);
-		if ((fabric = ibnd_discover_fabric(ibmad_port, timeout_ms, &port_id, hops)) == NULL) {
+		if ((fabric = ibnd_discover_fabric(ibmad_port, ibd_timeout, &port_id, hops)) == NULL) {
 			fprintf(stderr, "discover failed\n");
 			rc = 1;
 			goto close_port;
@@ -375,7 +323,7 @@ main(int argc, char **argv)
 		if (ib_resolve_portid_str_via(&port_id, guid_str, IB_DEST_GUID,
 				NULL, ibmad_port) >= 0) {
 			if ((fabric = ibnd_discover_fabric(ibmad_port,
-					timeout_ms, &port_id, 1)) == NULL)
+					ibd_timeout, &port_id, 1)) == NULL)
 				IBWARN("Single node discover failed; attempting full scan\n");
 		} else
 			IBWARN("Failed to resolve %s; attempting full scan\n",
@@ -383,7 +331,7 @@ main(int argc, char **argv)
 	}
 
 	if (!fabric) /* do a full scan */
-		if ((fabric = ibnd_discover_fabric(ibmad_port, timeout_ms, NULL, -1)) == NULL) {
+		if ((fabric = ibnd_discover_fabric(ibmad_port, ibd_timeout, NULL, -1)) == NULL) {
 			fprintf(stderr, "discover failed\n");
 			rc = 1;
 			goto close_port;
-- 
1.5.4.5




More information about the general mailing list