Blame opasadb/path_tools/query/opa_osd_query.c

Packit 857059
/* BEGIN_ICS_COPYRIGHT2 ****************************************
Packit 857059
Packit 857059
Copyright (c) 2015-2017, Intel Corporation
Packit 857059
Packit 857059
Redistribution and use in source and binary forms, with or without
Packit 857059
modification, are permitted provided that the following conditions are met:
Packit 857059
Packit 857059
    * Redistributions of source code must retain the above copyright notice,
Packit 857059
      this list of conditions and the following disclaimer.
Packit 857059
    * Redistributions in binary form must reproduce the above copyright
Packit 857059
      notice, this list of conditions and the following disclaimer in the
Packit 857059
      documentation and/or other materials provided with the distribution.
Packit 857059
    * Neither the name of Intel Corporation nor the names of its contributors
Packit 857059
      may be used to endorse or promote products derived from this software
Packit 857059
      without specific prior written permission.
Packit 857059
Packit 857059
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit 857059
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 857059
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Packit 857059
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
Packit 857059
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 857059
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Packit 857059
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Packit 857059
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Packit 857059
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 857059
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 857059
Packit 857059
** END_ICS_COPYRIGHT2   ****************************************/
Packit 857059
Packit 857059
/* [ICS VERSION STRING: unknown] */
Packit 857059
Packit 857059
/*
Packit 857059
 * This program demonstrates how to use the op_path_path functions. 
Packit 857059
 *
Packit 857059
 * It allows the user to create an arbitrary path query and to view
Packit 857059
 * the response. 
Packit 857059
 */
