[ofa-general] [PATCH] infiniband-diags: using common command line option processing

Sasha Khapyorsky sashak at voltaire.com
Sun Jan 25 03:52:49 PST 2009


This converts infiniband-diags tools to use the common command line
processing framework - it unifies the usages, options and removes a lot
of duplications. The tools functionality is preserved (should not be a
problems with backward compatibility), however the usage message is
changed for many tools and now looks like:

Usage: ibaddr [options] [<lid|dr_path|guid>]

Options:
  --gid_show, -g          show gid address only
  --lid_show, -l          show lid range only
  --Lid_show, -L          show lid range (in decimal) only
  --Ca, -C <ca>           Ca name to use
  --Port, -P <port>       Ca port number to use
  --Direct, -D            use Direct address argument
  --Guid, -G              use GUID address argument
  --timeout, -t <ms>      timeout in ms
  --sm_port, -s <lid>     SM port lid
  --errors, -e            show send and receive errors
  --verbose, -v           increase verbosity level
  --debug, -d             raise debug level
  --usage, -u             usage message
  --help, -h              help message
  --version, -V           show version

Examples:
  ibaddr 		# local port's address
  ibaddr 32		# show lid range and gid of lid 32
  ibaddr -G 0x8f1040023	# same but using guid address
  ibaddr -l 32		# show lid range only
  ibaddr -L 32		# show decimal lid range only
  ibaddr -g 32		# show gid address only

Signed-off-by: Sasha Khapyorsky <sashak at voltaire.com>
---
 infiniband-diags/src/ibaddr.c        |  133 ++++---------
 infiniband-diags/src/ibnetdiscover.c |  151 +++++---------
 infiniband-diags/src/ibping.c        |  147 ++++----------
 infiniband-diags/src/ibportstate.c   |  117 ++---------
 infiniband-diags/src/ibroute.c       |  159 +++++----------
 infiniband-diags/src/ibsendtrap.c    |   66 ++-----
 infiniband-diags/src/ibstat.c        |   78 +++-----
 infiniband-diags/src/ibsysstat.c     |  124 +++---------
 infiniband-diags/src/ibtracert.c     |  173 +++++-----------
 infiniband-diags/src/perfquery.c     |  178 ++++++-----------
 infiniband-diags/src/saquery.c       |  378 +++++++++++++++-------------------
 infiniband-diags/src/sminfo.c        |  123 ++++--------
 infiniband-diags/src/smpdump.c       |  109 ++++------
 infiniband-diags/src/smpquery.c      |  152 ++++----------
 infiniband-diags/src/vendstat.c      |  118 +++--------
 15 files changed, 706 insertions(+), 1500 deletions(-)

diff --git a/infiniband-diags/src/ibaddr.c b/infiniband-diags/src/ibaddr.c
index 688972b..4890da3 100644
--- a/infiniband-diags/src/ibaddr.c
+++ b/infiniband-diags/src/ibaddr.c
@@ -86,108 +86,53 @@ ib_resolve_addr(ib_portid_t *portid, int portnum, int show_lid, int show_gid)
 	return 0;
 }
 
-static void
-usage(void)
+static int show_lid, show_gid;
+
+static int process_opt(void *context, int ch, char *optarg)
 {
-	char *basename;
-
-	if (!(basename = strrchr(argv0, '/')))
-		basename = argv0;
-	else
-		basename++;
-
-	fprintf(stderr, "Usage: %s [-d(ebug) -D(irect) -G(uid) -l(id_show) -g(id_show) -s(m_port) sm_lid -C ca_name -P ca_port "
-			"-t(imeout) timeout_ms -V(ersion) -h(elp)] [<lid|dr_path|guid>]\n",
-			basename);
-	fprintf(stderr, "\tExamples:\n");
-	fprintf(stderr, "\t\t%s\t\t\t# local port's address\n", basename);
-	fprintf(stderr, "\t\t%s 32\t\t# show lid range and gid of lid 32\n", basename);
-	fprintf(stderr, "\t\t%s -G 0x8f1040023\t# same but using guid address\n", basename);
-	fprintf(stderr, "\t\t%s -l 32\t\t# show lid range only\n", basename);
-	fprintf(stderr, "\t\t%s -L 32\t\t# show decimal lid range only\n", basename);
-	fprintf(stderr, "\t\t%s -g 32\t\t# show gid address only\n", basename);
-	exit(-1);
+	switch (ch) {
+	case 'g':
+		show_gid = 1;
+		break;
+	case 'l':
+		show_lid++;
+		break;
+	case 'L':
+		show_lid = -100;
+		break;
+	default:
+		return -1;
+	}
+	return 0;
 }
 
