[ofa-general] [PATCH 4/8] convert ibping to "new" ibmad interface

Ira Weiny weiny2 at llnl.gov
Tue Feb 17 21:06:48 PST 2009


>From d109788f46b5839698f3f4a1f75bcbfe22a3b46d Mon Sep 17 00:00:00 2001
From: weiny2 at llnl.gov <weiny2 at wopri.(none)>
Date: Tue, 17 Feb 2009 20:08:53 -0800
Subject: [PATCH] convert ibping to "new" ibmad interface

   To do this I needed the following additional functions
   mad_register_client_via
   mad_register_server_via
   mad_send_via
   mad_receive_via
   mad_respond_via
   ib_vendor_call_via

Also further mark some functions as depricated and clean up interface a bit
more.

Signed-off-by: weiny2 at llnl.gov <weiny2 at wopri.(none)>
---
 infiniband-diags/src/ibping.c     |   21 +++++++++----
 libibmad/include/infiniband/mad.h |   29 ++++++++++++++++++
 libibmad/src/libibmad.map         |    5 +++
 libibmad/src/mad_internal.h       |   44 ++++++++++++++++++++++++++++
 libibmad/src/register.c           |   58 ++++++++++++++++++++++++++++++-------
 libibmad/src/rpc.c                |    8 +----
 libibmad/src/serv.c               |   39 +++++++++++++++++++++++--
 libibmad/src/vendor.c             |   15 ++++++++-
 8 files changed, 190 insertions(+), 29 deletions(-)
 create mode 100644 libibmad/src/mad_internal.h

diff --git a/infiniband-diags/src/ibping.c b/infiniband-diags/src/ibping.c
index 29c98c2..7d458bf 100644
--- a/infiniband-diags/src/ibping.c
+++ b/infiniband-diags/src/ibping.c
@@ -48,6 +48,8 @@
 
 #include "ibdiag_common.h"
 
+struct ibmad_port *srcport;
+
 static char host_and_domain[IB_VENDOR_RANGE2_DATA_SIZE];
 static char last_host[IB_VENDOR_RANGE2_DATA_SIZE];
 
@@ -90,7 +92,7 @@ ibping_serv(void)
 
 	DEBUG("starting to serve...");
 