Packit 857059
Packit 857059
#include <stdio.h>
Packit 857059
#include <stdlib.h>
Packit 857059
#include <string.h>
Packit 857059
#include <unistd.h>
Packit 857059
#include <getopt.h>
Packit 857059
#include <sys/socket.h>
Packit 857059
#include <arpa/inet.h>
Packit 857059
#include <sys/time.h>
Packit 857059
#include "dumppath.h"
Packit 857059
#include "opasadb_path.h"
Packit 857059
#include "opasadb_debug.h"
Packit 857059
Packit 857059
#define DEFAULT_SID 0x1000117500000000
Packit 857059
Packit 857059
void Usage()
Packit 857059
{
Packit 857059
	fprintf(stderr,"Usage: opa_osd_query (options)\n");
Packit 857059
	fprintf(stderr,"Query the opasadb for path records\n\n");
Packit 857059
	fprintf(stderr,"Options are:\n");
Packit 857059
	fprintf(stderr, "\t-v/--verbose\t<arg>\tDebug Level. Should be a number between 1 and 7\n");
Packit 857059
	fprintf(stderr, "\t-s/--slid\t<arg>\tSource LID\n");
Packit 857059
	fprintf(stderr, "\t-d/--dlid\t<arg>\tDestination LID\n");
Packit 857059
	fprintf(stderr, "\t-S/--sgid\t<arg>\tSource GID (in GID or inet6 format)\n");
Packit 857059
	fprintf(stderr, "\t-D/--dgid\t<arg>\tDestination GID (in GID or inet6 format)\n");
Packit 857059
	fprintf(stderr, "\t-k/--pkey\t<arg>\tPartition Key\n");
Packit 857059
	fprintf(stderr, "\t-i/--sid\t<arg>\tService ID\n");
Packit 857059
	fprintf(stderr, "\t-h/--hfi\t<arg>\tThe HFI to use (Defaults to the first hfi)\n");
Packit 857059
	fprintf(stderr, "\t-p/--port\t<arg>\tThe port to use (Defaults to the first port)\n");
Packit 857059
	fprintf(stderr, "\t--help\t\t\tProvide full help text\n");
Packit 857059
	
Packit 857059
	exit(2);
Packit 857059
}
Packit 857059
Packit 857059
Packit 857059
void Usage_full()
Packit 857059
{
Packit 857059
	fprintf(stderr,"Usage: opa_osd_query (options)\n");
Packit 857059
	fprintf(stderr,"Query the opasadb for path records\n\n");
Packit 857059
	fprintf(stderr,"Options are:\n");
Packit 857059
	fprintf(stderr, "\t-v/--verbose\t<arg>\tDebug Level. Should be a number between 1 and 7\n");
Packit 857059
	fprintf(stderr, "\t-s/--slid\t<arg>\tSource LID\n");
Packit 857059
	fprintf(stderr, "\t-d/--dlid\t<arg>\tDestination LID\n");
Packit 857059
	fprintf(stderr, "\t-S/--sgid\t<arg>\tSource GID (in GID or inet6 format)\n");
Packit 857059
	fprintf(stderr, "\t-D/--dgid\t<arg>\tDestination GID (in GID or inet6 format)\n");
Packit 857059
	fprintf(stderr, "\t-k/--pkey\t<arg>\tPartition Key\n");
Packit 857059
	fprintf(stderr, "\t-i/--sid\t<arg>\tService ID\n");
Packit 857059
	fprintf(stderr, "\t-h/--hfi\t<arg>\tThe HFI to use (Defaults to the first hfi)\n");
Packit 857059
	fprintf(stderr, "\t-p/--port\t<arg>\tThe port to use (Defaults to the first port)\n");
Packit 857059
	fprintf(stderr, "\t--help\t\t\tProvide full help text\n");
Packit 857059
	fprintf(stderr,"\n"
Packit 857059
				"This tool allows you to create an arbitrary path\n"
Packit 857059
				"query and view the result. \n\n"
Packit 857059
				"All arguments are optional, but ill-formed\n"
Packit 857059
				"queries can be expected to fail. You must provide\n"
Packit 857059
				"at least a pair of lids, or a pair of gids.\n\n"
Packit 857059
				"Mixing of lids and gid in query is not permitted.\n\n"
Packit 857059
				"SID or PKey can also be provided but not both.\n\n"
Packit 857059
				"If you have multiple HFIs, the same lid can appear on\n"
Packit 857059
				"more than one HFI, so you must specify which HFI to use\n"
Packit 857059
				"when searching by lids and you have multiple HFIs.\n\n"
Packit 857059
				"Numbers can be in decimal, hex or octal.\n\n"
Packit 857059
				"Gids can be specified in GID format (\"0x00000000:0x00000000\")\n"
Packit 857059
				"or in Inet6 format (\"x:x:x:x:x:x:x:x\").\n\n"
Packit 857059
				"The HFI can be identified by name (\"hfi1_0\") or by\n"
Packit 857059
				"number (1, 2, 3, et cetera)\n\n"
Packit 857059
				"Example:\topa_osd_query -s2 -d4\n");
Packit 857059
	exit(0);
Packit 857059
}
Packit 857059
Packit 857059
int main(int argc, char **argv)
Packit 857059
{
Packit 857059
	int err, debug;
Packit 857059
	char *hfi_name = NULL;
Packit 857059
	int  port = 1;
Packit 857059
Packit 857059
	op_path_rec_t query;
Packit 857059
	op_path_rec_t response;
Packit 857059
Packit 857059
	struct ibv_context *context;
Packit 857059
	struct ibv_device *device;
Packit 857059
	void *hfi;
Packit 857059
Packit 857059
	memset(&query,0,sizeof(query));
Packit 857059
	memset(&response,0,sizeof(response));
Packit 857059
Packit 857059
	do {
Packit 857059
		int c;
Packit 857059
Packit 857059
		static char *short_options = "v:h:p:s:d:S:D:k:i:";
Packit 857059
		static struct option long_options[] = {
Packit 857059
			{ .name = "verbose", .has_arg = 1, .val = 'v' },
Packit 857059
			{ .name = "slid", .has_arg = 1, .val = 's' },
Packit 857059
			{ .name = "dlid", .has_arg = 1, .val = 'd' },
Packit 857059
			{ .name = "sgid", .has_arg = 1, .val = 'S' },
Packit 857059
			{ .name = "dgid", .has_arg = 1, .val = 'D' },
Packit 857059
			{ .name = "pkey", .has_arg = 1, .val = 'k' },
Packit 857059
			{ .name = "sid",  .has_arg = 1, .val = 'i' },
Packit 857059
			{ .name = "hfi",  .has_arg = 1, .val = 'h' },
Packit 857059
			{ .name = "port", .has_arg = 1, .val = 'p' },
Packit 857059
			{ .name = "help", .has_arg = 0, .val = '$' },
Packit 857059
			{0}
Packit 857059
		};
Packit 857059
		
Packit 857059
		c = getopt_long(argc, argv, short_options, long_options, NULL);
Packit 857059
		if (c == -1) 
Packit 857059
			break;
Packit 857059
Packit 857059
		switch (c) {
Packit 857059
		case 'v': debug = strtol(optarg,NULL,0); op_log_set_level(debug); break;
Packit 857059
		case 'h': hfi_name = (char*)strdupa(optarg); break;
Packit 857059
		case 'p': port = strtol(optarg,NULL,0); break;
Packit 857059
		case 's': query.slid = htons(strtol(optarg,NULL,0)); break;
Packit 857059
		case 'd': query.dlid = htons(strtol(optarg,NULL,0)); break;
Packit 857059
		case 'k': query.pkey = htons(strtol(optarg,NULL,0)); break;
Packit 857059
		case 'i': query.service_id = hton64(strtoll(optarg, NULL, 0)); break;
Packit 857059
		case 'S': 
Packit 857059
			if (!parse_gid(optarg, &query.sgid)) {
Packit 857059
				fprintf(stderr, "Badly formatted SGID.\n"); 
Packit 857059
				return -1;
Packit 857059
			}
Packit 857059
			break;
Packit 857059
		case 'D': 
Packit 857059
			if (!parse_gid(optarg, &query.dgid)) {
Packit 857059
				fprintf(stderr, "Badly formatted DGID.\n");
Packit 857059
				return -1;
Packit 857059
			}
Packit 857059
			break;
Packit 857059
		case '$':
Packit 857059
			Usage_full(); //exits
Packit 857059
			break;
Packit 857059
		default:
Packit 857059
			Usage(); //exits
Packit 857059
			break;
Packit 857059
		}			
Packit 857059
	} while (1);
Packit 857059
Packit 857059
	if ((query.pkey == 0) && (query.service_id == 0)) {
Packit 857059
		query.service_id = hton64(DEFAULT_SID);
Packit 857059
	}
Packit 857059
	print_path_record("Query Parameters", &query);
Packit 857059
	if ((query.pkey != 0) && (query.service_id != 0)) {
Packit 857059
                fprintf(stderr, "Query using both Service ID and PKey not supported\n");
Packit 857059
                return -1;
Packit 857059
	}
Packit 857059
	/*
Packit 857059
 	 * Finds and opens the HFI.
Packit 857059
	 */
Packit 857059
	hfi = op_path_find_hfi(hfi_name,&device);
Packit 857059
	if (!device || !hfi) {
Packit 857059
		fprintf(stderr, "Could not open device %s, error code %d\n", (hfi_name?hfi_name:"<null>"), errno);
Packit 857059
		return -1;
Packit 857059
	} else {
Packit 857059
		fprintf(stderr,"Using device %s\n",
Packit 857059
				device->name);
Packit 857059
	}
Packit 857059
Packit 857059
	/*
Packit 857059
	 * op_path_find_hfi maps the device name to an ibv_device
Packit 857059
 	 * structure. op_path_open opens a channel to the SM 
Packit 857059
	 * using the specified device and the specified port.
Packit 857059
	 */
Packit 857059
	if ((context=op_path_open(device,port)) == NULL) {
Packit 857059
		fprintf(stderr, "Could not access the Distributed SA. This may mean that\n"
Packit 857059
			"the ibacm/dsap has not been started, or that it has not been able\n"
Packit 857059
			"to contact the SA.\n");
Packit 857059
		return -1;
Packit 857059
	}
Packit 857059
Packit 857059
	err = op_path_get_path_by_rec(context,
Packit 857059
								 &query,
Packit 857059
								 &response);
Packit 857059
Packit 857059
	if (err == EINVAL || err == ENOENT) {
Packit 857059
		printf("No path found that matches this query.\n");
Packit 857059
	} else if (err) {
Packit 857059
		printf("******\n"
Packit 857059
				"Error: Get Path returned %d for query: %s\n"
Packit 857059
				"******\n", 
Packit 857059
				err, strerror(err));
Packit 857059
	} else {
Packit 857059
		print_path_record("Result",&response);
Packit 857059
	}
Packit 857059
Packit 857059
	ibv_close_device(hfi);
Packit 857059
	op_path_close(context);
Packit 857059
Packit 857059
	return 0;
Packit 857059
}