-int
-main(int argc, char **argv)
+int main(int argc, char **argv)
 {
 	int mgmt_classes[3] = {IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS};
-	ib_portid_t *sm_id = 0, sm_portid = {0};
 	ib_portid_t portid = {0};
-	int dest_type = IB_DEST_LID;
-	int timeout = 0;	/* use default */
-	int show_lid = 0, show_gid = 0;
 	int port = 0;
-	char *ca = 0;
-	int ca_port = 0;
-
-	static char const str_opts[] = "C:P:t:s:dDGglLVhu";
-	static const struct option long_opts[] = {
-		{ "C", 1, 0, 'C'},
-		{ "P", 1, 0, 'P'},
-		{ "debug", 0, 0, 'd'},
-		{ "Direct", 0, 0, 'D'},
-		{ "Guid", 0, 0, 'G'},
-		{ "gid_show", 0, 0, 'g'},
-		{ "lid_show", 0, 0, 'l'},
-		{ "Lid_show", 0, 0, 'L'},
-		{ "timeout", 1, 0, 't'},
-		{ "sm_port", 1, 0, 's'},
-		{ "Version", 0, 0, 'V'},
-		{ "help", 0, 0, 'h'},
-		{ "usage", 0, 0, 'u'},
-		{ }
+
+	const struct ibdiag_opt opts[] = {
+		{ "gid_show", 'g', 0, NULL, "show gid address only"},
+		{ "lid_show", 'l', 0, NULL, "show lid range only"},
+		{ "Lid_show", 'L', 0, NULL, "show lid range (in decimal) only"},
+		{}
+	};
+	char usage_args[] = "[<lid|dr_path|guid>]";
+	const char *usage_examples[] = {
+		"\t\t# local port's address",
+		"32\t\t# show lid range and gid of lid 32",
+		"-G 0x8f1040023\t# same but using guid address",
+		"-l 32\t\t# show lid range only",
+		"-L 32\t\t# show decimal lid range only",
+		"-g 32\t\t# show gid address only",
+		NULL
 	};
 
-	argv0 = argv[0];
+	ibdiag_process_opts(argc, argv, NULL, "L", opts, process_opt,
+			    usage_args, usage_examples);
 
-	while (1) {
-		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
-		if ( ch == -1 )
-			break;
-		switch(ch) {
-		case 'C':
-			ca = optarg;
-			break;
-		case 'P':
-			ca_port = strtoul(optarg, 0, 0);
-			break;
-		case 'd':
-			ibdebug++;
-			break;
-		case 'D':
-			dest_type = IB_DEST_DRPATH;
-			break;
-		case 'g':
-			show_gid++;
-			break;
-		case 'G':
-			dest_type = IB_DEST_GUID;
-			break;
-		case 'l':
-			show_lid++;
-			break;
-		case 'L':
-			show_lid = -100;
-			break;
-		case 's':
-			if (ib_resolve_portid_str(&sm_portid, optarg, IB_DEST_LID, 0) < 0)
-				IBERROR("can't resolve SM destination port %s", optarg);
-			sm_id = &sm_portid;
-			break;
-		case 't':
-			timeout = strtoul(optarg, 0, 0);
-			madrpc_set_timeout(timeout);
-			break;
-		case 'V':
-			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
-			exit(-1);
-		default:
-			usage();
-			break;
-		}
-	}
+	argv0 = argv[0];
 	argc -= optind;
 	argv += optind;
 
@@ -197,10 +142,10 @@ main(int argc, char **argv)
 	if (!show_lid && !show_gid)
 		show_lid = show_gid = 1;
 
-	madrpc_init(ca, ca_port, mgmt_classes, 3);
+	madrpc_init(ibd_ca, ibd_ca_port, mgmt_classes, 3);
 
 	if (argc) {
-		if (ib_resolve_portid_str(&portid, argv[0], dest_type, sm_id) < 0)
+		if (ib_resolve_portid_str(&portid, argv[0], ibd_dest_type, ibd_sm_id) < 0)
 			IBERROR("can't resolve destination port %s", argv[0]);
 	} else {
 		if (ib_resolve_self(&portid, &port, 0) < 0)
diff --git a/infiniband-diags/src/ibnetdiscover.c b/infiniband-diags/src/ibnetdiscover.c
index 296cb07..04a250f 100644
--- a/infiniband-diags/src/ibnetdiscover.c
+++ b/infiniband-diags/src/ibnetdiscover.c
@@ -85,7 +85,6 @@ static char *linkspeed_str[] = {
 
 static int timeout = 2000;		/* ms */
 static int dumplevel = 0;
-static int verbose;
 static FILE *f;
 
 char *argv0 = "ibnetdiscover";
@@ -919,119 +918,79 @@ void dump_ports_report ()
 		}
 }
 
-void
-usage(void)
+static int list, group, ports_report;
+
+static int process_opt(void *context, int ch, char *optarg)
 {
-	fprintf(stderr, "Usage: %s [-d(ebug)] -e(rr_show) -v(erbose) -s(how) -l(ist) -g(rouping) -H(ca_list) -S(witch_list) -R(outer_list) -V(ersion) -C ca_name -P ca_port "
-			"-t(imeout) timeout_ms --node-name-map node-name-map] -p(orts) [<topology-file>]\n",
-			argv0);
-	fprintf(stderr, "       --node-name-map <node-name-map> specify a node name map file\n");
-	exit(-1);
+	switch (ch) {
+	case 1:
+		node_name_map_file = strdup(optarg);
+		break;
+	case 's':
+		dumplevel = 1;
+		break;
+	case 'l':
+		list = LIST_CA_NODE | LIST_SWITCH_NODE | LIST_ROUTER_NODE;
+		break;
+	case 'g':
+		group = 1;
+		break;
+	case 'S':
+		list = LIST_SWITCH_NODE;
+		break;
+	case 'H':
+		list = LIST_CA_NODE;
+		break;
+	case 'R':
+		list = LIST_ROUTER_NODE;
+		break;
+	case 'p':
+		ports_report = 1;
+		break;
+	default:
+		return -1;
+	}
+
+	return 0;
 }
 
-int
-main(int argc, char **argv)
+int main(int argc, char **argv)
 {
 	int mgmt_classes[2] = {IB_SMI_CLASS, IB_SMI_DIRECT_CLASS};
 	ib_portid_t my_portid = {0};
-	int udebug = 0, list = 0;
-	char *ca = 0;
-	int ca_port = 0;
-	int group = 0;
-	int ports_report = 0;
-
-	static char const str_opts[] = "C:P:t:devslgHSRpVhu";
-	static const struct option long_opts[] = {
-		{ "C", 1, 0, 'C'},
-		{ "P", 1, 0, 'P'},
-		{ "debug", 0, 0, 'd'},
-		{ "err_show", 0, 0, 'e'},
-		{ "verbose", 0, 0, 'v'},
-		{ "show", 0, 0, 's'},
-		{ "list", 0, 0, 'l'},
-		{ "grouping", 0, 0, 'g'},
-		{ "Hca_list", 0, 0, 'H'},
-		{ "Switch_list", 0, 0, 'S'},
-		{ "Router_list", 0, 0, 'R'},
-		{ "timeout", 1, 0, 't'},
-		{ "node-name-map", 1, 0, 1},
-		{ "ports", 0, 0, 'p'},
-		{ "Version", 0, 0, 'V'},
-		{ "help", 0, 0, 'h'},
-		{ "usage", 0, 0, 'u'},
+
+	const struct ibdiag_opt opts[] = {
+		{ "show", 's', 0, NULL, "show more information" },
+		{ "list", 'l', 0, NULL, "list of connected nodes" },
+		{ "grouping", 'g', 0, NULL, "show grouping" },
+		{ "Hca_list", 'H', 0, NULL, "list of connected CAs" },
+		{ "Switch_list", 'S', 0, NULL, "list of connected switches" },
+		{ "Router_list", 'R', 0, NULL, "list of connected routers" },
+		{ "node-name-map", 1, 1, "<file>", "node name map file" },
+		{ "ports", 'p', 0, NULL, "obtain a ports report" },
 		{ }
 	};
+	char usage_args[] = "[topology-file]";
+
+	ibdiag_process_opts(argc, argv, NULL, "sGDL", opts, process_opt,
+			    usage_args, NULL);
 
 	f = stdout;
 
 	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 'C':
-			ca = optarg;
-			break;
-		case 'P':
-			ca_port = strtoul(optarg, 0, 0);
-			break;
-		case 'd':
-			ibdebug++;
-			madrpc_show_errors(1);
-			umad_debug(udebug);
-			udebug++;
-			break;
-		case 't':
-			timeout = strtoul(optarg, 0, 0);
-			break;
-		case 'v':
-			verbose++;
-			dumplevel++;
-			break;
-		case 's':
-			dumplevel = 1;
-			break;
-		case 'e':
-			madrpc_show_errors(1);
-			break;
-		case 'l':
-			list = LIST_CA_NODE | LIST_SWITCH_NODE | LIST_ROUTER_NODE;
-			break;
-		case 'g':
-			group = 1;
-			break;
-		case 'S':
-			list = LIST_SWITCH_NODE;
-			break;
-		case 'H':
-			list = LIST_CA_NODE;
-			break;
-		case 'R':
-			list = LIST_ROUTER_NODE;
-			break;
-		case 'V':
-			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
-			exit(-1);
-		case 'p':
-			ports_report = 1;
-			break;
-		default:
-			usage();
-			break;
-		}
-	}
 	argc -= optind;
 	argv += optind;
 
+	if (ibd_timeout)
+		timeout = ibd_timeout;
+
+	if (ibverbose)
+		dumplevel = 1;
+
 	if (argc && !(f = fopen(argv[0], "w")))
 		IBERROR("can't open file %s for writing", argv[0]);
 
-	madrpc_init(ca, ca_port, mgmt_classes, 2);
+	madrpc_init(ibd_ca, ibd_ca_port, mgmt_classes, 2);
 	node_name_map = open_node_name_map(node_name_map_file);
 
 	if (discover(&my_portid) < 0)
diff --git a/infiniband-diags/src/ibping.c b/infiniband-diags/src/ibping.c
index 7e51210..241d0ad 100644
--- a/infiniband-diags/src/ibping.c
+++ b/infiniband-diags/src/ibping.c
@@ -50,11 +50,6 @@
 
 #include "ibdiag_common.h"
 
-#undef DEBUG
-#define	DEBUG	if (verbose) IBWARN
-
-static int dest_type = IB_DEST_LID;
-static int verbose;
 static char host_and_domain[IB_VENDOR_RANGE2_DATA_SIZE];
 static char last_host[IB_VENDOR_RANGE2_DATA_SIZE];
 
@@ -152,28 +147,11 @@ ibping(ib_portid_t *portid, int quiet)
 	return rtt;
 }
 
-static void
-usage(void)
-{
-	char *basename;
-
-	if (!(basename = strrchr(argv0, '/')))
-		basename = argv0;
-	else
-		basename++;
-
-	fprintf(stderr, "Usage: %s [-d(ebug) -e(rr_show) -v(erbose) -G(uid) -s smlid -V(ersion) -C ca_name -P ca_port "
-			"-t(imeout) timeout_ms -c ping_count -f(lood) -o oui -S(erver)] <dest lid|guid>\n",
-			basename);
-	exit(-1);
-}
-
 static uint64_t minrtt = ~0ull, maxrtt, total_rtt;
 static uint64_t start, total_time, replied, lost, ntrans;
 static ib_portid_t portid = {0};
 
-void
-report(int sig)
+void report(int sig)
 {
 	total_time = getcurrenttime() - start;
 
@@ -193,104 +171,57 @@ report(int sig)
 	exit(0);
 }
 
-int
-main(int argc, char **argv)
+static int server = 0, flood = 0, oui = IB_OPENIB_OUI;
+static unsigned count = ~0;
+
+static int process_opt(void *context, int ch, char *optarg)
+{
+	switch (ch) {
+	case 'c':
+		count = strtoul(optarg, 0, 0);
+		break;
+	case 'f':
+		flood++;
+		break;
+	case 'o':
+		oui = strtoul(optarg, 0, 0);
+		break;
+	case 'S':
+		server++;
+		break;
+	default:
+		return -1;
+	}
+	return 0;
+}
+
+int main(int argc, char **argv)
 {
 	int mgmt_classes[3] = {IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS};
 	int ping_class = IB_VENDOR_OPENIB_PING_CLASS;
-	ib_portid_t *sm_id = 0, sm_portid = {0};
-	int timeout = 0, udebug = 0, server = 0, flood = 0;
-	int oui = IB_OPENIB_OUI;
 	uint64_t rtt;
-	unsigned count = ~0;
 	char *err;
-	char *ca = 0;
-	int ca_port = 0;
-
-	static char const str_opts[] = "C:P:t:s:c:o:devGfSVhu";
-	static const struct option long_opts[] = {
-		{ "C", 1, 0, 'C'},
-		{ "P", 1, 0, 'P'},
-		{ "debug", 0, 0, 'd'},
-		{ "err_show", 0, 0, 'e'},
-		{ "verbose", 0, 0, 'v'},
-		{ "Guid", 0, 0, 'G'},
-		{ "s", 1, 0, 's'},
-		{ "timeout", 1, 0, 't'},
-		{ "c", 1, 0, 'c'},
-		{ "flood", 0, 0, 'f'},
-		{ "o", 1, 0, 'o'},
-		{ "Server", 0, 0, 'S'},
-		{ "Version", 0, 0, 'V'},
-		{ "help", 0, 0, 'h'},
-		{ "usage", 0, 0, 'u'},
+
+	const struct ibdiag_opt opts[] = {
+		{ "count", 'c', 1, "<num>", "stop after count packets" },
+		{ "flood", 'f', 0, NULL, "flood destination" },
+		{ "oui", 'o', 1, NULL, "use specified OUI number" },
+		{ "Server", 'S', 0, NULL, "start in server mode" },
 		{ }
 	};
+	char usage_args[] = "<dest lid|guid>";
 
-	argv0 = argv[0];
+	ibdiag_process_opts(argc, argv, NULL, "D", opts, process_opt,
+			    usage_args, NULL);
 
-	while (1) {
-		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
-		if ( ch == -1 )
-			break;
-		switch(ch) {
-		case 'C':
-			ca = optarg;
-			break;
-		case 'P':
-			ca_port = strtoul(optarg, 0, 0);
-			break;
-		case 'c':
-			count = strtoul(optarg, 0, 0);
-			break;
-		case 'd':
-			ibdebug++;
-			madrpc_show_errors(1);
-			umad_debug(udebug);
-			udebug++;
-			break;
-		case 'e':
-			madrpc_show_errors(1);
-			break;
-		case 'f':
-			flood++;
-			break;
-		case 'G':
-			dest_type = IB_DEST_GUID;
-			break;
-		case 'o':
-			oui = strtoul(optarg, 0, 0);
-			break;
-		case 's':
-			if (ib_resolve_portid_str(&sm_portid, optarg, IB_DEST_LID, 0) < 0)
-				IBERROR("can't resolve SM destination port %s", optarg);
-			sm_id = &sm_portid;
-			break;
-		case 'S':
-			server++;
-			break;
-		case 't':
-			timeout = strtoul(optarg, 0, 0);
-			madrpc_set_timeout(timeout);
-			break;
-		case 'v':
-			verbose++;
-			break;
-		case 'V':
-			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
-			exit(-1);
-		default:
-			usage();
-			break;
-		}
-	}
+	argv0 = argv[0];
 	argc -= optind;
 	argv += optind;
 
 	if (!argc && !server)
-		usage();
+		ibdiag_show_usage();
 
-	madrpc_init(ca, ca_port, mgmt_classes, 3);
+	madrpc_init(ibd_ca, ibd_ca_port, mgmt_classes, 3);
 
 	if (server) {
 		if (mad_register_server(ping_class, 0, 0, oui) < 0)
@@ -306,7 +237,7 @@ main(int argc, char **argv)
 	if (mad_register_client(ping_class, 0) < 0)
 		IBERROR("can't register ping class %d on this port", ping_class);
 
-	if (ib_resolve_portid_str(&portid, argv[0], dest_type, sm_id) < 0)
+	if (ib_resolve_portid_str(&portid, argv[0], ibd_dest_type, ibd_sm_id) < 0)
 		IBERROR("can't resolve destination port %s", argv[0]);
 
 	signal(SIGINT, report);
diff --git a/infiniband-diags/src/ibportstate.c b/infiniband-diags/src/ibportstate.c
index 89675ed..c2e4028 100644
--- a/infiniband-diags/src/ibportstate.c
+++ b/infiniband-diags/src/ibportstate.c
@@ -48,12 +48,6 @@
 
 #include "ibdiag_common.h"
 
-#undef DEBUG
-#define	DEBUG	if (verbose>1) IBWARN
-
-static int dest_type = IB_DEST_LID;
-static int verbose;
-
 char *argv0 = "ibportstate";
 
 /*******************************************/
@@ -195,39 +189,11 @@ validate_speed(int speed, int peerspeed, int lsa)
 	}
 }
 
-void
-usage(void)
-{
-	char *basename;
-
-	if (!(basename = strrchr(argv0, '/')))
-		basename = argv0;
-	else
-		basename++;
-
-	fprintf(stderr, "Usage: %s [-d(ebug) -e(rr_show) -v(erbose) -D(irect) -G(uid) -s smlid -V(ersion) -C ca_name -P ca_port "
-			"-t(imeout) timeout_ms] <dest dr_path|lid|guid> <portnum> [<op>]\n",
-			basename);
-	fprintf(stderr, "\tsupported ops: enable, disable, reset, speed, query\n");
-	fprintf(stderr, "\n\texamples:\n");
-	fprintf(stderr, "\t\t%s 3 1 disable\t\t\t# by lid\n", basename);
-	fprintf(stderr, "\t\t%s -G 0x2C9000100D051 1 enable\t# by guid\n", basename);
-	fprintf(stderr, "\t\t%s -D 0 1\t\t\t# (query) by direct route\n", basename);
-	fprintf(stderr, "\t\t%s 3 1 reset\t\t\t# by lid\n", basename);
-	fprintf(stderr, "\t\t%s 3 1 speed 1\t\t\t# by lid\n", basename);
-	exit(-1);
-}
-
-int
-main(int argc, char **argv)
+int main(int argc, char **argv)
 {
 	int mgmt_classes[3] = {IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS};
 	ib_portid_t portid = {0};
-	ib_portid_t *sm_id = 0, sm_portid = {0};
 	int err;
-	int timeout = 0, udebug = 0;
-	char *ca = 0;
-	int ca_port = 0;
 	int port_op = 0;	/* default to query */
 	int speed = 15;
 	int is_switch = 1;
@@ -240,80 +206,31 @@ main(int argc, char **argv)
 	ib_portid_t selfportid = {0};
 	int selfport = 0;
 
-	static char const str_opts[] = "C:P:t:s:devDGVhu";
-	static const struct option long_opts[] = {
-		{ "C", 1, 0, 'C'},
-		{ "P", 1, 0, 'P'},
-		{ "debug", 0, 0, 'd'},
-		{ "err_show", 0, 0, 'e'},
-		{ "verbose", 0, 0, 'v'},
-		{ "Direct", 0, 0, 'D'},
-		{ "Guid", 0, 0, 'G'},
-		{ "timeout", 1, 0, 't'},
-		{ "s", 1, 0, 's'},
-		{ "Version", 0, 0, 'V'},
-		{ "help", 0, 0, 'h'},
-		{ "usage", 0, 0, 'u'},
-		{ }
+	char usage_args[] = "<dest dr_path|lid|guid> <portnum> [<op>]\n"
+		"\nSupported ops: enable, disable, reset, speed, query";
+	const char *usage_examples[] = {
+		"3 1 disable\t\t\t# by lid",
+		"-G 0x2C9000100D051 1 enable\t# by guid",
+		"-D 0 1\t\t\t# (query) by direct route",
+		"3 1 reset\t\t\t# by lid",
+		"3 1 speed 1\t\t\t# by lid",
+		NULL
 	};
 
-	argv0 = argv[0];
 
-	while (1) {
-		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
-		if ( ch == -1 )
-			break;
-		switch(ch) {
-		case 'd':
-			ibdebug++;
-			madrpc_show_errors(1);
-			umad_debug(udebug);
-			udebug++;
-			break;
-		case 'e':
-			madrpc_show_errors(1);
-			break;
-		case 'D':
-			dest_type = IB_DEST_DRPATH;
-			break;
-		case 'G':
-			dest_type = IB_DEST_GUID;
-			break;
-		case 'C':
-			ca = optarg;
-			break;
-		case 'P':
-			ca_port = strtoul(optarg, 0, 0);
-			break;
-		case 's':
-			if (ib_resolve_portid_str(&sm_portid, optarg, IB_DEST_LID, 0) < 0)
-				IBERROR("can't resolve SM destination port %s", optarg);
-			sm_id = &sm_portid;
-			break;
-		case 't':
-			timeout = strtoul(optarg, 0, 0);
-			madrpc_set_timeout(timeout);
-			break;
-		case 'v':
-			verbose++;
-			break;
-		case 'V':
-			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
-			exit(-1);
-		default:
-			usage();
-			break;
-		}
-	}
+	ibdiag_process_opts(argc, argv, NULL, NULL, NULL, NULL,
+			    usage_args, usage_examples);
+
+	argv0 = argv[0];
 	argc -= optind;
 	argv += optind;
 
 	if (argc < 2)
-		usage();
+		ibdiag_show_usage();
 
-	madrpc_init(ca, ca_port, mgmt_classes, 3);
+	madrpc_init(ibd_ca, ibd_ca_port, mgmt_classes, 3);
 
-	if (ib_resolve_portid_str(&portid, argv[0], dest_type, sm_id) < 0)
+	if (ib_resolve_portid_str(&portid, argv[0], ibd_dest_type, ibd_sm_id) < 0)
 		IBERROR("can't resolve destination port %s", argv[0]);
 
 	/* First, make sure it is a switch port if it is a "set" */
diff --git a/infiniband-diags/src/ibroute.c b/infiniband-diags/src/ibroute.c
index 921b5dd..bcc8d99 100644
--- a/infiniband-diags/src/ibroute.c
+++ b/infiniband-diags/src/ibroute.c
@@ -52,10 +52,7 @@
 
 #include "ibdiag_common.h"
 
-static int dest_type = IB_DEST_LID;
-static int brief;
-static int verbose;
-static int dump_all;
+static int brief, dump_all, multicast;
 
 char *argv0 = "ibroute";
 
@@ -191,7 +188,7 @@ dump_multicast_tables(ib_portid_t *portid, int startlid, int endlid)
 		printf("     Ports: %s\n", str);
 		printf(" MLid\n");
 	}
-	if (verbose)
+	if (ibverbose)
 		printf("Switch muticast mlids capability is 0x%d\n", cap);
 
 	chunks = ALIGN(nports + 1, 16) / 16;
@@ -349,138 +346,76 @@ dump_unicast_tables(ib_portid_t *portid, int startlid, int endlid)
 	return 0;
 }
 
-void
-usage(void)
+static int process_opt(void *context, int ch, char *optarg)
 {
-	char *basename;
-
-	if (!(basename = strrchr(argv0, '/')))
-		basename = argv0;
-	else
-		basename++;
-
-	fprintf(stderr, "Usage: %s [-d(ebug)] -a(ll) -n(o_dests) -v(erbose) -D(irect) -G(uid) -M(ulticast) -s smlid -V(ersion) -C ca_name -P ca_port "
-			"-t(imeout) timeout_ms] [<dest dr_path|lid|guid> [<startlid> [<endlid>]]]\n",
-			basename);
-	fprintf(stderr, "\n\tUnicast examples:\n");
-	fprintf(stderr, "\t\t%s 4\t# dump all lids with valid out ports of switch with lid 4\n", basename);
-	fprintf(stderr, "\t\t%s -a 4\t# same, but dump all lids, even with invalid out ports\n", basename);
-	fprintf(stderr, "\t\t%s -n 4\t# simple dump format - no destination resolving\n", basename);
-	fprintf(stderr, "\t\t%s 4 10\t# dump lids starting from 10\n", basename);
-	fprintf(stderr, "\t\t%s 4 0x10 0x20\t# dump lid range\n", basename);
-	fprintf(stderr, "\t\t%s -G 0x08f1040023\t# resolve switch by GUID\n", basename);
-	fprintf(stderr, "\t\t%s -D 0,1\t# resolve switch by direct path\n", basename);
-
-	fprintf(stderr, "\n\tMulticast examples:\n");
-	fprintf(stderr, "\t\t%s -M 4\t# dump all non empty mlids of switch with lid 4\n", basename);
-	fprintf(stderr, "\t\t%s -M 4 0xc010 0xc020\t# same, but with range\n", basename);
-	fprintf(stderr, "\t\t%s -M -n 4\t# simple dump format\n", basename);
-	exit(-1);
+	switch (ch) {
+	case 'a':
+		dump_all++;
+		break;
+	case 'M':
+		multicast++;
+		break;
+	case 'n':
+		brief++;
+		break;
+	default:
+		return -1;
+	}
+	return 0;
 }
 
-int
-main(int argc, char **argv)
+int main(int argc, char **argv)
 {
 	int mgmt_classes[3] = {IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS};
 	ib_portid_t portid = {0};
-	ib_portid_t *sm_id = 0, sm_portid = {0};
-	int timeout;
-	int multicast = 0, startlid = 0, endlid = 0;
+	int startlid = 0, endlid = 0;
 	char *err;
-	char *ca = 0;
-	int ca_port = 0;
-
-	static char const str_opts[] = "C:P:t:s:danvDGMVhu";
-	static const struct option long_opts[] = {
-		{ "C", 1, 0, 'C'},
-		{ "P", 1, 0, 'P'},
-		{ "debug", 0, 0, 'd'},
-		{ "all", 0, 0, 'a'},
-		{ "no_dests", 0, 0, 'n'},
-		{ "verbose", 0, 0, 'v'},
-		{ "Direct", 0, 0, 'D'},
-		{ "Guid", 0, 0, 'G'},
-		{ "Multicast", 0, 0, 'M'},
-		{ "timeout", 1, 0, 't'},
-		{ "s", 1, 0, 's'},
-		{ "Version", 0, 0, 'V'},
-		{ "help", 0, 0, 'h'},
-		{ "usage", 0, 0, 'u'},
+
+	const struct ibdiag_opt opts[] = {
+		{ "all", 'a', 0, NULL, "show all lids, even invalid entries" },
+		{ "no_dests", 'n', 0, NULL, "do not try to resolve destinations" },
+		{ "Multicast", 'M', 0, NULL, "show multicast forwarding tables" },
 		{ }
 	};
+	char usage_args[] = "[<dest dr_path|lid|guid> [<startlid> [<endlid>]]]";
+	const char *usage_examples[] = {
+		" -- Unicast examples:",
+		"4\t# dump all lids with valid out ports of switch with lid 4",
+		"-a 4\t# same, but dump all lids, even with invalid out ports",
+		"-n 4\t# simple dump format - no destination resolving",
+		"4 10\t# dump lids starting from 10",
+		"4 0x10 0x20\t# dump lid range",
+		"-G 0x08f1040023\t# resolve switch by GUID",
+		"-D 0,1\t# resolve switch by direct path",
+		" -- Multicast examples:",
+		"-M 4\t# dump all non empty mlids of switch with lid 4",
+		"-M 4 0xc010 0xc020\t# same, but with range",
+		"-M -n 4\t# simple dump format",
+		NULL,
+	};
 
-	argv0 = argv[0];
+	ibdiag_process_opts(argc, argv, NULL, NULL, opts, process_opt,
+			    usage_args, usage_examples);
 
-	while (1) {
-		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
-		if ( ch == -1 )
-			break;
-		switch(ch) {
-		case 'C':
-			ca = optarg;
-			break;
-		case 'P':
-			ca_port = strtoul(optarg, 0, 0);
-			break;
-		case 'a':
-			dump_all++;
-			break;
-		case 'd':
-			ibdebug++;
-			break;
-		case 'D':
-			dest_type = IB_DEST_DRPATH;
-			break;
-		case 'G':
-			dest_type = IB_DEST_GUID;
-			break;
-		case 'M':
-			multicast++;
-			break;
-		case 'n':
-			brief++;
-			break;
-		case 's':
-			if (ib_resolve_portid_str(&sm_portid, optarg, IB_DEST_LID, 0) < 0)
-				IBERROR("can't resolve SM destination port %s", optarg);
-			sm_id = &sm_portid;
-			break;
-		case 't':
-			timeout = strtoul(optarg, 0, 0);
-			madrpc_set_timeout(timeout);
-			break;
-		case 'v':
-			madrpc_show_errors(1);
-			verbose++;
-			break;
-		case 'V':
-			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
-			exit(-1);
-		default:
-			usage();
-			break;
-		}
-	}
+	argv0 = argv[0];
 	argc -= optind;
 	argv += optind;
 
 	if (!argc)
-		usage();
+		ibdiag_show_usage();
 
 	if (argc > 1)
 		startlid = strtoul(argv[1], 0, 0);
 	if (argc > 2)
 		endlid = strtoul(argv[2], 0, 0);
 
-	madrpc_init(ca, ca_port, mgmt_classes, 3);
+	madrpc_init(ibd_ca, ibd_ca_port, mgmt_classes, 3);
 
 	if (!argc) {
 		if (ib_resolve_self(&portid, 0, 0) < 0)
 			IBERROR("can't resolve self addr");
-	} else {
-		if (ib_resolve_portid_str(&portid, argv[0], dest_type, sm_id) < 0)
-			IBERROR("can't resolve destination port %s", argv[1]);
-	}
+	} else if (ib_resolve_portid_str(&portid, argv[0], ibd_dest_type, ibd_sm_id) < 0)
+		IBERROR("can't resolve destination port %s", argv[1]);
 
 	if (multicast)
 		err = dump_multicast_tables(&portid, startlid, endlid);
diff --git a/infiniband-diags/src/ibsendtrap.c b/infiniband-diags/src/ibsendtrap.c
index 66620de..a733a2c 100644
--- a/infiniband-diags/src/ibsendtrap.c
+++ b/infiniband-diags/src/ibsendtrap.c
@@ -47,7 +47,7 @@
 
 #include "ibdiag_common.h"
 
-char *argv0 = "";
+char *argv0 = "ibsendtrap";
 
 static int send_144_node_desc_update(void)
 {
@@ -95,23 +95,6 @@ trap_def_t traps[2] = {
 	{NULL, NULL}
 };
 
-static void usage(void)
-{
-	int i;
-
-	fprintf(stderr, "Usage: %s [-hV]"
-		" [-C <ca_name>] [-P <ca_port>] [<trap_name>]\n", argv0);
-	fprintf(stderr, "   -V print version\n");
-	fprintf(stderr, "   <trap_name> can be one of the following\n");
-	for (i = 0; traps[i].trap_name; i++) {
-		fprintf(stderr, "      %s\n", traps[i].trap_name);
-	}
-	fprintf(stderr, "   default behavior is to send \"%s\"\n",
-		traps[0].trap_name);
-
-	exit(-1);
-}
-
 int send_trap(char *trap_name)
 {
 	int i;
@@ -121,45 +104,32 @@ int send_trap(char *trap_name)
 			return (traps[i].send_func());
 		}
 	}
-	usage();
+	ibdiag_show_usage();
 	exit(1);
 }
 
 int main(int argc, char **argv)
 {
+	char usage_args[1024];
 	int mgmt_classes[2] = { IB_SMI_CLASS, IB_SMI_DIRECT_CLASS };
-	int ch = 0;
 	char *trap_name = NULL;
-	char *ca = NULL;
-	int ca_port = 0;
-
-	static char const str_opts[] = "hVP:C:";
-	static const struct option long_opts[] = {
-		{"Version", 0, 0, 'V'},
-		{"P", 1, 0, 'P'},
-		{"C", 1, 0, 'C'},
-		{"help", 0, 0, 'h'},
-		{}
-	};
+	int i, n;
 
-	argv0 = argv[0];
-
-	while ((ch = getopt_long(argc, argv, str_opts, long_opts, NULL)) != -1) {
-		switch (ch) {
-		case 'V':
-			fprintf(stderr, "%s %s\n", argv0, get_build_version());
+	n = sprintf(usage_args, "[<trap_name>]\n"
+		    "\nArgument <trap_name> can be one of the following:\n");
+	for (i = 0; traps[i].trap_name; i++) {
+		n += snprintf(usage_args + n, sizeof(usage_args) - n,
+			      "  %s\n", traps[i].trap_name);
+		if (n >= sizeof(usage_args))
 			exit(-1);
-		case 'C':
-			ca = optarg;
-			break;
-		case 'P':
-			ca_port = strtoul(optarg, NULL, 0);
-			break;
-		case 'h':
-		default:
-			usage();
-		}
 	}
+	snprintf(usage_args + n, sizeof(usage_args) - n,
+		 "\n  default behavior is to send \"%s\"", traps[0].trap_name);
+
+	ibdiag_process_opts(argc, argv, NULL, "DLG", NULL, NULL,
+			    usage_args, NULL);
+
+	argv0 = argv[0];
 	argc -= optind;
 	argv += optind;
 
@@ -170,7 +140,7 @@ int main(int argc, char **argv)
 	}
 
 	madrpc_show_errors(1);
-	madrpc_init(ca, ca_port, mgmt_classes, 2);
+	madrpc_init(ibd_ca, ibd_ca_port, mgmt_classes, 2);
 
 	return (send_trap(trap_name));
 }
diff --git a/infiniband-diags/src/ibstat.c b/infiniband-diags/src/ibstat.c
index 6bd3c8a..c4f965e 100644
--- a/infiniband-diags/src/ibstat.c
+++ b/infiniband-diags/src/ibstat.c
@@ -49,8 +49,6 @@
 
 #include <ibdiag_common.h>
 
-static int debug;
-
 char *argv0 = "ibstat";
 
 static char *node_type_str[] = {
@@ -174,63 +172,49 @@ ports_list(char names[][UMAD_CA_NAME_LEN], int n)
 	return found;
 }
 
-void
-usage(void)
+static int list_only, short_format, list_ports;
+
+static int process_opt(void *context, int ch, char *optarg)
 {
-	fprintf(stderr, "Usage: %s [-d(ebug) -l(ist_of_cas) -s(hort) -p(ort_list) -V(ersion)] <ca_name> [portnum]\n", argv0);
-	fprintf(stderr, "\tExamples:\n");
-	fprintf(stderr, "\t\t%s -l	  # list all IB devices\n", argv0);
-	fprintf(stderr, "\t\t%s mthca0 2 # stat port 2 of 'mthca0'\n", argv0);
-	exit(-1);
+	switch (ch) {
+	case 'l':
+		list_only++;
+		break;
+	case 's':
+		short_format++;
+		break;
+	case 'p':
+		list_ports++;
+		break;
+	default:
+		return -1;
+	}
+	return 0;
 }
 
-int
-main(int argc, char *argv[])
+int main(int argc, char *argv[])
 {
 	char names[UMAD_MAX_DEVICES][UMAD_CA_NAME_LEN];
 	int dev_port = -1;
-	int list_only = 0, short_format = 0, list_ports = 0;
 	int n, i;
 
-	static char const str_opts[] = "dlspVhu";
-	static const struct option long_opts[] = {
-		{ "debug", 0, 0, 'd'},
-		{ "list_of_cas", 0, 0, 'l'},
-		{ "short", 0, 0, 's'},
-		{ "port_list", 0, 0, 'p'},
-		{ "Version", 0, 0, 'V'},
-		{ "help", 0, 0, 'h'},
-		{ "usage", 0, 0, 'u'},
+	const struct ibdiag_opt opts[] = {
+		{ "list_of_cas", 'l', 0, NULL, "list all IB devices" },
+		{ "short", 's', 0, NULL, "short output" },
+		{ "port_list", 'p', 0, NULL, "show port list" },
 		{ }
 	};
+	char usage_args[] = "<ca_name> [portnum]";
+	const char *usage_examples[] = {
+		"-l       # list all IB devices",
+		"mthca0 2 # stat port 2 of 'mthca0'",
+		NULL
+	};
 
-	argv0 = argv[0];
+	ibdiag_process_opts(argc, argv, NULL, "sDGLCPte", opts, process_opt,
+			    usage_args, usage_examples);
 
-	while (1) {
-		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
-		if ( ch == -1 )
-			break;
-		switch(ch) {
-		case 'd':
-			debug++;
-			break;
-		case 'l':
-			list_only++;
-			break;
-		case 's':
-			short_format++;
-			break;
-		case 'p':
-			list_ports++;
-			break;
-		case 'V':
-			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
-			exit(-1);
-		default:
-			usage();
-			break;
-		}
-	}
+	argv0 = argv[0];
 	argc -= optind;
 	argv += optind;
 
diff --git a/infiniband-diags/src/ibsysstat.c b/infiniband-diags/src/ibsysstat.c
index 29d6327..792e8f4 100644
--- a/infiniband-diags/src/ibsysstat.c
+++ b/infiniband-diags/src/ibsysstat.c
@@ -48,12 +48,6 @@
 
 #include "ibdiag_common.h"
 
-#undef DEBUG
-#define	DEBUG	if (verbose) IBWARN
-
-static int dest_type = IB_DEST_LID;
-static int verbose;
-
 #define MAX_CPUS 8
 
 enum ib_sysstat_attr_t {
@@ -219,114 +213,52 @@ build_cpuinfo(void)
 	return ncpu;
 }
 
-static void
-usage(void)
-{
-	char *basename;
-
-	if (!(basename = strrchr(argv0, '/')))
-		basename = argv0;
-	else
-		basename++;
+static int server = 0, oui = IB_OPENIB_OUI;
 
-	fprintf(stderr, "Usage: %s [-d(ebug) -e(rr_show) -v(erbose) -G(uid) -s smlid -V(ersion) -C ca_name -P ca_port "
-			"-t(imeout) timeout_ms -o oui -S(erver)] <dest lid|guid> [<op>]\n",
-			basename);
-	exit(-1);
+static int process_opt(void *context, int ch, char *optarg)
+{
+	switch (ch) {
+	case 'o':
+		oui = strtoul(optarg, 0, 0);
+		break;
+	case 'S':
+		server++;
+		break;
+	default:
+		return -1;
+	}
+	return 0;
 }
 
-int
-main(int argc, char **argv)
+int main(int argc, char **argv)
 {
 	int mgmt_classes[3] = {IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS};
 	int sysstat_class = IB_VENDOR_OPENIB_SYSSTAT_CLASS;
 	ib_portid_t portid = {0};
-	ib_portid_t *sm_id = 0, sm_portid = {0};
-	int timeout = 0, udebug = 0, server = 0;
-	int oui = IB_OPENIB_OUI, attr = IB_PING_ATTR;
+	int attr = IB_PING_ATTR;
 	char *err;
-	char *ca = 0;
-	int ca_port = 0;
-
-	static char const str_opts[] = "C:P:t:s:o:devGSVhu";
-	static const struct option long_opts[] = {
-		{ "C", 1, 0, 'C'},
-		{ "P", 1, 0, 'P'},
-		{ "debug", 0, 0, 'd'},
-		{ "err_show", 0, 0, 'e'},
-		{ "verbose", 0, 0, 'v'},
-		{ "Guid", 0, 0, 'G'},
-		{ "timeout", 1, 0, 't'},
-		{ "s", 1, 0, 's'},
-		{ "o", 1, 0, 'o'},
-		{ "Server", 0, 0, 'S'},
-		{ "Version", 0, 0, 'V'},
-		{ "help", 0, 0, 'h'},
-		{ "usage", 0, 0, 'u'},
+
+	const struct ibdiag_opt opts[] = {
+		{ "oui", 'o', 1, NULL, "use specified OUI number" },
+		{ "Server", 'S', 0, NULL, "start in server mode" },
 		{ }
 	};
+	char usage_args[] = "<dest lid|guid> [<op>]";
 
-	argv0 = argv[0];
+	ibdiag_process_opts(argc, argv, NULL, "D", opts, process_opt,
+			    usage_args, NULL);
 
-	while (1) {
-		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
-		if ( ch == -1 )
-			break;
-		switch(ch) {
-		case 'C':
-			ca = optarg;
-			break;
-		case 'P':
-			ca_port = strtoul(optarg, 0, 0);
-			break;
-		case 'd':
-			ibdebug++;
-			madrpc_show_errors(1);
-			umad_debug(udebug);
-			udebug++;
-			break;
-		case 'e':
-			madrpc_show_errors(1);
-			break;
-		case 'G':
-			dest_type = IB_DEST_GUID;
-			break;
-		case 'o':
-			oui = strtoul(optarg, 0, 0);
-			break;
-		case 's':
-			if (ib_resolve_portid_str(&sm_portid, optarg, IB_DEST_LID, 0) < 0)
-				IBERROR("can't resolve SM destination port %s", optarg);
-			sm_id = &sm_portid;
-			break;
-		case 'S':
-			server++;
-			break;
-		case 't':
-			timeout = strtoul(optarg, 0, 0);
-			madrpc_set_timeout(timeout);
-			break;
-		case 'v':
-			verbose++;
-			break;
-		case 'V':
-			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
-			exit(-1);
-		default:
-			usage();
-			break;
-		}
-	}
+	argv0 = argv[0];
 	argc -= optind;
 	argv += optind;
 
 	if (!argc && !server)
-		usage();
+		ibdiag_show_usage();
 
 	if (argc > 1 && (attr = match_attr(argv[1])) < 0)
-		usage();
+		ibdiag_show_usage();
 
-	madrpc_init(ca, ca_port, mgmt_classes, 3);
+	madrpc_init(ibd_ca, ibd_ca_port, mgmt_classes, 3);
 
 	if (server) {
 		if (mad_register_server(sysstat_class, 0, 0, oui) < 0)
@@ -342,7 +274,7 @@ main(int argc, char **argv)
 	if (mad_register_client(sysstat_class, 0) < 0)
 		IBERROR("can't register to sysstat class %d", sysstat_class);
 
-	if (ib_resolve_portid_str(&portid, argv[0], dest_type, sm_id) < 0)
+	if (ib_resolve_portid_str(&portid, argv[0], ibd_dest_type, ibd_sm_id) < 0)
 		IBERROR("can't resolve destination port %s", argv[0]);
 
 	if ((err = ibsystat(&portid, attr)))
diff --git a/infiniband-diags/src/ibtracert.c b/infiniband-diags/src/ibtracert.c
index 7a28940..5b9a210 100644
--- a/infiniband-diags/src/ibtracert.c
+++ b/infiniband-diags/src/ibtracert.c
@@ -63,7 +63,6 @@ static char *node_type_str[] = {
 };
 
 static int timeout = 0;		/* ms */
-static int verbose;
 static int force;
 static FILE *f;
 
@@ -220,7 +219,7 @@ dump_route(int dump, Node *node, int outport, Port *port)
 {
 	char *nodename = NULL;
 
-	if (!dump && !verbose)
+	if (!dump && !ibverbose)
 		return;
 
 	nodename = remap_node_name(node_name_map, node->nodeguid, node->nodedesc);
@@ -413,7 +412,7 @@ new_node(Node *node, Port *port, ib_portid_t *path, int dist)
 	if (port->portguid == target_portguid) {
 		node->dist = -1;		/* tag as target */
 		link_port(port, node);
-		dump_endnode(verbose, "found target", node, port);
+		dump_endnode(ibverbose, "found target", node, port);
 		return 1;	/* found; */
 	}
 
@@ -522,7 +521,7 @@ find_mcpath(ib_portid_t *from, int mlid)
 			path = &node->path;
 
 			VERBOSE("dist %d node %p", dist, node);
-			dump_endnode(verbose, "processing", node, node->ports);
+			dump_endnode(ibverbose, "processing", node, node->ports);
 
 			memset(map, 0, sizeof(map));
 
@@ -599,7 +598,7 @@ find_mcpath(ib_portid_t *from, int mlid)
 					return remotenode;
 
 				if (r == 0)
-					dump_endnode(verbose, "new remote",
+					dump_endnode(ibverbose, "new remote",
 						remotenode, remoteport);
 				else if (remotenode->type == IB_NODE_SWITCH)
 					dump_endnode(2, "ERR: circle discovered at",
@@ -686,140 +685,82 @@ static int resolve_lid(ib_portid_t  *portid, const void *srcport)
 	return 0;
 }
 
-static void
-usage(void)
-{
-	char *basename;
+static int dumplevel = 2, multicast, mlid;
 
-	if (!(basename = strrchr(argv0, '/')))
-		basename = argv0;
-	else
-		basename++;
-
-	fprintf(stderr, "Usage: %s [-d(ebug) -v(erbose) -D(irect) -G(uids) -n(o_info) -C ca_name -P ca_port "
-			"-s smlid -t(imeout) timeout_ms -m mlid --node-name-map node-name-map ] <src-addr> <dest-addr>\n",
-			basename);
-	fprintf(stderr, "\n\tUnicast examples:\n");
-	fprintf(stderr, "\t\t%s 4 16\t\t\t# show path between lids 4 and 16\n", basename);
-	fprintf(stderr, "\t\t%s -n 4 16\t\t# same, but using simple output format\n", basename);
-	fprintf(stderr, "\t\t%s -G 0x8f1040396522d 0x002c9000100d051\t# use guid addresses\n", basename);
-
-	fprintf(stderr, "\n\tMulticast example:\n");
-	fprintf(stderr, "\t\t%s -m 0xc000 4 16\t# show multicast path of mlid 0xc000 between lids 4 and 16\n", basename);
-	exit(-1);
+static int process_opt(void *context, int ch, char *optarg)
+{
+	switch (ch) {
+	case 1:
+		node_name_map_file = strdup(optarg);
+		break;
+	case 'm':
+		multicast++;
+		mlid = strtoul(optarg, 0, 0);
+		break;
+	case 'f':
+		force++;
+		break;
+	case 'n':
+		dumplevel = 1;
+		break;
+	default:
+		return -1;
+	}
+	return 0;
 }
 
-int
-main(int argc, char **argv)
+int main(int argc, char **argv)
 {
 	int mgmt_classes[3] = {IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS};
 	ib_portid_t my_portid = {0};
 	ib_portid_t src_portid = {0};
 	ib_portid_t dest_portid = {0};
-	ib_portid_t *sm_id = 0, sm_portid = {0};
-	int dumplevel = 2, dest_type = IB_DEST_LID, multicast = 0, mlid = 0;
 	Node *endnode;
-	int udebug = 0;
-	char *ca = 0;
-	int ca_port = 0;
-
-	static char const str_opts[] = "C:P:t:s:m:dvfDGnVhu";
-	static const struct option long_opts[] = {
-		{ "C", 1, 0, 'C'},
-		{ "P", 1, 0, 'P'},
-		{ "debug", 0, 0, 'd'},
-		{ "verbose", 0, 0, 'v'},
-		{ "force", 0, 0, 'f'},
-		{ "Direct", 0, 0, 'D'},
-		{ "Guids", 0, 0, 'G'},
-		{ "no_info", 0, 0, 'n'},
-		{ "timeout", 1, 0, 't'},
-		{ "s", 1, 0, 's'},
-		{ "m", 1, 0, 'm'},
-		{ "Version", 0, 0, 'V'},
-		{ "help", 0, 0, 'h'},
-		{ "usage", 0, 0, 'u'},
-		{ "node-name-map", 1, 0, 1},
+
+	const struct ibdiag_opt opts[] = {
+		{ "force", 'f', 0, NULL, "force" },
+		{ "no_info", 'n', 0, NULL, "simple format" },
+		{ "mlid", 'm', 1, "<mlid>", "multicast trace of the mlid" },
+		{ "node-name-map", 1, 1, "<file>", "node name map file" },
 		{ }
 	};
+	char usage_args[] = "<src-addr> <dest-addr>";
+	const char *usage_examples[] = {
+		"- Unicast examples:",
+		"4 16\t\t\t# show path between lids 4 and 16",
+		"-n 4 16\t\t# same, but using simple output format",
+		"-G 0x8f1040396522d 0x002c9000100d051\t# use guid addresses",
+
+		" - Multicast examples:",
+		"-m 0xc000 4 16\t# show multicast path of mlid 0xc000 between lids 4 and 16",
+		NULL,
+	};
 
-	argv0 = argv[0];
 
-	f = stdout;
+	ibdiag_process_opts(argc, argv, NULL, NULL, opts, process_opt,
+			    usage_args, usage_examples);
 
-	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 'C':
-			ca = optarg;
-			break;
-		case 'P':
-			ca_port = strtoul(optarg, 0, 0);
-			break;
-		case 'd':
-			ibdebug++;
-			madrpc_show_errors(1);
-			umad_debug(udebug);
-			udebug++;
-			break;
-		case 'D':
-			dest_type = IB_DEST_DRPATH;
-			break;
-		case 'G':
-			dest_type = IB_DEST_GUID;
-			break;
-		case 'm':
-			multicast++;
-			mlid = strtoul(optarg, 0, 0);
-			break;
-		case 'f':
-			force++;
-			break;
-		case 'n':
-			dumplevel = 1;
-			break;
-		case 's':
-			if (ib_resolve_portid_str(&sm_portid, optarg, IB_DEST_LID, 0) < 0)
-				IBERROR("can't resolve SM destination port %s", optarg);
-			sm_id = &sm_portid;
-			break;
-		case 't':
-			timeout = strtoul(optarg, 0, 0);
-			madrpc_set_timeout(timeout);
-			break;
-		case 'v':
-			madrpc_show_errors(1);
-			verbose++;
-			break;
-		case 'V':
-			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
-			exit(-1);
-		default:
-			usage();
-			break;
-		}
-	}
+	f = stdout;
+	argv0 = argv[0];
 	argc -= optind;
 	argv += optind;
 
 	if (argc < 2)
-		usage();
+		ibdiag_show_usage();
+
+	if (ibd_timeout)
+		timeout = ibd_timeout;
 
-	madrpc_init(ca, ca_port, mgmt_classes, 3);
+	madrpc_init(ibd_ca, ibd_ca_port, mgmt_classes, 3);
 	node_name_map = open_node_name_map(node_name_map_file);
 
-	if (ib_resolve_portid_str(&src_portid, argv[0], dest_type, sm_id) < 0)
+	if (ib_resolve_portid_str(&src_portid, argv[0], ibd_dest_type, ibd_sm_id) < 0)
 		IBERROR("can't resolve source port %s", argv[0]);
 
-	if (ib_resolve_portid_str(&dest_portid, argv[1], dest_type, sm_id) < 0)
+	if (ib_resolve_portid_str(&dest_portid, argv[1], ibd_dest_type, ibd_sm_id) < 0)
 		IBERROR("can't resolve destination port %s", argv[1]);
 
-	if (dest_type == IB_DEST_DRPATH) {
+	if (ibd_dest_type == IB_DEST_DRPATH) {
 		if (resolve_lid(&src_portid, NULL) < 0)
 			IBERROR("cannot resolve lid for port \'%s\'",
 				portid2str(&src_portid));
@@ -830,10 +771,10 @@ main(int argc, char **argv)
 
 	if (dest_portid.lid == 0 || src_portid.lid == 0) {
 		IBWARN("bad src/dest lid");
-		usage();
+		ibdiag_show_usage();
 	}
 
-	if (dest_type != IB_DEST_DRPATH) {
+	if (ibd_dest_type != IB_DEST_DRPATH) {
 		/* first find a direct path to the src port */
 		if (find_route(&my_portid, &src_portid, 0) < 0)
 			IBERROR("can't find a route to the src port");
diff --git a/infiniband-diags/src/perfquery.c b/infiniband-diags/src/perfquery.c
index 6662926..5bf15c5 100644
--- a/infiniband-diags/src/perfquery.c
+++ b/infiniband-diags/src/perfquery.c
@@ -92,34 +92,6 @@ char *argv0 = "perfquery";
 
 #define ALL_PORTS 0xFF
 
-static void
-usage(void)
-{
-	char *basename;
-
-	if (!(basename = strrchr(argv0, '/')))
-		basename = argv0;
-	else
-		basename++;
-
-	fprintf(stderr, "Usage: %s [-d(ebug) -G(uid) -a(ll_ports) -l(oop_ports) -r(eset_after_read) -C ca_name -P ca_port "
-			"-R(eset_only) -t(imeout) timeout_ms -V(ersion) -h(elp)] [<lid|guid> [[port] [reset_mask]]]\n",
-			basename);
-	fprintf(stderr, "\tExamples:\n");
-	fprintf(stderr, "\t\t%s\t\t# read local port's performance counters\n", basename);
-	fprintf(stderr, "\t\t%s 32 1\t\t# read performance counters from lid 32, port 1\n", basename);
-	fprintf(stderr, "\t\t%s -e 32 1\t# read extended performance counters from lid 32, port 1\n", basename);
-	fprintf(stderr, "\t\t%s -a 32\t\t# read performance counters from lid 32, all ports\n", basename);
-	fprintf(stderr, "\t\t%s -r 32 1\t# read performance counters and reset\n", basename);
-	fprintf(stderr, "\t\t%s -e -r 32 1\t# read extended performance counters and reset\n", basename);
-	fprintf(stderr, "\t\t%s -R 0x20 1\t# reset performance counters of port 1 only\n", basename);
-	fprintf(stderr, "\t\t%s -e -R 0x20 1\t# reset extended performance counters of port 1 only\n", basename);
-	fprintf(stderr, "\t\t%s -R -a 32\t# reset performance counters of all ports\n", basename);
-	fprintf(stderr, "\t\t%s -R 32 2 0x0fff\t# reset only error counters of port 2\n", basename);
-	fprintf(stderr, "\t\t%s -R 32 2 0xf000\t# reset only non-error counters of port 2\n", basename);
-	exit(-1);
-}
-
 /* Notes: IB semantics is to cap counters if count has exceeded limits.
  * Therefore we must check for overflows and cap the counters if necessary.
  *
@@ -338,104 +310,74 @@ static void reset_counters(int extended, int timeout, int mask, ib_portid_t *por
 	}
 }
 
-int
-main(int argc, char **argv)
+static int reset, reset_only, all_ports, loop_ports, port, extended;
+
+static int process_opt(void *context, int ch, char *optarg)
+{
+	switch (ch) {
+	case 'e':
+		extended = 1;
+		break;
+	case 'a':
+		all_ports++;
+		port = ALL_PORTS;
+		break;
+	case 'l':
+		loop_ports++;
+		break;
+	case 'r':
+		reset++;
+		break;
+	case 'R':
+		reset_only++;
+		break;
+	default:
+		return -1;
+	}
+	return 0;
+}
+
+int main(int argc, char **argv)
 {
 	int mgmt_classes[4] = {IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS, IB_PERFORMANCE_CLASS};
-	ib_portid_t *sm_id = 0, sm_portid = {0};
 	ib_portid_t portid = {0};
-	int dest_type = IB_DEST_LID;
-	int timeout = 0;	/* use default */
-	int mask = 0xffff, all_ports = 0;
-	int reset = 0, reset_only = 0;
-	int port = 0;
-	int udebug = 0;
-	char *ca = 0;
-	int ca_port = 0;
-	int extended = 0;
+	int mask = 0xffff;
 	uint16_t cap_mask;
 	int all_ports_loop = 0;
-	int loop_ports = 0;
 	int node_type, num_ports = 0;
 	uint8_t data[IB_SMP_DATA_SIZE];
 	int start_port = 1;
 	int enhancedport0;
 	int i;
 
-	static char const str_opts[] = "C:P:s:t:dGealrRVhu";
-	static const struct option long_opts[] = {
-		{ "C", 1, 0, 'C'},
-		{ "P", 1, 0, 'P'},
-		{ "debug", 0, 0, 'd'},
-		{ "Guid", 0, 0, 'G'},
-		{ "extended", 0, 0, 'e'},
-		{ "all_ports", 0, 0, 'a'},
-		{ "loop_ports", 0, 0, 'l'},
-		{ "reset_after_read", 0, 0, 'r'},
-		{ "Reset_only", 0, 0, 'R'},
-		{ "sm_portid", 1, 0, 's'},
-		{ "timeout", 1, 0, 't'},
-		{ "Version", 0, 0, 'V'},
-		{ "help", 0, 0, 'h'},
-		{ "usage", 0, 0, 'u'},
+	const struct ibdiag_opt opts[] = {
+		{ "extended", 'e', 0, NULL, "show extended port counters" },
+		{ "all_ports", 'a', 0, NULL, "show aggregated counters" },
+		{ "loop_ports", 'l', 0, NULL, "iterate through each port" },
+		{ "reset_after_read", 'r', 0, NULL, "reset counters after read" },
+		{ "Reset_only", 'R', 0, NULL, "only reset counters" },
 		{ }
 	};
+	char usage_args[] = " [<lid|guid> [[port] [reset_mask]]]";
+	const char *usage_examples[] = {
+		"\t\t# read local port's performance counters",
+		"32 1\t\t# read performance counters from lid 32, port 1",
+		"-e 32 1\t# read extended performance counters from lid 32, port 1",
+		"-a 32\t\t# read performance counters from lid 32, all ports",
+		"-r 32 1\t# read performance counters and reset",
+		"-e -r 32 1\t# read extended performance counters and reset",
+		"-R 0x20 1\t# reset performance counters of port 1 only",
+		"-e -R 0x20 1\t# reset extended performance counters of port 1 only",
+		"-R -a 32\t# reset performance counters of all ports",
+		"-R 32 2 0x0fff\t# reset only error counters of port 2",
+		"-R 32 2 0xf000\t# reset only non-error counters of port 2",
+		NULL,
+	};
 
-	argv0 = argv[0];
+	ibdiag_process_opts(argc, argv, NULL, "De", opts, process_opt,
+			    usage_args, usage_examples);
 
-	while (1) {
-		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
-		if ( ch == -1 )
-			break;
-		switch(ch) {
-		case 'C':
-			ca = optarg;
-			break;
-		case 'P':
-			ca_port = strtoul(optarg, 0, 0);
-			break;
-		case 'e':
-			extended = 1;
-			break;
-		case 'a':
-			all_ports++;
-			port = ALL_PORTS;
-			break;
-		case 'l':
-			loop_ports++;
-			break;
-		case 'd':
-			ibdebug++;
-			madrpc_show_errors(1);
-			umad_debug(udebug);
-			udebug++;
-			break;
-		case 'G':
-			dest_type = IB_DEST_GUID;
-			break;
-		case 's':
-			if (ib_resolve_portid_str(&sm_portid, optarg, IB_DEST_LID, 0) < 0)
-				IBERROR("can't resolve SM destination port %s", optarg);
-			sm_id = &sm_portid;
-			break;
-		case 'r':
-			reset++;
-			break;
-		case 'R':
-			reset_only++;
-			break;
-		case 't':
-			timeout = strtoul(optarg, 0, 0);
-			madrpc_set_timeout(timeout);
-			break;
-		case 'V':
-			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
-			exit(-1);
-		default:
-			usage();
-			break;
-		}
-	}
+	argv0 = argv[0];
 	argc -= optind;
 	argv += optind;
 
@@ -444,10 +386,10 @@ main(int argc, char **argv)
 	if (argc > 2)
 		mask = strtoul(argv[2], 0, 0);
 
-	madrpc_init(ca, ca_port, mgmt_classes, 4);
+	madrpc_init(ibd_ca, ibd_ca_port, mgmt_classes, 4);
 
 	if (argc) {
-		if (ib_resolve_portid_str(&portid, argv[0], dest_type, sm_id) < 0)
+		if (ib_resolve_portid_str(&portid, argv[0], ibd_dest_type, ibd_sm_id) < 0)
 			IBERROR("can't resolve destination port %s", argv[0]);
 	} else {
 		if (ib_resolve_self(&portid, &port, 0) < 0)
@@ -455,7 +397,7 @@ main(int argc, char **argv)
 	}
 
 	/* PerfMgt ClassPortInfo is a required attribute */
-	if (!perf_classportinfo_query(pc, &portid, port, timeout))
+	if (!perf_classportinfo_query(pc, &portid, port, ibd_timeout))
 		IBERROR("classportinfo query");
 	/* ClassPortInfo should be supported as part of libibmad */
 	memcpy(&cap_mask, pc + 2, sizeof(cap_mask));	/* CapabilityMask */
@@ -491,7 +433,7 @@ main(int argc, char **argv)
 
 	if (all_ports_loop || (loop_ports && (all_ports || port == ALL_PORTS))) {
 		for (i = start_port; i <= num_ports; i++)
-			dump_perfcounters(extended, timeout, cap_mask, &portid, i,
+			dump_perfcounters(extended, ibd_timeout, cap_mask, &portid, i,
 					  (all_ports_loop && !loop_ports));
 		if (all_ports_loop && !loop_ports) {
 			if (extended != 1)
@@ -501,7 +443,7 @@ main(int argc, char **argv)
 		}
 	}
 	else
-		dump_perfcounters(extended, timeout, cap_mask, &portid, port, 0);
+		dump_perfcounters(extended, ibd_timeout, cap_mask, &portid, port, 0);
 
 	if (!reset)
 		exit(0);
@@ -513,10 +455,10 @@ do_reset:
 
 	if (all_ports_loop || (loop_ports && (all_ports || port == ALL_PORTS))) {
 		for (i = start_port; i <= num_ports; i++)
-			reset_counters(extended, timeout, mask, &portid, i);
+			reset_counters(extended, ibd_timeout, mask, &portid, i);
 	}
 	else
-		reset_counters(extended, timeout, mask, &portid, port);
+		reset_counters(extended, ibd_timeout, mask, &portid, port);
 
 	exit(0);
 }
diff --git a/infiniband-diags/src/saquery.c b/infiniband-diags/src/saquery.c
index 22d3186..c86d8b4 100644
--- a/infiniband-diags/src/saquery.c
+++ b/infiniband-diags/src/saquery.c
@@ -82,8 +82,6 @@ osmv_query_res_t result;
 osm_log_t log_osm;
 osm_mad_pool_t mad_pool;
 osm_vendor_t *vendor = NULL;
-int osm_debug = 0;
-uint32_t sa_timeout_ms = DEFAULT_SA_TIMEOUT_MS;
 char *sa_hca_name = NULL;
 uint32_t sa_port_num = 0;
 
@@ -691,7 +689,7 @@ get_any_records(osm_bind_handle_t h,
 	user.p_attr = attr;
 
 	req.query_type = OSMV_QUERY_USER_DEFINED;
-	req.timeout_ms = sa_timeout_ms;
+	req.timeout_ms = ibd_timeout;
 	req.retry_cnt = 1;
 	req.flags = OSM_SA_FLAGS_SYNC;
 	req.query_context = NULL;
@@ -888,7 +886,7 @@ get_print_path_rec_lid(osm_bind_handle_t h,
 	memset(&req, 0, sizeof(req));
 
 	req.query_type = OSMV_QUERY_PATH_REC_BY_LIDS;
-	req.timeout_ms = sa_timeout_ms;
+	req.timeout_ms = ibd_timeout;
 	req.retry_cnt = 1;
 	req.flags = OSM_SA_FLAGS_SYNC;
 	req.query_context = NULL;
@@ -926,7 +924,7 @@ get_print_path_rec_gid(osm_bind_handle_t h,
 	memset(&req, 0, sizeof(req));
 
 	req.query_type = OSMV_QUERY_PATH_REC_BY_GIDS;
-	req.timeout_ms = sa_timeout_ms;
+	req.timeout_ms = ibd_timeout;
 	req.retry_cnt = 1;
 	req.flags = OSM_SA_FLAGS_SYNC;
 	req.query_context = NULL;
@@ -958,7 +956,7 @@ static ib_api_status_t get_print_class_port_info(osm_bind_handle_t h)
 	memset(&req, 0, sizeof(req));
 
 	req.query_type = OSMV_QUERY_CLASS_PORT_INFO;
-	req.timeout_ms = sa_timeout_ms;
+	req.timeout_ms = ibd_timeout;
 	req.retry_cnt = 1;
 	req.flags = OSM_SA_FLAGS_SYNC;
 	req.query_context = NULL;
@@ -1401,10 +1399,10 @@ static osm_bind_handle_t get_bind_handle(void)
 		exit(-1);
 	}
 	osm_log_set_level(&log_osm, OSM_LOG_NONE);
-	if (osm_debug)
+	if (ibdebug)
 		osm_log_set_level(&log_osm, OSM_LOG_DEFAULT_LEVEL);
 
-	vendor = osm_vendor_new(&log_osm, sa_timeout_ms);
+	vendor = osm_vendor_new(&log_osm, ibd_timeout);
 	osm_mad_pool_construct(&mad_pool);
 	if ((status = osm_mad_pool_init(&mad_pool)) != IB_SUCCESS) {
 		fprintf(stderr, "Failed to init mad pool: %s\n",
@@ -1511,60 +1509,6 @@ static const struct query_cmd *find_query_by_type(ib_net16_t type)
 	return NULL;
 }
 
-static void usage(void)
-{
-	const struct query_cmd *q;
-
-	fprintf(stderr, "Usage: %s [-h -d -p -N] [--list | -D] [-S -I -L -l -G"
-		" -O -U -c -s -g -m --src-to-dst <src:dst> --sgid-to-dgid <src-dst> "
-		"-C <ca_name> -P <ca_port> -t(imeout) <msec>] [query-name] [<name> | <lid> | <guid>]\n",
-		argv0);
-	fprintf(stderr, "   Queries node records by default\n");
-	fprintf(stderr, "   -d enable debugging\n");
-	fprintf(stderr, "   -p get PathRecord info\n");
-	fprintf(stderr, "   -N get NodeRecord info\n");
-	fprintf(stderr, "   --list | -D the node desc of the CA's\n");
-	fprintf(stderr, "   -S get ServiceRecord info\n");
-	fprintf(stderr, "   -I get InformInfoRecord (subscription) info\n");
-	fprintf(stderr, "   -L return the Lids of the name specified\n");
-	fprintf(stderr, "   -l return the unique Lid of the name specified\n");
-	fprintf(stderr, "   -G return the Guids of the name specified\n");
-	fprintf(stderr, "   -O return name for the Lid specified\n");
-	fprintf(stderr, "   -U return name for the Guid specified\n");
-	fprintf(stderr, "   -c get the SA's class port info\n");
-	fprintf(stderr, "   -s return the PortInfoRecords with isSM or "
-		"isSMdisabled capability mask bit on\n");
-	fprintf(stderr, "   -g get multicast group info\n");
-	fprintf(stderr, "   -m get multicast member info\n");
-	fprintf(stderr, "      (if multicast group specified, list member GIDs"
-		" only for group specified\n");
-	fprintf(stderr, "      specified, for example 'saquery -m 0xC000')\n");
-	fprintf(stderr, "   -x get LinkRecord info\n");
-	fprintf(stderr, "   --src-to-dst get a PathRecord for <src:dst>\n"
-		"                where src and dst are either node "
-		"names or LIDs\n");
-	fprintf(stderr, "   --sgid-to-dgid get a PathRecord for <sgid-dgid>\n"
-		"                where sgid and dgid are addresses in "
-		"IPv6 format\n");
-	fprintf(stderr, "   -C <ca_name> specify the SA query HCA\n");
-	fprintf(stderr, "   -P <ca_port> specify the SA query port\n");
-	fprintf(stderr, "   --smkey <val> specify SM_Key value for the query."
-		" If non-numeric value \n"
-		"                 (like 'x') is specified then "
-		"saquery will prompt for a value\n");
-	fprintf(stderr, "   -t | --timeout <msec> specify the SA query "
-		"response timeout (default %u msec)\n", DEFAULT_SA_TIMEOUT_MS);
-	fprintf(stderr,
-		"   --node-name-map <node-name-map> specify a node name map\n");
-	fprintf(stderr, "\n   Supported query names (and aliases):\n");
-	for (q = query_cmds; q->name; q++)
-		fprintf(stderr, "      %s (%s) %s\n", q->name,
-			q->alias ? q->alias : "", q->usage ? q->usage : "");
-	fprintf(stderr, "\n");
-
-	exit(-1);
-}
-
 enum saquery_command {
 	SAQUERY_CMD_QUERY,
 	SAQUERY_CMD_NODE_RECORD,
@@ -1575,164 +1519,170 @@ enum saquery_command {
 	SAQUERY_CMD_MCMEMBERS,
 };
 
+static enum saquery_command command = SAQUERY_CMD_QUERY;
+static ib_net16_t query_type;
+static char *src, *dst, *sgid, *dgid;
+
+static int process_opt(void *context, int ch, char *optarg)
+{
+	switch (ch) {
+	case 1:
+		{
+			char *opt = strdup(optarg);
+			char *ch = strchr(opt, ':');
+			if (!ch) {
+				fprintf(stderr,
+					"ERROR: --src-to-dst <node>:<node>\n");
+				ibdiag_show_usage();
+			}
+			*ch++ = '\0';
+			if (*opt)
+				src = strdup(opt);
+			if (*ch)
+				dst = strdup(ch);
+			free(opt);
+			command = SAQUERY_CMD_PATH_RECORD;
+			break;
+		}
+	case 2:
+		{
+			char *opt = strdup(optarg);
+			char *tok1 = strtok(opt, "-");
+			char *tok2 = strtok(NULL, "\0");
+
+			if (tok1 && tok2) {
+				sgid = strdup(tok1);
+				dgid = strdup(tok2);
+			} else {
+				fprintf(stderr,
+					"ERROR: --sgid-to-dgid <GID>-<GID>\n");
+				ibdiag_show_usage();
+			}
+			free(opt);
+			command = SAQUERY_CMD_PATH_RECORD;
+			break;
+		}
+	case 3:
+		node_name_map_file = strdup(optarg);
+		break;
+	case 4:
+		if (!isxdigit(*optarg) &&
+		    !(optarg = getpass("SM_Key: "))) {
+			fprintf(stderr, "cannot get SM_Key\n");
+			ibdiag_show_usage();
+		}
+		smkey = cl_hton64(strtoull(optarg, NULL, 0));
+		break;
+	case 'p':
+		command = SAQUERY_CMD_PATH_RECORD;
+		break;
+	case 'D':
+		node_print_desc = ALL_DESC;
+		break;
+	case 'c':
+		command = SAQUERY_CMD_CLASS_PORT_INFO;
+		break;
+	case 'S':
+		query_type = IB_MAD_ATTR_SERVICE_RECORD;
+		break;
+	case 'I':
+		query_type = IB_MAD_ATTR_INFORM_INFO_RECORD;
+		break;
+	case 'N':
+		command = SAQUERY_CMD_NODE_RECORD;
+		break;
+	case 'L':
+		node_print_desc = LID_ONLY;
+		break;
+	case 'l':
+		node_print_desc = UNIQUE_LID_ONLY;
+		break;
+	case 'G':
+		node_print_desc = GUID_ONLY;
+		break;
+	case 'O':
+		node_print_desc = NAME_OF_LID;
+		break;
+	case 'U':
+		node_print_desc = NAME_OF_GUID;
+		break;
+	case 's':
+		command = SAQUERY_CMD_ISSM;
+		break;
+	case 'g':
+		command = SAQUERY_CMD_MCGROUPS;
+		break;
+	case 'm':
+		command = SAQUERY_CMD_MCMEMBERS;
+		break;
+	case 'x':
+		query_type = IB_MAD_ATTR_LINK_RECORD;
+		break;
+	default:
+		return -1;
+	}
+	return 0;
+}
+
 int main(int argc, char **argv)
 {
-	int ch = 0;
+	char usage_args[1024];
 	osm_bind_handle_t h;
-	enum saquery_command command = SAQUERY_CMD_QUERY;
-	const struct query_cmd *q = NULL;
-	char *src = NULL, *dst = NULL;
-	char *sgid = NULL, *dgid = NULL;
-	ib_net16_t query_type = 0;
+	const struct query_cmd *q;
 	ib_net16_t src_lid, dst_lid;
 	ib_api_status_t status;
-
-	static char const str_opts[] = "pVNDLlGOUcSIsgmxdhP:C:t:";
-	static const struct option long_opts[] = {
-		{"p", 0, 0, 'p'},
-		{"Version", 0, 0, 'V'},
-		{"N", 0, 0, 'N'},
-		{"L", 0, 0, 'L'},
-		{"l", 0, 0, 'l'},
-		{"G", 0, 0, 'G'},
-		{"O", 0, 0, 'O'},
-		{"U", 0, 0, 'U'},
-		{"s", 0, 0, 's'},
-		{"g", 0, 0, 'g'},
-		{"m", 0, 0, 'm'},
-		{"x", 0, 0, 'x'},
-		{"d", 0, 0, 'd'},
-		{"c", 0, 0, 'c'},
-		{"S", 0, 0, 'S'},
-		{"I", 0, 0, 'I'},
-		{"P", 1, 0, 'P'},
-		{"C", 1, 0, 'C'},
-		{"help", 0, 0, 'h'},
-		{"list", 0, 0, 'D'},
-		{"src-to-dst", 1, 0, 1},
-		{"sgid-to-dgid", 1, 0, 2},
-		{"timeout", 1, 0, 't'},
-		{"node-name-map", 1, 0, 3},
-		{"smkey", 1, 0, 4},
+	int n;
+
+	const struct ibdiag_opt opts[] = {
+		{"p", 'p', 0, NULL, "get PathRecord info"},
+		{"N", 'N', 0, NULL, "get NodeRecord info"},
+		{"L", 'L', 0, NULL, "return the Lids of the name specified"},
+		{"l", 'l', 0, NULL, "return the unique Lid of the name specified"},
+		{"G", 'G', 0, NULL, "return the Guids of the name specified"},
+		{"O", 'O', 0, NULL, "return name for the Lid specified"},
+		{"U", 'U', 0, NULL, "return name for the Guid specified"},
+		{"s", 's', 0, NULL, "return the PortInfoRecords with isSM or"
+		 " isSMdisabled capability mask bit on"},
+		{"g", 'g', 0, NULL, "get multicast group info"},
+		{"m", 'm', 0, NULL, "get multicast member info (if multicast"
+		 " group specified, list member GIDs only for group specified,"
+		 " for example 'saquery -m 0xC000')"},
+		{"x", 'x', 0, NULL, "get LinkRecord info"},
+		{"c", 'c', 0, NULL, "get the SA's class port info"},
+		{"S", 'S', 0, NULL, "get ServiceRecord info"},
+		{"I", 'I', 0, NULL, "get InformInfoRecord (subscription) info"},
+		{"list", 'D', 0, NULL, "the node desc of the CA's"},
+		{"src-to-dst", 1, 1, "<src:dst>", "get a PathRecord for"
+		 " <src:dst> where src and dst are either node names or LIDs"},
+		{"sgid-to-dgid", 2, 1, "<sgid-dgid>", "get a PathRecord for"
+		 " <sgid-dgid> where sgid and dgid are addresses in IPv6 format"},
+		{"node-name-map", 3, 1, "<file>", "specify a node name map file"},
+		{"smkey", 4, 1, "<val>", "SA SM_Key value for the query."
+		 " If non-numeric value (like 'x') is specified then"
+		 " saquery will prompt for a value"},
 		{}
 	};
 
-	argv0 = argv[0];
-
-	while ((ch = getopt_long(argc, argv, str_opts, long_opts, NULL)) != -1) {
-		switch (ch) {
-		case 1:
-			{
-				char *opt = strdup(optarg);
-				char *ch = strchr(opt, ':');
-				if (!ch) {
-					fprintf(stderr,
-						"ERROR: --src-to-dst <node>:<node>\n");
-					usage();
-				}
-				*ch++ = '\0';
-				if (*opt)
-					src = strdup(opt);
-				if (*ch)
-					dst = strdup(ch);
-				free(opt);
-				command = SAQUERY_CMD_PATH_RECORD;
-				break;
-			}
-		case 2:
-			{
-				char *opt = strdup(optarg);
-				char *tok1 = strtok(opt, "-");
-				char *tok2 = strtok(NULL, "\0");
-
-				if (tok1 && tok2) {
-					sgid = strdup(tok1);
-					dgid = strdup(tok2);
-				} else {
-					fprintf(stderr,
-						"ERROR: --sgid-to-dgid <GID>-<GID>\n");
-					usage();
-				}
-				free(opt);
-				command = SAQUERY_CMD_PATH_RECORD;
-				break;
-			}
-		case 3:
-			node_name_map_file = strdup(optarg);
-			break;
-		case 4:
-			if (!isxdigit(*optarg) &&
-			    !(optarg = getpass("SM_Key: "))) {
-				fprintf(stderr, "cannot get SM_Key\n");
-				usage();
-			}
-			smkey = cl_hton64(strtoull(optarg, NULL, 0));
-			break;
-		case 'p':
-			command = SAQUERY_CMD_PATH_RECORD;
-			break;
-		case 'V':
-			fprintf(stderr, "%s %s\n", argv0, get_build_version());
+	n = sprintf(usage_args, "[query-name] [<name> | <lid> | <guid>]\n"
+		    "\nSupported query names (and aliases):\n");
+	for (q = query_cmds; q->name; q++) {
+		n += snprintf(usage_args + n, sizeof(usage_args) - n,
+			      "  %s (%s) %s\n", q->name,
+			      q->alias ? q->alias : "",
+			      q->usage ? q->usage : "");
+		if (n >= sizeof(usage_args))
 			exit(-1);
-		case 'D':
-			node_print_desc = ALL_DESC;
-			break;
-		case 'c':
-			command = SAQUERY_CMD_CLASS_PORT_INFO;
-			break;
-		case 'S':
-			query_type = IB_MAD_ATTR_SERVICE_RECORD;
-			break;
-		case 'I':
-			query_type = IB_MAD_ATTR_INFORM_INFO_RECORD;
-			break;
-		case 'N':
-			command = SAQUERY_CMD_NODE_RECORD;
-			break;
-		case 'L':
-			node_print_desc = LID_ONLY;
-			break;
-		case 'l':
-			node_print_desc = UNIQUE_LID_ONLY;
-			break;
-		case 'G':
-			node_print_desc = GUID_ONLY;
-			break;
-		case 'O':
-			node_print_desc = NAME_OF_LID;
-			break;
-		case 'U':
-			node_print_desc = NAME_OF_GUID;
-			break;
-		case 's':
-			command = SAQUERY_CMD_ISSM;
-			break;
-		case 'g':
-			command = SAQUERY_CMD_MCGROUPS;
-			break;
-		case 'm':
-			command = SAQUERY_CMD_MCMEMBERS;
-			break;
-		case 'x':
-			query_type = IB_MAD_ATTR_LINK_RECORD;
-			break;
-		case 'd':
-			osm_debug = 1;
-			break;
-		case 'C':
-			sa_hca_name = optarg;
-			break;
-		case 'P':
-			sa_port_num = strtoul(optarg, NULL, 0);
-			break;
-		case 't':
-			sa_timeout_ms = strtoul(optarg, NULL, 0);
-			break;
-		case 'h':
-		default:
-			usage();
-		}
 	}
+	snprintf(usage_args + n, sizeof(usage_args) - n,
+		 "\n  Queries node records by default.");
+
+	q = NULL;
+	ibd_timeout = DEFAULT_SA_TIMEOUT_MS;
+
+	ibdiag_process_opts(argc, argv, NULL, "DLGs", opts, process_opt,
+			    usage_args, NULL);
+
+	argv0 = argv[0];
 	argc -= optind;
 	argv += optind;
 
@@ -1762,23 +1712,23 @@ int main(int argc, char **argv)
 	     node_print_desc == UNIQUE_LID_ONLY ||
 	     node_print_desc == GUID_ONLY) && !requested_name) {
 		fprintf(stderr, "ERROR: name not specified\n");
-		usage();
+		ibdiag_show_usage();
 	}
 
 	if (node_print_desc == NAME_OF_LID && !requested_lid_flag) {
 		fprintf(stderr, "ERROR: lid not specified\n");
-		usage();
+		ibdiag_show_usage();
 	}
 
 	if (node_print_desc == NAME_OF_GUID && !requested_guid_flag) {
 		fprintf(stderr, "ERROR: guid not specified\n");
-		usage();
+		ibdiag_show_usage();
 	}
 
 	/* Note: lid cannot be 0; see infiniband spec 4.1.3 */
 	if (node_print_desc == NAME_OF_LID && !requested_lid) {
 		fprintf(stderr, "ERROR: lid invalid\n");
-		usage();
+		ibdiag_show_usage();
 	}
 
 	h = get_bind_handle();
diff --git a/infiniband-diags/src/sminfo.c b/infiniband-diags/src/sminfo.c
index f7d09db..4cb7a6b 100644
--- a/infiniband-diags/src/sminfo.c
+++ b/infiniband-diags/src/sminfo.c
@@ -51,15 +51,6 @@ static uint8_t sminfo[1024];
 
 char *argv0 = "sminfo";
 
-static void
-usage(void)
-{
-	fprintf(stderr, "Usage: %s [-d(ebug) -e(rr_show) -s state -p prio -a activity -D(irect) -G(uid) -V(ersion) -C ca_name -P ca_port "
-			"-t(imeout) timeout_ms] <sm_lid|sm_dr_path> [modifier]\n",
-			argv0);
-	exit(-1);
-}
-
 int strdata, xdata=1, bindata;
 enum {
 	SMINFO_NOTACT,
@@ -79,102 +70,60 @@ char *statestr[] = {
 
 #define STATESTR(s)	(((unsigned)(s)) < SMINFO_STATE_LAST ? statestr[s] : "???")
 
-int
-main(int argc, char **argv)
+static unsigned act;
+static int prio, state = SMINFO_STANDBY;
+
+static int process_opt(void *context, int ch, char *optarg)
+{
+	switch (ch) {
+	case 'a':
+		act = strtoul(optarg, 0, 0);
+		break;
+	case 's':
+		state = strtoul(optarg, 0, 0);
+		break;
+	case 'p':
+		prio = strtoul(optarg, 0, 0);
+		break;
+	default:
+		return -1;
+	}
+	return 0;
+}
+
+int main(int argc, char **argv)
 {
 	int mgmt_classes[3] = {IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS};
 	int mod = 0;
 	ib_portid_t portid = {0};
-	int timeout = 0;	/* use default */
 	uint8_t *p;
-	unsigned act = 0;
-	int prio = 0, state = SMINFO_STANDBY;
 	uint64_t guid = 0, key = 0;
-	int dest_type = IB_DEST_LID;
-	int udebug = 0;
-	char *ca = 0;
-	int ca_port = 0;
-
-	static char const str_opts[] = "C:P:t:s:p:a:deDGVhu";
-	static const struct option long_opts[] = {
-		{ "C", 1, 0, 'C'},
-		{ "P", 1, 0, 'P'},
-		{ "debug", 0, 0, 'd'},
-		{ "err_show", 0, 0, 'e'},
-		{ "s", 1, 0, 's'},
-		{ "p", 1, 0, 'p'},
-		{ "a", 1, 0, 'a'},
-		{ "Direct", 0, 0, 'D'},
-		{ "Guid", 0, 0, 'G'},
-		{ "Version", 0, 0, 'V'},
-		{ "timeout", 1, 0, 't'},
-		{ "help", 0, 0, 'h'},
-		{ "usage", 0, 0, 'u'},
+
+	const struct ibdiag_opt opts[] = {
+		{ "state", 's', 1, "<0-3>", "set SM state"},
+		{ "priority", 'p', 1, "<0-15>", "set SM priority"},
+		{ "activity", 'a', 1, NULL, "set activity count"},
 		{ }
 	};
+	char usage_args[] = "<sm_lid|sm_dr_path> [modifier]";
 
-	argv0 = argv[0];
+	ibdiag_process_opts(argc, argv, NULL, "s", opts, process_opt,
+			    usage_args, NULL);
 
-	while (1) {
-		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
-		if ( ch == -1 )
-			break;
-		switch(ch) {
-		case 'C':
-			ca = optarg;
-			break;
-		case 'P':
-			ca_port = strtoul(optarg, 0, 0);
-			break;
-		case 'd':
-			ibdebug++;
-			madrpc_show_errors(1);
-			umad_debug(udebug);
-			udebug++;
-			break;
-		case 'e':
-			madrpc_show_errors(1);
-			break;
-		case 'D':
-			dest_type = IB_DEST_DRPATH;
-			break;
-		case 'G':
-			dest_type = IB_DEST_GUID;
-			break;
-		case 't':
-			timeout = strtoul(optarg, 0, 0);
-			madrpc_set_timeout(timeout);
-			break;
-		case 'a':
-			act = strtoul(optarg, 0, 0);
-			break;
-		case 's':
-			state = strtoul(optarg, 0, 0);
-			break;
-		case 'p':
-			prio = strtoul(optarg, 0, 0);
-			break;
-		case 'V':
-			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
-			exit(-1);
-		default:
-			usage();
-			break;
-		}
-	}
+	argv0 = argv[0];
 	argc -= optind;
 	argv += optind;
 
 	if (argc > 1)
 		mod = atoi(argv[1]);
 
-	madrpc_init(ca, ca_port, mgmt_classes, 3);
+	madrpc_init(ibd_ca, ibd_ca_port, mgmt_classes, 3);
 
 	if (argc) {
-		if (ib_resolve_portid_str(&portid, argv[0], dest_type, 0) < 0)
+		if (ib_resolve_portid_str(&portid, argv[0], ibd_dest_type, 0) < 0)
 			IBERROR("can't resolve destination port %s", argv[0]);
 	} else {
-		if (ib_resolve_smlid(&portid, timeout) < 0)
+		if (ib_resolve_smlid(&portid, ibd_timeout) < 0)
 			IBERROR("can't resolve sm port %s", argv[0]);
 	}
 
@@ -185,10 +134,10 @@ main(int argc, char **argv)
 	mad_encode_field(sminfo, IB_SMINFO_STATE_F, &state);
 
 	if (mod) {
-		if (!(p = smp_set(sminfo, &portid, IB_ATTR_SMINFO, mod, timeout)))
+		if (!(p = smp_set(sminfo, &portid, IB_ATTR_SMINFO, mod, ibd_timeout)))
 			IBERROR("query");
 	} else
-		if (!(p = smp_query(sminfo, &portid, IB_ATTR_SMINFO, 0, timeout)))
+		if (!(p = smp_query(sminfo, &portid, IB_ATTR_SMINFO, 0, ibd_timeout)))
 			IBERROR("query");
 
 	mad_decode_field(sminfo, IB_SMINFO_GUID_F, &guid);
diff --git a/infiniband-diags/src/smpdump.c b/infiniband-diags/src/smpdump.c
index a1a83c4..6299dba 100644
--- a/infiniband-diags/src/smpdump.c
+++ b/infiniband-diags/src/smpdump.c
@@ -53,8 +53,6 @@
 static int mad_agent;
 static int drmad_tid = 0x123;
 
-static int debug, verbose;
-
 char *argv0 = "smpdump";
 
 typedef struct {
@@ -193,26 +191,29 @@ str2DRPath(char *str, DRPath *path)
 	return path->hop_cnt;
 }
 
-void
-usage(void)
+static int dump_char, mgmt_class = IB_SMI_CLASS;
+
+static int process_opt(void *context, int ch, char *optarg)
 {
-	fprintf(stderr, "Usage: %s [-s(ring) -D(irect) -V(ersion) -C ca_name -P ca_port -t(imeout) timeout_ms] <dlid|dr_path> <attr> [mod]\n", argv0);
-	fprintf(stderr, "\tDR examples:\n");
-	fprintf(stderr, "\t\t%s -D 0,1,2,3,5 16	# NODE DESC\n", argv0);
-	fprintf(stderr, "\t\t%s -D 0,1,2 0x15 2	# PORT INFO, port 2\n", argv0);
-	fprintf(stderr, "\n\tLID routed examples:\n");
-	fprintf(stderr, "\t\t%s 3 0x15 2	# PORT INFO, lid 3 port 2\n", argv0);
-	fprintf(stderr, "\t\t%s 0xa0 0x11	# NODE INFO, lid 0xa0\n", argv0);
-	fprintf(stderr, "\n");
-	exit(-1);
+	switch (ch) {
+	case 's':
+		dump_char++;
+		break;
+	case 'D':
+		mgmt_class = IB_SMI_DIRECT_CLASS;
+		break;
+	case 'L':
+		mgmt_class = IB_SMI_CLASS;
+		break;
+	default:
+		return -1;
+	}
+	return 0;
 }
 
-int
-main(int argc, char *argv[])
+int main(int argc, char *argv[])
 {
-	int dump_char = 0, timeout_ms = 1000;
-	int dev_port = 0, mgmt_class = IB_SMI_CLASS, dlid = 0;
-	char *dev_name = 0;
+	int dlid = 0;
 	void *umad;
 	struct drsmp *smp;
 	int i, portid, mod = 0, attr;
@@ -220,60 +221,32 @@ main(int argc, char *argv[])
 	uint8_t *desc;
 	int length;
 
-	static char const str_opts[] = "C:P:t:dsDVhu";
-	static const struct option long_opts[] = {
-		{ "C", 1, 0, 'C'},
-		{ "P", 1, 0, 'P'},
-		{ "debug", 0, 0, 'd'},
-		{ "sring", 0, 0, 's'},
-		{ "Direct", 0, 0, 'D'},
-		{ "timeout", 1, 0, 't'},
-		{ "Version", 0, 0, 'V'},
-		{ "help", 0, 0, 'h'},
-		{ "usage", 0, 0, 'u'},
+	const struct ibdiag_opt opts[] = {
+		{ "sring", 's', 0, NULL, ""},
 		{ }
 	};
+	char usage_args[] = "<dlid|dr_path> <attr> [mod]";
+	const char *usage_examples[] = {
+		" -- DR routed examples:",
+		"%s -D 0,1,2,3,5 16	# NODE DESC",
+		"%s -D 0,1,2 0x15 2	# PORT INFO, port 2",
+		" -- LID routed examples:",
+		"%s 3 0x15 2	# PORT INFO, lid 3 port 2",
+		"%s 0xa0 0x11	# NODE INFO, lid 0xa0",
+		NULL
+	};
 
-	argv0 = argv[0];
+	ibd_timeout = 1000;
 
-	while (1) {
-		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
-		if ( ch == -1 )
-			break;
-		switch(ch) {
-		case 's':
-			dump_char++;
-			break;
-		case 'd':
-			debug++;
-			if (debug > 1)
-				umad_debug(debug-1);
-			break;
-		case 'D':
-			mgmt_class = IB_SMI_DIRECT_CLASS;
-			break;
-		case 'C':
-			dev_name = optarg;
-			break;
-		case 'P':
-			dev_port = atoi(optarg);
-			break;
-		case 't':
-			timeout_ms = strtoul(optarg, 0, 0);
-			break;
-		case 'V':
-			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
-			exit(-1);
-		default:
-			usage();
-			break;
-		}
-	}
+	ibdiag_process_opts(argc, argv, NULL, "Gs", opts, process_opt,
+			    usage_args, usage_examples);
+
+	argv0 = argv[0];
 	argc -= optind;
 	argv += optind;
 
 	if (argc < 2)
-		usage();
+		ibdiag_show_usage();
 
 	if (mgmt_class == IB_SMI_DIRECT_CLASS &&
 	    str2DRPath(strdupa(argv[0]), &path) < 0)
@@ -289,8 +262,8 @@ main(int argc, char *argv[])
 	if (umad_init() < 0)
 		IBPANIC("can't init UMAD library");
 
-	if ((portid = umad_open_port(dev_name, dev_port)) < 0)
-		IBPANIC("can't open UMAD port (%s:%d)", dev_name, dev_port);
+	if ((portid = umad_open_port(ibd_ca, ibd_ca_port)) < 0)
+		IBPANIC("can't open UMAD port (%s:%d)", ibd_ca, ibd_ca_port);
 
 	if ((mad_agent = umad_register(portid, mgmt_class, 1, 0, 0)) < 0)
 		IBPANIC("Couldn't register agent for SMPs");
@@ -305,11 +278,11 @@ main(int argc, char *argv[])
 	else
 		smp_get_init(umad, dlid, attr, mod);
 
-	if (debug > 1)
+	if (ibdebug > 1)
 		xdump(stderr, "before send:\n", smp, 256);
 
 	length = IB_MAD_SIZE;
-	if (umad_send(portid, mad_agent, umad, length, timeout_ms, 0) < 0)
+	if (umad_send(portid, mad_agent, umad, length, ibd_timeout, 0) < 0)
 		IBPANIC("send failed");
 
 	if (umad_recv(portid, umad, &length, -1) != mad_agent)
diff --git a/infiniband-diags/src/smpquery.c b/infiniband-diags/src/smpquery.c
index 7a7dddf..c5916b8 100644
--- a/infiniband-diags/src/smpquery.c
+++ b/infiniband-diags/src/smpquery.c
@@ -53,12 +53,6 @@
 
 #include "ibdiag_common.h"
 
-#undef DEBUG
-#define	DEBUG	if (verbose>1) IBWARN
-
-static int dest_type = IB_DEST_LID;
-static int verbose;
-
 typedef char *(op_fn_t)(ib_portid_t *dest, char **argv, int argc);
 
 typedef struct match_rec {
@@ -392,132 +386,72 @@ match_op(char *name)
 	return 0;
 }
 
-static void
-usage(void)
+static int process_opt(void *context, int ch, char *optarg)
 {
-	char *basename;
-	const match_rec_t *r;
-
-	if (!(basename = strrchr(argv0, '/')))
-		basename = argv0;
-	else
-		basename++;
-
-	fprintf(stderr, "Usage: %s [-d(ebug) -e(rr_show) -v(erbose) -D(irect) -G(uid) -s smlid -V(ersion) -C ca_name -P ca_port "
-			"-t(imeout) timeout_ms --node-name-map node-name-map] <op> <dest dr_path|lid|guid> [op params]\n",
-			basename);
-	fprintf(stderr, "\tsupported ops:\n");
-	for (r = match_tbl ; r->name ; r++) {
-		fprintf(stderr, "\t\t%s <addr>%s\n", r->name,
-				r->opt_portnum ? " [<portnum>]" : "");
+	switch (ch) {
+	case 1:
+		node_name_map_file = strdup(optarg);
+		break;
+	case 'c':
+		ibd_dest_type = IB_DEST_DRSLID;
+		break;
+	default:
+		return -1;
 	}
-	fprintf(stderr, "\n\texamples:\n");
-	fprintf(stderr, "\t\t%s portinfo 3 1\t\t\t\t# portinfo by lid, with port modifier\n", basename);
-	fprintf(stderr, "\t\t%s -G switchinfo 0x2C9000100D051 1\t# switchinfo by guid\n", basename);
-	fprintf(stderr, "\t\t%s -D nodeinfo 0\t\t\t\t# nodeinfo by direct route\n", basename);
-	fprintf(stderr, "\t\t%s -c nodeinfo 6 0,12\t\t\t# nodeinfo by combined route\n", basename);
-	exit(-1);
+	return 0;
 }
 
-int
-main(int argc, char **argv)
+int main(int argc, char **argv)
 {
+	char usage_args[1024];
 	int mgmt_classes[3] = {IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS};
 	ib_portid_t portid = {0};
-	ib_portid_t *sm_id = 0, sm_portid = {0};
-	int timeout = 0, udebug = 0;
-	char *ca = 0;
-	int ca_port = 0;
 	char *err;
 	op_fn_t *fn;
+	const match_rec_t *r;
+	int n;
 
-	static char const str_opts[] = "C:P:t:s:devDcGVhu";
-	static const struct option long_opts[] = {
-		{ "C", 1, 0, 'C'},
-		{ "P", 1, 0, 'P'},
-		{ "debug", 0, 0, 'd'},
-		{ "err_show", 0, 0, 'e'},
-		{ "verbose", 0, 0, 'v'},
-		{ "Direct", 0, 0, 'D'},
-		{ "combined", 0, 0, 'c'},
-		{ "Guid", 0, 0, 'G'},
-		{ "smlid", 1, 0, 's'},
-		{ "timeout", 1, 0, 't'},
-		{ "node-name-map", 1, 0, 1},
-		{ "Version", 0, 0, 'V'},
-		{ "help", 0, 0, 'h'},
-		{ "usage", 0, 0, 'u'},
-		{ }
+	const struct ibdiag_opt opts[] = {
+		{ "combined", 'c', 0, NULL, "use Combined route address argument"},
+		{ "node-name-map", 1, 1, "<file>", "node name map file"},
+		{}
+	};
+	const char *usage_examples[] = {
+		"portinfo 3 1\t\t\t\t# portinfo by lid, with port modifier",
+		"-G switchinfo 0x2C9000100D051 1\t# switchinfo by guid",
+		"-D nodeinfo 0\t\t\t\t# nodeinfo by direct route",
+		"-c nodeinfo 6 0,12\t\t\t# nodeinfo by combined route",
+		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 'd':
-			ibdebug++;
-			madrpc_show_errors(1);
-			umad_debug(udebug);
-			udebug++;
-			break;
-		case 'e':
-			madrpc_show_errors(1);
-			break;
-		case 'D':
-			dest_type = IB_DEST_DRPATH;
-			break;
-		case 'c':
-			dest_type = IB_DEST_DRSLID;
-			break;
-		case 'G':
-			dest_type = IB_DEST_GUID;
-			break;
-		case 'C':
-			ca = optarg;
-			break;
-		case 'P':
-			ca_port = strtoul(optarg, 0, 0);
-			break;
-		case 's':
-			if (ib_resolve_portid_str(&sm_portid, optarg, IB_DEST_LID, 0) < 0)
-				IBERROR("can't resolve SM destination port %s", optarg);
-			sm_id = &sm_portid;
-			break;
-		case 't':
-			timeout = strtoul(optarg, 0, 0);
-			madrpc_set_timeout(timeout);
-			break;
-		case 'v':
-			verbose++;
-			break;
-		case 'V':
-			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
+	n = sprintf(usage_args, "<op> <dest dr_path|lid|guid> [op params]\n"
+		    "\nSupported ops:\n");
+	for (r = match_tbl ; r->name ; r++) {
+		n += snprintf(usage_args + n, sizeof(usage_args) - n,
+			      "  %s <addr>%s\n", r->name,
+			      r->opt_portnum ? " [<portnum>]" : "");
+		if (n >= sizeof(usage_args))
 			exit(-1);
-		default:
-			usage();
-			break;
-		}
 	}
+
+	ibdiag_process_opts(argc, argv, NULL, NULL, opts, process_opt,
+			    usage_args, usage_examples);
+
+	argv0 = argv[0];
 	argc -= optind;
 	argv += optind;
 
 	if (argc < 2)
-		usage();
+		ibdiag_show_usage();
 
 	if (!(fn = match_op(argv[0])))
 		IBERROR("operation '%s' not supported", argv[0]);
 
-	madrpc_init(ca, ca_port, mgmt_classes, 3);
+	madrpc_init(ibd_ca, ibd_ca_port, mgmt_classes, 3);
 	node_name_map = open_node_name_map(node_name_map_file);
 
-	if (dest_type != IB_DEST_DRSLID) {
-		if (ib_resolve_portid_str(&portid, argv[1], dest_type, sm_id) < 0)
+	if (ibd_dest_type != IB_DEST_DRSLID) {
+		if (ib_resolve_portid_str(&portid, argv[1], ibd_dest_type, ibd_sm_id) < 0)
 			IBERROR("can't resolve destination port %s", argv[1]);
 		if ((err = fn(&portid, argv+2, argc-2)))
 			IBERROR("operation %s: %s", argv[0], err);
@@ -526,7 +460,7 @@ main(int argc, char **argv)
 
 		memset(concat, 0, 64);
 		snprintf(concat, sizeof(concat), "%s %s", argv[1], argv[2]);
-		if (ib_resolve_portid_str(&portid, concat, dest_type, sm_id) < 0)
+		if (ib_resolve_portid_str(&portid, concat, ibd_dest_type, ibd_sm_id) < 0)
 			IBERROR("can't resolve destination port %s", concat);
 		if ((err = fn(&portid, argv+3, argc-3)))
 			IBERROR("operation %s: %s", argv[0], err);
diff --git a/infiniband-diags/src/vendstat.c b/infiniband-diags/src/vendstat.c
index 61f9501..93b32c0 100644
--- a/infiniband-diags/src/vendstat.c
+++ b/infiniband-diags/src/vendstat.c
@@ -106,116 +106,60 @@ typedef struct {
 	is3_record_t record[18];
 } is3_config_space_t;
 
-static void
-usage(void)
+static int general_info, xmit_wait = 0;
+
+static int process_opt(void *context, int ch, char *optarg)
 {
-	char *basename;
-
-	if (!(basename = strrchr(argv0, '/')))
-		basename = argv0;
-	else
-		basename++;
-
-	fprintf(stderr, "Usage: %s [-d(ebug) -N -w -G(uid) -C ca_name -P ca_port "
-			"-t(imeout) timeout_ms -V(ersion) -h(elp)] <lid|guid>\n",
-			basename);
-	fprintf(stderr, "\tExamples:\n");
-	fprintf(stderr, "\t\t%s -N 6\t\t# read IS3 general information\n", basename);
-	fprintf(stderr, "\t\t%s -w 6\t\t# read IS3 port xmit wait counters\n", basename);
-	exit(-1);
+	switch (ch) {
+	case 'N':
+		general_info = 1;
+		break;
+	case 'w':
+		xmit_wait = 1;
+		break;
+	default:
+		return -1;
+	}
+	return 0;
 }
 
-int
-main(int argc, char **argv)
+int main(int argc, char **argv)
 {
 	int mgmt_classes[4] = {IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS, IB_MLX_VENDOR_CLASS};
-	ib_portid_t *sm_id = 0, sm_portid = {0};
 	ib_portid_t portid = {0};
-	int dest_type = IB_DEST_LID;
-	int timeout = 0;	/* use default */
 	int port = 0;
 	char buf[1024];
-	int udebug = 0;
-	char *ca = 0;
-	int ca_port = 0;
 	ib_vendor_call_t call;
 	is3_general_info_t *gi;
 	is3_config_space_t *cs;
-	int general_info = 0;
-	int xmit_wait = 0;
 	int i;
 
-	static char const str_opts[] = "C:P:s:t:dNwGVhu";
-	static const struct option long_opts[] = {
-		{ "C", 1, 0, 'C'},
-		{ "P", 1, 0, 'P'},
-		{ "N", 1, 0, 'N'},
-		{ "w", 1, 0, 'w'},
-		{ "debug", 0, 0, 'd'},
-		{ "Guid", 0, 0, 'G'},
-		{ "sm_portid", 1, 0, 's'},
-		{ "timeout", 1, 0, 't'},
-		{ "Version", 0, 0, 'V'},
-		{ "help", 0, 0, 'h'},
-		{ "usage", 0, 0, 'u'},
-		{ }
+	const struct ibdiag_opt opts[] = {
+		{ "N", 'N', 0, NULL, "show IS3 general information"},
+		{ "w", 'w', 0, NULL, "show IS3 port xmit wait counters"},
+		{}
+	};
+	char usage_args[] = "<lid|guid>";
+	const char *usage_examples[] = {
+		"-N 6\t\t# read IS3 general information",
+		"-w 6\t\t# read IS3 port xmit wait counters",
+		NULL
 	};
 
-	argv0 = argv[0];
+	ibdiag_process_opts(argc, argv, NULL, "D", opts, process_opt,
+			    usage_args, usage_examples);
 
-	while (1) {
-		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
-		if ( ch == -1 )
-			break;
-		switch(ch) {
-		case 'C':
-			ca = optarg;
-			break;
-		case 'P':
-			ca_port = strtoul(optarg, 0, 0);
-			break;
-		case 'N':
-			general_info = 1;
-			break;
-		case 'w':
-			xmit_wait = 1;
-			break;
-		case 'd':
-			ibdebug++;
-			madrpc_show_errors(1);
-			umad_debug(udebug);
-			udebug++;
-			break;
-		case 'G':
-			dest_type = IB_DEST_GUID;
-			break;
-		case 's':
-			if (ib_resolve_portid_str(&sm_portid, optarg, IB_DEST_LID, 0) < 0)
-				IBERROR("can't resolve SM destination port %s", optarg);
-			sm_id = &sm_portid;
-			break;
-		case 't':
-			timeout = strtoul(optarg, 0, 0);
-			madrpc_set_timeout(timeout);
-			break;
-		case 'V':
-			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
-			exit(-1);
-		default:
-			usage();
-			break;
-		}
-	}
+	argv0 = argv[0];
 	argc -= optind;
 	argv += optind;
 
 	if (argc > 1)
 		port = strtoul(argv[1], 0, 0);
 
-	madrpc_init(ca, ca_port, mgmt_classes, 4);
+	madrpc_init(ibd_ca, ibd_ca_port, mgmt_classes, 4);
 
 	if (argc) {
-		if (ib_resolve_portid_str(&portid, argv[0], dest_type, sm_id) < 0)
+		if (ib_resolve_portid_str(&portid, argv[0], ibd_dest_type, ibd_sm_id) < 0)
 			IBERROR("can't resolve destination port %s", argv[0]);
 	} else {
 		if (ib_resolve_self(&portid, &port, 0) < 0)
@@ -235,7 +179,7 @@ main(int argc, char **argv)
 	memset(&call, 0, sizeof(call));
 	call.mgmt_class = IB_MLX_VENDOR_CLASS;
 	call.method = IB_MAD_METHOD_GET;
-	call.timeout = timeout;
+	call.timeout = ibd_timeout;
 
 	memset(&buf, 0, sizeof(buf));
 	/* vendor ClassPortInfo is required attribute if class supported */
-- 
1.6.0.4.766.g6fc4a




More information about the general mailing list