[PATCH v2] Add enum strings and *_str functions for enums (Was: Re: [ofa-general] Pending libibverbs patches?)

Ira Weiny weiny2 at llnl.gov
Tue Apr 15 13:35:48 PDT 2008


On Tue, 15 Apr 2008 09:53:22 -0700
Roland Dreier <rdreier at cisco.com> wrote:

>  > I wonder if you would take a small patch to map enums to strings.  I thought I
>  > submitted this before but I do not find it in the list archive so I must have
>  > forgotten about it.
> 
> Yes, that is a useful addition (although it's not that small a patch ;).
> 
> However
> 
>  > +++ b/src/libibverbs.map
>  > @@ -91,4 +91,9 @@ IBVERBS_1.1 {
>  >  		ibv_dontfork_range;
>  >  		ibv_dofork_range;
>  >  		ibv_register_driver;
>  > +
>  > +      __ibv_node_type_str;
>  > +      __ibv_port_state_str;
>  > +      __ibv_event_type_str;
>  > +      __ibv_wc_status_str;
> 
> I don't think we want to export the array of strings as the ABI, since
> that would prevent us from doing localization or anything like that in
> the future, and compiling the inline functions into the application just
> seems less flexible.
>

Good point.

>
>     So I would rather see
>
>  > +static inline const char *ibv_node_type_str(enum ibv_node_type node_type)
>  > +{
>  > +	if (node_type < IBV_NODE_CA || node_type > IBV_NODE_RNIC)
>  > +		node_type = 0;
>  > +	return (__ibv_node_type_str[node_type]);
>  > +}
> 
> the API should be ibv_node_type_str() and it should be a non-inline
> exported string function.
> 

Done, here is v2 of the patch,
Ira


>From 82edbb7d63dcef42bdf20b0ee819dea5794c0c03 Mon Sep 17 00:00:00 2001
From: Ira K. Weiny <weiny2 at llnl.gov>
Date: Wed, 5 Sep 2007 17:10:11 -0700
Subject: [PATCH] Add enum strings and *_str functions for enums


Signed-off-by: Ira K. Weiny <weiny2 at llnl.gov>
---
 Makefile.am                |    3 +-
 examples/devinfo.c         |   13 +----
 examples/rc_pingpong.c     |    3 +-
 examples/srq_pingpong.c    |    3 +-
 examples/uc_pingpong.c     |    3 +-
 examples/ud_pingpong.c     |    3 +-
 include/infiniband/verbs.h |    4 ++
 src/enum_strs.c            |  125 ++++++++++++++++++++++++++++++++++++++++++++
 src/libibverbs.map         |    5 ++
 9 files changed, 145 insertions(+), 17 deletions(-)
 create mode 100644 src/enum_strs.c

diff --git a/Makefile.am b/Makefile.am
index 705b184..46e2354 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -9,7 +9,8 @@ src_libibverbs_la_CFLAGS = $(AM_CFLAGS) -DIBV_CONFIG_DIR=\"$(sysconfdir)/libibve
 libibverbs_version_script = @LIBIBVERBS_VERSION_SCRIPT@
 
 src_libibverbs_la_SOURCES = src/cmd.c src/compat-1_0.c src/device.c src/init.c \
-			    src/marshall.c src/memory.c src/sysfs.c src/verbs.c
+			    src/marshall.c src/memory.c src/sysfs.c src/verbs.c \
+				 src/enum_strs.c
 src_libibverbs_la_LDFLAGS = -version-info 1 -export-dynamic \
     $(libibverbs_version_script)
 src_libibverbs_la_DEPENDENCIES = $(srcdir)/src/libibverbs.map
diff --git a/examples/devinfo.c b/examples/devinfo.c
index 4e4316a..1fadc80 100644
--- a/examples/devinfo.c
+++ b/examples/devinfo.c
@@ -67,17 +67,6 @@ static const char *guid_str(uint64_t node_guid, char *str)
 	return str;
 }
 
-static const char *port_state_str(enum ibv_port_state pstate)
-{
-	switch (pstate) {
-	case IBV_PORT_DOWN:   return "PORT_DOWN";
-	case IBV_PORT_INIT:   return "PORT_INIT";
-	case IBV_PORT_ARMED:  return "PORT_ARMED";
-	case IBV_PORT_ACTIVE: return "PORT_ACTIVE";
-	default:              return "invalid state";
-	}
-}
-
 static const char *port_phy_state_str(uint8_t phys_state)
 {
 	switch (phys_state) {
@@ -266,7 +255,7 @@ static int print_hca_cap(struct ibv_device *ib_dev, uint8_t ib_port)
 		}
 		printf("\t\tport:\t%d\n", port);
 		printf("\t\t\tstate:\t\t\t%s (%d)\n",
-		       port_state_str(port_attr.state), port_attr.state);
+		       ibv_port_state_str(port_attr.state), port_attr.state);
 		printf("\t\t\tmax_mtu:\t\t%s (%d)\n",
 		       mtu_str(port_attr.max_mtu), port_attr.max_mtu);
 		printf("\t\t\tactive_mtu:\t\t%s (%d)\n",
diff --git a/examples/rc_pingpong.c b/examples/rc_pingpong.c
index 7181914..26fa45c 100644
--- a/examples/rc_pingpong.c
+++ b/examples/rc_pingpong.c
@@ -709,7 +709,8 @@ int main(int argc, char *argv[])
 
 			for (i = 0; i < ne; ++i) {
 				if (wc[i].status != IBV_WC_SUCCESS) {
-					fprintf(stderr, "Failed status %d for wr_id %d\n",
+					fprintf(stderr, "Failed status %s (%d) for wr_id %d\n",
+						ibv_wc_status_str(wc[i].status),
 						wc[i].status, (int) wc[i].wr_id);
 					return 1;
 				}
diff --git a/examples/srq_pingpong.c b/examples/srq_pingpong.c
index bc869c9..95bebf4 100644
--- a/examples/srq_pingpong.c
+++ b/examples/srq_pingpong.c
@@ -805,7 +805,8 @@ int main(int argc, char *argv[])
 
 			for (i = 0; i < ne; ++i) {
 				if (wc[i].status != IBV_WC_SUCCESS) {
-					fprintf(stderr, "Failed status %d for wr_id %d\n",
+					fprintf(stderr, "Failed status %s (%d) for wr_id %d\n",
+						ibv_wc_status_str(wc[i].status),
 						wc[i].status, (int) wc[i].wr_id);
 					return 1;
 				}
diff --git a/examples/uc_pingpong.c b/examples/uc_pingpong.c
index 6135030..c09c8c1 100644
--- a/examples/uc_pingpong.c
+++ b/examples/uc_pingpong.c
@@ -697,7 +697,8 @@ int main(int argc, char *argv[])
 
 			for (i = 0; i < ne; ++i) {
 				if (wc[i].status != IBV_WC_SUCCESS) {
-					fprintf(stderr, "Failed status %d for wr_id %d\n",
+					fprintf(stderr, "Failed status %s (%d) for wr_id %d\n",
+						ibv_wc_status_str(wc[i].status),
 						wc[i].status, (int) wc[i].wr_id);
 					return 1;
 				}
diff --git a/examples/ud_pingpong.c b/examples/ud_pingpong.c
index aaee26c..8f3d50b 100644
--- a/examples/ud_pingpong.c
+++ b/examples/ud_pingpong.c
@@ -697,7 +697,8 @@ int main(int argc, char *argv[])
 
 			for (i = 0; i < ne; ++i) {
 				if (wc[i].status != IBV_WC_SUCCESS) {
-					fprintf(stderr, "Failed status %d for wr_id %d\n",
+					fprintf(stderr, "Failed status %s (%d) for wr_id %d\n",
+						ibv_wc_status_str(wc[i].status),
 						wc[i].status, (int) wc[i].wr_id);
 					return 1;
 				}
diff --git a/include/infiniband/verbs.h b/include/infiniband/verbs.h
index a51bb9d..ccabb52 100644
--- a/include/infiniband/verbs.h
+++ b/include/infiniband/verbs.h
@@ -70,6 +70,7 @@ enum ibv_node_type {
 	IBV_NODE_ROUTER,
 	IBV_NODE_RNIC
 };
+const char *ibv_node_type_str(enum ibv_node_type node_type);
 
 enum ibv_transport_type {
 	IBV_TRANSPORT_UNKNOWN	= -1,
@@ -160,6 +161,7 @@ enum ibv_port_state {
 	IBV_PORT_ACTIVE		= 4,
 	IBV_PORT_ACTIVE_DEFER	= 5
 };
+const char *ibv_port_state_str(enum ibv_port_state port_state);
 
 struct ibv_port_attr {
 	enum ibv_port_state	state;
@@ -203,6 +205,7 @@ enum ibv_event_type {
 	IBV_EVENT_QP_LAST_WQE_REACHED,
 	IBV_EVENT_CLIENT_REREGISTER
 };
+const char *ibv_event_type_str(enum ibv_event_type event);
 
 struct ibv_async_event {
 	union {
@@ -238,6 +241,7 @@ enum ibv_wc_status {
 	IBV_WC_RESP_TIMEOUT_ERR,
 	IBV_WC_GENERAL_ERR
 };
+const char *ibv_wc_status_str(enum ibv_wc_status status);
 
 enum ibv_wc_opcode {
 	IBV_WC_SEND,
diff --git a/src/enum_strs.c b/src/enum_strs.c
new file mode 100644
index 0000000..7056f8a
--- /dev/null
+++ b/src/enum_strs.c
@@ -0,0 +1,125 @@
+/*
+ * Copyright (c) 2008 Lawrence Livermore National Laboratory
+ *
+ * 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.
+ *
+ */
+
+#include <infiniband/verbs.h>
+
+static const char *const __ibv_node_type_str[] = {
+	"UNKNOWN",
+	"Channel Adapter",
+	"Switch",
+	"Router",
+	"RNIC"
+};
+const char *ibv_node_type_str(enum ibv_node_type node_type)
+{
+	if (node_type < IBV_NODE_CA || node_type > IBV_NODE_RNIC)
+		node_type = 0;
+	return (__ibv_node_type_str[node_type]);
+}
+
+static const char *const __ibv_port_state_str[] = {
+	"No State Change (NOP)",
+	"DOWN",
+	"INIT",
+	"ARMED",
+	"ACTIVE",
+	"ACTDEFER",
+	"UNKNOWN"
+};
+const char *ibv_port_state_str(enum ibv_port_state port_state)
+{
+	if (port_state < IBV_PORT_NOP || port_state > IBV_PORT_ACTIVE_DEFER)
+		port_state = IBV_PORT_ACTIVE_DEFER + 1;
+	return (__ibv_port_state_str[port_state]);
+}
+
+
+static const char *const __ibv_event_type_str[] = {
+	"CQ Error",
+	"QP Fatal",
+	"QP Request Error",
+	"QP Access Error",
+	"Communication Established",
+	"SQ Drained",
+	"Path Migrated",
+	"Path Migration Request Error",
+	"Device Fatal",
+	"Port Active",
+	"Port Error",
+	"LID Change",
+	"PKey Change",
+	"SM Change",
+	"SRQ Error",
+	"SRQ Limit Reached",
+	"QP Last WQE Reached",
+	"Client Reregistration",
+	"UNKNOWN"
+};
+const char *ibv_event_type_str(enum ibv_event_type event)
+{
+	if (event < IBV_EVENT_CQ_ERR || event > IBV_EVENT_CLIENT_REREGISTER)
+		event = (IBV_EVENT_CLIENT_REREGISTER+1);
+	return (__ibv_event_type_str[event]);
+}
+
+static const char *const __ibv_wc_status_str[] = {
+	"Success",
+	"Local Length Error",
+	"Local QP Operation Error",
+	"Local EE Context Operation Error",
+	"Local Protection Error",
+	"Work Request Flushed Error",
+	"Memory Management Operation Error",
+	"Bad Response Error",
+	"Local Access Error",
+	"Remote Invalid Request Error",
+	"Remote Access Error",
+	"Remote Operation Error",
+	"Transport Retry Counter Exceeded",
+	"RNR Retry Counter Exceeded",
+	"Local RDD Violation Error",
+	"Remote Invalid RD Request",
+	"Aborted Error",
+	"Invalid EE Context Number",
+	"Invalid EE Context State",
+	"Fatal Error",
+	"Response Timeout Error",
+	"General Error"
+};
+const char *ibv_wc_status_str(enum ibv_wc_status status)
+{
+	if (status < IBV_WC_SUCCESS || status > IBV_WC_GENERAL_ERR)
+		status = IBV_WC_GENERAL_ERR;
+	return (__ibv_wc_status_str[status]);
+}
+
diff --git a/src/libibverbs.map b/src/libibverbs.map
index 3a346ed..1827da0 100644
--- a/src/libibverbs.map
+++ b/src/libibverbs.map
@@ -91,4 +91,9 @@ IBVERBS_1.1 {
 		ibv_dontfork_range;
 		ibv_dofork_range;
 		ibv_register_driver;
+
+		ibv_node_type_str;
+		ibv_port_state_str;
+		ibv_event_type_str;
+		ibv_wc_status_str;
 } IBVERBS_1.0;
-- 
1.5.1




More information about the general mailing list