-	while ((umad = mad_receive(0, -1))) {
+	while ((umad = mad_receive_via(0, -1, srcport))) {
 
 		mad = umad_get_mad(umad);
 		data = (char *)mad + IB_VENDOR_RANGE2_DATA_OFFS;
@@ -99,7 +101,7 @@ ibping_serv(void)
 
 		DEBUG("Pong: %s", data);
 
-		if (mad_respond(umad, 0, 0) < 0)
+		if (mad_respond_via(umad, 0, 0, srcport) < 0)
 			DEBUG("respond failed");
 
 		mad_free(umad);
@@ -128,7 +130,7 @@ ibping(ib_portid_t *portid, int quiet)
 	call.timeout = 0;
 	memset(&call.rmpp, 0, sizeof call.rmpp);
 
-	if (!ib_vendor_call(data, portid, &call))
+	if (!ib_vendor_call_via(data, portid, &call, srcport))
 		return ~0llu;
 
 	rtt = getcurrenttime() - start;
@@ -216,10 +218,12 @@ int main(int argc, char **argv)
 	if (!argc && !server)
 		ibdiag_show_usage();
 
-	madrpc_init(ibd_ca, ibd_ca_port, mgmt_classes, 3);
+	srcport = mad_rpc_open_port(ibd_ca, ibd_ca_port, mgmt_classes, 3);
+	if (!srcport)
+		IBERROR("Failed to open '%s' port '%d'", ibd_ca, ibd_ca_port);
 
 	if (server) {
-		if (mad_register_server(ping_class, 0, 0, oui) < 0)
+		if (mad_register_server_via(ping_class, 0, 0, oui, srcport) < 0)
 			IBERROR("can't serve class %d on this port", ping_class);
 
 		get_host_and_domain(host_and_domain, sizeof host_and_domain);
@@ -229,10 +233,11 @@ int main(int argc, char **argv)
 		exit(0);
 	}
 
-	if (mad_register_client(ping_class, 0) < 0)
+	if (mad_register_client_via(ping_class, 0, srcport) < 0)
 		IBERROR("can't register ping class %d on this port", ping_class);
 
-	if (ib_resolve_portid_str(&portid, argv[0], ibd_dest_type, ibd_sm_id) < 0)
+	if (ib_resolve_portid_str_via(&portid, argv[0], ibd_dest_type,
+					ibd_sm_id, srcport) < 0)
 		IBERROR("can't resolve destination port %s", argv[0]);
 
 	signal(SIGINT, report);
@@ -260,5 +265,7 @@ int main(int argc, char **argv)
 
 	report(0);
 
+	mad_rpc_close_port(srcport);
+
 	exit(-1);
 }
diff --git a/libibmad/include/infiniband/mad.h b/libibmad/include/infiniband/mad.h
index 5806e70..8e61395 100644
--- a/libibmad/include/infiniband/mad.h
+++ b/libibmad/include/infiniband/mad.h
@@ -650,6 +650,7 @@ enum MAD_NODE_TYPE {
 };
 
 /******************************************************************************/
+struct ibmad_port;
 
 /* portid.c */
 MAD_EXPORT char *portid2str(ib_portid_t * portid);
@@ -692,26 +693,50 @@ MAD_EXPORT int mad_build_pkt(void *umad, ib_rpc_t * rpc, ib_portid_t * dport,
 			     ib_rmpp_hdr_t * rmpp, void *data);
 
 /* register.c */
+/* depricated */
 MAD_EXPORT int mad_register_port_client(int port_id, int mgmt,
 					uint8_t rmpp_version);
 MAD_EXPORT int mad_register_client(int mgmt, uint8_t rmpp_version);
 MAD_EXPORT int mad_register_server(int mgmt, uint8_t rmpp_version,
 				   long method_mask[16 / sizeof(long)],
 				   uint32_t class_oui);
+
+/* register.c new interface */
+MAD_EXPORT int mad_register_client_via(int mgmt, uint8_t rmpp_version,
+				struct ibmad_port *srcport);
+MAD_EXPORT int mad_register_server_via(int mgmt, uint8_t rmpp_version,
+				long method_mask[16 / sizeof(long)],
+				uint32_t class_oui,
+				struct ibmad_port *srcport);
 MAD_EXPORT int mad_class_agent(int mgmt);
 MAD_EXPORT int mad_agent_class(int agent);
 
 /* serv.c */
+/* depricated */
 MAD_EXPORT int mad_send(ib_rpc_t * rpc, ib_portid_t * dport,
 			ib_rmpp_hdr_t * rmpp, void *data);
 MAD_EXPORT void *mad_receive(void *umad, int timeout);
 MAD_EXPORT int mad_respond(void *umad, ib_portid_t * portid, uint32_t rstatus);
+
+/* serv.c new interface */
+MAD_EXPORT int mad_send_via(ib_rpc_t * rpc, ib_portid_t * dport,
+			ib_rmpp_hdr_t * rmpp, void *data,
+			struct ibmad_port *srcport);
+MAD_EXPORT void *mad_receive_via(void *umad, int timeout,
+			struct ibmad_port *srcport);
+MAD_EXPORT int mad_respond_via(void *umad, ib_portid_t * portid, uint32_t rstatus,
+			struct ibmad_port *srcport);
 MAD_EXPORT void *mad_alloc(void);
 MAD_EXPORT void mad_free(void *umad);
 
 /* vendor.c */
+/* depricated */
 MAD_EXPORT uint8_t *ib_vendor_call(void *data, ib_portid_t * portid,
 				   ib_vendor_call_t * call);
+/* vendor.c new interface */
+MAD_EXPORT uint8_t *ib_vendor_call_via(void *data, ib_portid_t * portid,
+				   ib_vendor_call_t * call,
+				   struct ibmad_port *srcport);
 
 static inline int mad_is_vendor_range1(int mgmt)
 {
@@ -746,6 +771,7 @@ void *mad_rpc_rmpp(const struct ibmad_port *srcport, ib_rpc_t * rpc, ib_portid_t
 MAD_EXPORT int mad_rpc_portid(struct ibmad_port *srcport);
 
 /* smp.c */
+/* depricated */
 MAD_EXPORT uint8_t *smp_query(void *buf, ib_portid_t * id, unsigned attrid,
 			      unsigned mod, unsigned timeout);
 MAD_EXPORT uint8_t *smp_set(void *buf, ib_portid_t * id, unsigned attrid,
@@ -758,6 +784,7 @@ uint8_t *smp_set_via(void *buf, ib_portid_t * id, unsigned attrid, unsigned mod,
 		     unsigned timeout, const struct ibmad_port *srcport);
 
 /* sa.c */
+/* depricated */
 uint8_t *sa_call(void *rcvbuf, ib_portid_t * portid, ib_sa_call_t * sa,
 		 unsigned timeout);
 MAD_EXPORT int ib_path_query(ibmad_gid_t srcgid, ibmad_gid_t destgid, ib_portid_t * sm_id, void *buf);	/* returns lid */
@@ -769,6 +796,7 @@ int ib_path_query_via(const struct ibmad_port *srcport, ibmad_gid_t srcgid,
 		      ibmad_gid_t destgid, ib_portid_t * sm_id, void *buf);
 
 /* resolve.c */
+/* depricated */
 MAD_EXPORT int ib_resolve_smlid(ib_portid_t * sm_id, int timeout);
 MAD_EXPORT int ib_resolve_guid(ib_portid_t * portid, uint64_t * guid,
 			       ib_portid_t * sm_id, int timeout);
@@ -790,6 +818,7 @@ int ib_resolve_self_via(ib_portid_t * portid, int *portnum, ibmad_gid_t * gid,
 			const struct ibmad_port *srcport);
 
 /* gs.c */
+/* depricated */
 MAD_EXPORT uint8_t *perf_classportinfo_query(void *rcvbuf, ib_portid_t * dest,
 					     int port, unsigned timeout);
 MAD_EXPORT uint8_t *port_performance_query(void *rcvbuf, ib_portid_t * dest,
diff --git a/libibmad/src/libibmad.map b/libibmad/src/libibmad.map
index 6f0c0b5..ee1804a 100644
--- a/libibmad/src/libibmad.map
+++ b/libibmad/src/libibmad.map
@@ -60,6 +60,8 @@ IBMAD_1.3 {
 		mad_class_agent;
 		mad_register_client;
 		mad_register_server;
+		mad_register_client_via;
+		mad_register_server_via;
 		ib_resolve_guid;
 		ib_resolve_portid_str;
 		ib_resolve_self;
@@ -85,10 +87,13 @@ IBMAD_1.3 {
 		mad_free;
 		mad_receive;
 		mad_respond;
+		mad_receive_via;
+		mad_respond_via;
 		mad_send;
 		smp_query;
 		smp_set;
 		ib_vendor_call;
+		ib_vendor_call_via;
 		smp_query_via;
 		smp_set_via;
 		ib_path_query_via;
diff --git a/libibmad/src/mad_internal.h b/libibmad/src/mad_internal.h
new file mode 100644
index 0000000..9afe7a9
--- /dev/null
+++ b/libibmad/src/mad_internal.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2004-2006 Voltaire Inc.  All rights reserved.
+ *
+ * This software is available to you under a choice of one of two
+ * licenses.  You may choose to be licensed under the terms of the GNU
+ * General Public License (GPL) Version 2, available from the file
+ * COPYING in the main directory of this source tree, or the
+ * OpenIB.org BSD license below:
+ *
+ *     Redistribution and use in source and binary forms, with or
+ *     without modification, are permitted provided that the following
+ *     conditions are met:
+ *
+ *      - Redistributions of source code must retain the above
+ *        copyright notice, this list of conditions and the following
+ *        disclaimer.
+ *
+ *      - Redistributions in binary form must reproduce the above
+ *        copyright notice, this list of conditions and the following
+ *        disclaimer in the documentation and/or other materials
+ *        provided with the distribution.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ */
+
+#ifndef _MAD_INTERNAL_H_
+#define _MAD_INTERNAL_H_
+
+#define MAX_CLASS 256
+
+struct ibmad_port {
+	int port_id;		/* file descriptor returned by umad_open() */
+	int class_agents[MAX_CLASS];	/* class2agent mapper */
+};
+
+#endif /* _MAD_INTERNAL_H_ */
diff --git a/libibmad/src/register.c b/libibmad/src/register.c
index 4d91ff8..4aabd7c 100644
--- a/libibmad/src/register.c
+++ b/libibmad/src/register.c
@@ -43,10 +43,11 @@
 #include <infiniband/umad.h>
 #include <infiniband/mad.h>
 
+#include "mad_internal.h"
+
 #undef DEBUG
 #define DEBUG	if (ibdebug)	IBWARN
 
-#define MAX_CLASS	256
 #define MAX_AGENTS	256
 
 static int class_agent[MAX_CLASS];
@@ -136,22 +137,57 @@ int mad_register_port_client(int port_id, int mgmt, uint8_t rmpp_version)
 
 int mad_register_client(int mgmt, uint8_t rmpp_version)
 {
+	int rc = 0;
+	struct ibmad_port port;
+
+	port.port_id = madrpc_portid();
+	rc = mad_register_client_via(mgmt, rmpp_version, &port);
+	if (rc < 0)
+		return rc;
+	return register_agent(port.class_agents[mgmt], mgmt);
+}
+
+int mad_register_client_via(int mgmt, uint8_t rmpp_version,
+			struct ibmad_port *srcport)
+{
 	int agent;
 
-	agent = mad_register_port_client(madrpc_portid(), mgmt, rmpp_version);
+	if (!srcport)
+		return -1;
+
+	agent = mad_register_port_client(mad_rpc_portid(srcport), mgmt, rmpp_version);
 	if (agent < 0)
 		return agent;
 
-	return register_agent(agent, mgmt);
+	srcport->class_agents[mgmt] = agent;
+	return 0;
 }
 
 int
 mad_register_server(int mgmt, uint8_t rmpp_version,
 		    long method_mask[], uint32_t class_oui)
 {
+	int rc = 0;
+	struct ibmad_port port;
+
+	port.port_id = madrpc_portid();
+	port.class_agents[mgmt] = class_agent[mgmt];
+	rc = mad_register_server_via(mgmt, rmpp_version,
+				method_mask, class_oui,
+				&port);
+	if (rc < 0)
+		return rc;
+	return register_agent(port.class_agents[mgmt], mgmt);
+}
+
+int
+mad_register_server_via(int mgmt, uint8_t rmpp_version,
+		    long method_mask[], uint32_t class_oui,
+		    struct ibmad_port *srcport)
+{
 	long class_method_mask[16 / sizeof(long)];
 	uint8_t oui[3];
-	int agent, vers, mad_portid;
+	int agent, vers;
 
 	if (method_mask)
 		memcpy(class_method_mask, method_mask,
@@ -159,11 +195,12 @@ mad_register_server(int mgmt, uint8_t rmpp_version,
 	else
 		memset(class_method_mask, 0xff, sizeof(class_method_mask));
 
-	if ((mad_portid = madrpc_portid()) < 0)
+	if (!srcport)
 		return -1;
 
-	if (class_agent[mgmt] >= 0) {
-		DEBUG("Class 0x%x already registered", mgmt);
+	if (srcport->class_agents[mgmt] >= 0) {
+		DEBUG("Class 0x%x already registered %d",
+			mgmt, srcport->class_agents[mgmt]);
 		return -1;
 	}
 	if ((vers = mgmt_class_vers(mgmt)) <= 0) {
@@ -175,19 +212,18 @@ mad_register_server(int mgmt, uint8_t rmpp_version,
 		oui[0] = (class_oui >> 16) & 0xff;
 		oui[1] = (class_oui >> 8) & 0xff;
 		oui[2] = class_oui & 0xff;
-		if ((agent = umad_register_oui(mad_portid, mgmt, rmpp_version,
+		if ((agent = umad_register_oui(srcport->port_id, mgmt, rmpp_version,
 					       oui, class_method_mask)) < 0) {
 			DEBUG("Can't register agent for class %d", mgmt);
 			return -1;
 		}
-	} else if ((agent = umad_register(mad_portid, mgmt, vers, rmpp_version,
+	} else if ((agent = umad_register(srcport->port_id, mgmt, vers, rmpp_version,
 					  class_method_mask)) < 0) {
 		DEBUG("Can't register agent for class %d", mgmt);
 		return -1;
 	}
 
-	if (register_agent(agent, mgmt) < 0)
-		return -1;
+	srcport->class_agents[mgmt] = agent;
 
 	return agent;
 }
diff --git a/libibmad/src/rpc.c b/libibmad/src/rpc.c
index 20eeb89..bcb0a75 100644
--- a/libibmad/src/rpc.c
+++ b/libibmad/src/rpc.c
@@ -43,12 +43,7 @@
 #include <infiniband/umad.h>
 #include <infiniband/mad.h>
 
-#define MAX_CLASS 256
-
-struct ibmad_port {
-	int port_id;		/* file descriptor returned by umad_open() */
-	int class_agents[MAX_CLASS];	/* class2agent mapper */
-};
+#include "mad_internal.h"
 
 int ibdebug;
 
@@ -325,6 +320,7 @@ struct ibmad_port *mad_rpc_open_port(char *dev_name, int dev_port,
 		return NULL;
 	}
 
+	memset(p->class_agents, 0xff, sizeof p->class_agents);
 	while (num_classes--) {
 		uint8_t rmpp_version = 0;
 		int mgmt = *mgmt_classes++;
diff --git a/libibmad/src/serv.c b/libibmad/src/serv.c
index c7631bb..0ce1660 100644
--- a/libibmad/src/serv.c
+++ b/libibmad/src/serv.c
@@ -42,12 +42,25 @@
 #include <infiniband/umad.h>
 #include <infiniband/mad.h>
 
+#include "mad_internal.h"
+
 #undef DEBUG
 #define DEBUG	if (ibdebug)	IBWARN
 
 int
 mad_send(ib_rpc_t * rpc, ib_portid_t * dport, ib_rmpp_hdr_t * rmpp, void *data)
 {
+	struct ibmad_port port;
+
+	port.port_id = madrpc_portid();
+	port.class_agents[rpc->mgtclass] = mad_class_agent(rpc->mgtclass);
+	return mad_send_via(rpc, dport, rmpp, data, &port);
+}
+
+int
+mad_send_via(ib_rpc_t * rpc, ib_portid_t * dport, ib_rmpp_hdr_t * rmpp, void *data,
+		struct ibmad_port *srcport)
+{
 	uint8_t pktbuf[1024];
 	void *umad = pktbuf;
 
@@ -64,7 +77,7 @@ mad_send(ib_rpc_t * rpc, ib_portid_t * dport, ib_rmpp_hdr_t * rmpp, void *data)
 		      (char *)umad_get_mad(umad) + rpc->dataoffs, rpc->datasz);
 	}
 
-	if (umad_send(madrpc_portid(), mad_class_agent(rpc->mgtclass),
+	if (umad_send(srcport->port_id, srcport->class_agents[rpc->mgtclass],
 		      umad, IB_MAD_SIZE, rpc->timeout, 0) < 0) {
 		IBWARN("send failed; %m");
 		return -1;
@@ -75,6 +88,18 @@ mad_send(ib_rpc_t * rpc, ib_portid_t * dport, ib_rmpp_hdr_t * rmpp, void *data)
 
 int mad_respond(void *umad, ib_portid_t * portid, uint32_t rstatus)
 {
+	int i = 0;
+	struct ibmad_port port;
+
+	port.port_id = madrpc_portid();
+	for (i = 1; i < MAX_CLASS; i++)
+		port.class_agents[i] = mad_class_agent(i);
+	return mad_respond_via(umad, portid, rstatus, &port);
+}
+
+int mad_respond_via(void *umad, ib_portid_t * portid, uint32_t rstatus,
+		struct ibmad_port *srcport)
+{
 	uint8_t *mad = umad_get_mad(umad);
 	ib_mad_addr_t *mad_addr;
 	ib_rpc_t rpc = { 0 };
@@ -138,7 +163,7 @@ int mad_respond(void *umad, ib_portid_t * portid, uint32_t rstatus)
 	if (ibdebug > 1)
 		xdump(stderr, "mad respond pkt\n", mad, IB_MAD_SIZE);
 
-	if (umad_send(madrpc_portid(), mad_class_agent(rpc.mgtclass), umad,
+	if (umad_send(srcport->port_id, srcport->class_agents[rpc.mgtclass], umad,
 		      IB_MAD_SIZE, rpc.timeout, 0) < 0) {
 		DEBUG("send failed; %m");
 		return -1;
@@ -149,11 +174,19 @@ int mad_respond(void *umad, ib_portid_t * portid, uint32_t rstatus)
 
 void *mad_receive(void *umad, int timeout)
 {
+	struct ibmad_port port;
+
+	port.port_id = madrpc_portid();
+	return mad_receive_via(umad, timeout, &port);
+}
+
+void *mad_receive_via(void *umad, int timeout, struct ibmad_port *srcport)
+{
 	void *mad = umad ? umad : umad_alloc(1, umad_size() + IB_MAD_SIZE);
 	int agent;
 	int length = IB_MAD_SIZE;
 
-	if ((agent = umad_recv(madrpc_portid(), mad, &length, timeout)) < 0) {
+	if ((agent = umad_recv(srcport->port_id, mad, &length, timeout)) < 0) {
 		if (!umad)
 			umad_free(mad);
 		DEBUG("recv failed: %m");
diff --git a/libibmad/src/vendor.c b/libibmad/src/vendor.c
index 50a878e..1a129e5 100644
--- a/libibmad/src/vendor.c
+++ b/libibmad/src/vendor.c
@@ -40,6 +40,7 @@
 #include <string.h>
 
 #include <infiniband/mad.h>
+#include "mad_internal.h"
 
 #undef DEBUG
 #define DEBUG 	if (ibdebug)	IBWARN
@@ -53,6 +54,16 @@ static inline int response_expected(int method)
 uint8_t *ib_vendor_call(void *data, ib_portid_t * portid,
 			ib_vendor_call_t * call)
 {
+	struct ibmad_port port;
+
+	port.port_id = madrpc_portid();
+	return ib_vendor_call_via(data, portid, call, &port);
+}
+
+uint8_t *ib_vendor_call_via(void *data, ib_portid_t * portid,
+			ib_vendor_call_t * call,
+			struct ibmad_port *srcport)
+{
 	ib_rpc_t rpc = { 0 };
 	int range1 = 0, resp_expected;
 
@@ -90,7 +101,7 @@ uint8_t *ib_vendor_call(void *data, ib_portid_t * portid,
 		portid->qkey = IB_DEFAULT_QP1_QKEY;
 
 	if (resp_expected)
-		return madrpc_rmpp(&rpc, portid, 0, data);	/* FIXME: no RMPP for now */
+		return mad_rpc_rmpp(srcport, &rpc, portid, 0, data);	/* FIXME: no RMPP for now */
 
-	return mad_send(&rpc, portid, 0, data) < 0 ? 0 : data;	/* FIXME: no RMPP for now */
+	return mad_send_via(&rpc, portid, 0, data, srcport) < 0 ? 0 : data;	/* FIXME: no RMPP for now */
 }
-- 
1.5.4.5




More information about the general mailing list