[ofa-general] Re: [PATCH v2] Update mad formatting functions.

Ira Weiny weiny2 at llnl.gov
Thu Apr 16 00:16:43 PDT 2009


Ok, v2 has a couple of changes.

   1) implements the mad_vsnprintf with vsnprintf.
   2) change formatting char to 'm' since "F" is floating point
   3) add 'M' for printing the "name" of the field specified.

The reason I did not use vsnprintf before was because of this statement in the
vsnprintf man page.

       The functions vprintf(), vfprintf(), vsprintf(), vsnprintf() are equiv-
       alent  to  the  functions  printf(),  fprintf(), sprintf(), snprintf(),
       respectively, except that they are called with a va_list instead  of  a
       variable  number  of  arguments. These functions do not call the va_end
       macro. Consequently, the value of ap is undefined after the  call.  The
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
       application should call va_end(ap) itself afterwards.

I have made a comment in the patch where I am unsure of the call.  This seems
to work just fine on my Linux systems with gcc.  Will this work on other
systems/compilers?

Ira


From: Ira Weiny <weiny2 at llnl.gov>
Date: Thu, 16 Apr 2009 00:07:03 -0700
Subject: [PATCH] Update mad formatting functions.

   Add mad_snprintf w/ man page
   Add mad_fprintf w/ man page
   Add comments to document current functions.
   Rename parameters to avoid confusion with other functions which take "buf"
   Mark mad_print_field as deprecated

Signed-off-by: Ira Weiny <weiny2 at llnl.gov>
---
 libibmad/Makefile.am              |    2 +
 libibmad/include/infiniband/mad.h |   28 ++++++++-
 libibmad/man/mad_fprintf.3        |   76 +++++++++++++++++++++++
 libibmad/man/mad_snprintf.3       |    2 +
 libibmad/src/fields.c             |  121 +++++++++++++++++++++++++++++++++++-
 libibmad/src/libibmad.map         |    2 +
 6 files changed, 224 insertions(+), 7 deletions(-)
 create mode 100644 libibmad/man/mad_fprintf.3
 create mode 100644 libibmad/man/mad_snprintf.3

diff --git a/libibmad/Makefile.am b/libibmad/Makefile.am
index 4f3ba98..da32899 100644
--- a/libibmad/Makefile.am
+++ b/libibmad/Makefile.am
@@ -5,6 +5,8 @@ INCLUDES = -I$(srcdir)/include -I$(includedir)
 
 lib_LTLIBRARIES = libibmad.la
 
+man_MANS = man/mad_fprintf.3 man/mad_snprintf.3
+
 libibmad_la_CFLAGS = -Wall
 
 if HAVE_LD_VERSION_SCRIPT
diff --git a/libibmad/include/infiniband/mad.h b/libibmad/include/infiniband/mad.h
index b8290a7..ce840ac 100644
--- a/libibmad/include/infiniband/mad.h
+++ b/libibmad/include/infiniband/mad.h
@@ -719,9 +719,31 @@ MAD_EXPORT void mad_set_array(void *buf, int base_offs, enum MAD_FIELDS field, v
 MAD_EXPORT void mad_get_array(void *buf, int base_offs, enum MAD_FIELDS field, void *val);
 MAD_EXPORT void mad_decode_field(uint8_t * buf, enum MAD_FIELDS field, void *val);
 MAD_EXPORT void mad_encode_field(uint8_t * buf, enum MAD_FIELDS field, void *val);
-MAD_EXPORT int mad_print_field(enum MAD_FIELDS field, const char *name, void *val);
-MAD_EXPORT char *mad_dump_field(enum MAD_FIELDS field, char *buf, int bufsz, void *val);
-MAD_EXPORT char *mad_dump_val(enum MAD_FIELDS field, char *buf, int bufsz, void *val);
+MAD_EXPORT int mad_print_field(enum MAD_FIELDS field, const char *name, void *val)
+				DEPRECATED;
+
+/**
+ * The following functions print fields to "s" in various ways
+ *
+ * mad_dump_[val|field] take a value "val" and use "field" to format it
+ *
+ * mad_snprint_field takes a data buffer "buf" and uses field to extract and
+ * format it.
+ *
+ * RETURN "s" or NULL on failure
+ */
+MAD_EXPORT char *mad_dump_field(enum MAD_FIELDS field, char *s, int n, void *val);
+	/* outputs string "<field>:........<val>" */
+MAD_EXPORT char *mad_dump_val(enum MAD_FIELDS field, char *s, int n, void *val);
+	/* outputs string "<val>" */
+
+/**
+ * printf functions
+ * input's "standard" printf parameters except for "buf" which is a mad buffer
+ * return the number of actual chars written to "s" or "stream"
+ */
+MAD_EXPORT int mad_snprintf(char *s, size_t n, uint8_t *buf, const char *format, ...);
+MAD_EXPORT int mad_fprintf(FILE *stream, uint8_t *buf, const char *format, ...);
 
 /* mad.c */
 MAD_EXPORT void *mad_encode(void *buf, ib_rpc_t * rpc, ib_dr_path_t * drpath,
diff --git a/libibmad/man/mad_fprintf.3 b/libibmad/man/mad_fprintf.3
new file mode 100644
index 0000000..f95f799
--- /dev/null
+++ b/libibmad/man/mad_fprintf.3
@@ -0,0 +1,76 @@
+.\" -*- nroff -*-
+.\"
+.TH MAD_FPRINTF 3  "Feb 26, 2009" "OpenIB" "OpenIB Programmer\'s Manual"
+.SH "NAME"
+mad_fprintf, mad_snprintf \- formatted output conversion for mad packets
+.SH "SYNOPSIS"
+.nf
+.B #include <infiniband/mad.h>
+.sp
+.BI "MAD_EXPORT int mad_snprintf(char " "*s" ", size_t "n ", uint8_t " "*buf" ", const char " "*format" ", ...);
+.BI "MAD_EXPORT int mad_fprintf(FILE " "*stream" ", uint8_t " "*buf" ", const char " "*format" ", ...);
+.fi
+.SH "DESCRIPTION"
+Similar to the printf family of functions.  The exception being they accept a
+"buf" parameter which represents a mad data buffer.  This buffer is used to
+extract and print fields as specified with the
+.B %m
+and 
+.B %M
+format specifiers.
+.PP
+.B m
+The %m specifier is used to print out fields decoded from the "buf" data
+buffer.
+.B M
+The %M specifier is used to print the name of the field specified.
+.I enum MAD_FIELDS\fR
+values should be used to specify the field to be decoded.
+.PP
+.SH "EXAMPLES"
+.nf
+char portinfo[64];
+void *pi = portinfo;
+.PP
+if (!smp_query(pi, portid, IB_ATTR_PORT_INFO, portnum, timeout))
+.in +8
+	return -1;
+.in -16
+.PP
+mad_fprintf(stdout, pi, "Port info (%s):\\n"
+.in +16
+"   %-10s (%M): %m\\n"
+"   %-10s (%M): %m\\n"
+"   %-10s (%M): %m\\n"
+"   %-10s (%M): %m\\n"
+"   %-10s (%M): %m\\n"
+"   %-10s (%M): %m\\n",
+portid2str(portid),
+"LID", IB_PORT_LID_F, IB_PORT_LID_F,
+"LMC", IB_PORT_LMC_F, IB_PORT_LMC_F,
+"state", IB_PORT_STATE_F, IB_PORT_STATE_F,
+"physstate", IB_PORT_PHYS_STATE_F, IB_PORT_PHYS_STATE_F,
+"linkwidth", IB_PORT_LINK_WIDTH_ACTIVE_F, IB_PORT_LINK_WIDTH_ACTIVE_F,
+"linkspeed", IB_PORT_LINK_SPEED_ACTIVE_F, IB_PORT_LINK_SPEED_ACTIVE_F);
+.in -16
+.PP
+Results in the output.
+.PP
+Port info (DR path slid 0; dlid 0; 0,1,8,22):
+.in +3
+   LID        (Lid): 15
+   LMC        (LMC): 0
+   state      (LinkState): Active
+   physstate  (PhysLinkState): LinkUp
+   linkwidth  (LinkWidthActive): 4X
+   linkspeed  (LinkSpeedActive): 2.5 Gbps
+.in -3
+
+.SH "RETURN VALUE"
+.B return the number of characters printed.
+
+.SH "SEE ALSO"
+.BR printf (3)
+.SH "AUTHOR"
+.TP
+Ira Weiny <weiny2 at llnl.gov>
diff --git a/libibmad/man/mad_snprintf.3 b/libibmad/man/mad_snprintf.3
new file mode 100644
index 0000000..c004ab9
--- /dev/null
+++ b/libibmad/man/mad_snprintf.3
@@ -0,0 +1,2 @@
+.TH MAD_SNPRINTF 3  "Feb 26, 2009" "OpenIB" "OpenIB Programmer\'s Manual"
+.so man3/mad_fprintf.3
diff --git a/libibmad/src/fields.c b/libibmad/src/fields.c
index df43ceb..af36912 100644
--- a/libibmad/src/fields.c
+++ b/libibmad/src/fields.c
@@ -39,6 +39,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <stdarg.h>
 
 #include <infiniband/mad.h>
 
@@ -442,6 +443,9 @@ static const ib_field_t ib_mad_f[] = {
 
 };
 
+#define MAD_FIELD_MAX_BYTE_LEN (256)
+	/* currently "Vendor2Data" increased to the next power of 2 */
+
 static void _set_field64(void *buf, int base_offs, const ib_field_t * f,
 			 uint64_t val)
 {
@@ -666,6 +670,7 @@ static int _mad_print_field(const ib_field_t * f, const char *name, void *val,
 			 valsz ? valsz : ALIGN(f->bitlen, 8) / 8);
 }
 
+/* This function is deprecated use mad_snprint_field or mad_dump_* instead */
 int mad_print_field(enum MAD_FIELDS field, const char *name, void *val)
 {
 	if (field <= IB_NO_FIELD || field >= IB_FIELD_LAST_)
@@ -673,16 +678,124 @@ int mad_print_field(enum MAD_FIELDS field, const char *name, void *val)
 	return _mad_print_field(ib_mad_f + field, name, val, 0);
 }
 
-char *mad_dump_field(enum MAD_FIELDS field, char *buf, int bufsz, void *val)
+char *mad_dump_field(enum MAD_FIELDS field, char *s, int n, void *val)
 {
 	if (field <= IB_NO_FIELD || field >= IB_FIELD_LAST_)
 		return 0;
-	return _mad_dump_field(ib_mad_f + field, 0, buf, bufsz, val);
+	return _mad_dump_field(ib_mad_f + field, 0, s, n, val);
 }
 
-char *mad_dump_val(enum MAD_FIELDS field, char *buf, int bufsz, void *val)
+char *mad_dump_val(enum MAD_FIELDS field, char *s, int n, void *val)
 {
 	if (field <= IB_NO_FIELD || field >= IB_FIELD_LAST_)
 		return 0;
-	return _mad_dump_val(ib_mad_f + field, buf, bufsz, val);
+	return _mad_dump_val(ib_mad_f + field, s, n, val);
 }
+
+static int mad_vsnprintf(char *s, size_t n, void *buf, const char *fmt, va_list args)
+{
+	int rc = 0;
+	char *str;
+	char tmp[256];
+
+/* Macros allows for bounding length of print to provided buffer
+ * remove 1 to allow for \0 char */
+#define WRITE_CHAR(c) do { \
+	*str++ = c; \
+	if (++rc >= (n-1)) { \
+		goto max_len_hit; \
+	} \
+} while(0)
+
+#define WRITE_STR(STR) do { \
+	const char *ls; \
+	for (ls = STR; *ls != '\0'; ls++) \
+		WRITE_CHAR(*ls); \
+} while (0)
+
+	for (str=s ; *fmt ; /* fmt is incremented in body */) {
+		if (*fmt != '%') {
+			WRITE_CHAR(*fmt);
+			++fmt;
+			continue;
+		}
+
+		++fmt; /* skip '%' */
+		switch (*fmt) {
+			case 'M':
+			{
+				/* print our special mad field name */
+				int field = va_arg(args, int);
+				++fmt; /* consume 'M' */
+				if (field <= IB_NO_FIELD || field >= IB_FIELD_LAST_) {
+					WRITE_STR("<field error>");
+					continue;
+				}
+
+				WRITE_STR(ib_mad_f[field].name);
+				break;
+			}
+			case 'm':
+			{
+				/* print our special mad field */
+				uint8_t val[MAD_FIELD_MAX_BYTE_LEN];
+				int field = va_arg(args, int);
+				const ib_field_t *f = ib_mad_f + field;
+
+				++fmt; /* consume 'm' */
+				if (field <= IB_NO_FIELD || field >= IB_FIELD_LAST_) {
+					WRITE_STR("<field error>");
+					continue;
+				}
+
+				mad_decode_field(buf, field, val);
+				f->def_dump_fn(tmp, n, val, ALIGN(f->bitlen, 8) / 8);
+				WRITE_STR(tmp);
+				break;
+			}
+			default:
+			{
+				/* process all other formatting */
+				char tf[256];
+				int i;
+				tf[0] = '%';
+				for (i = 1; *fmt != '%' || i == 255; fmt++, i++)
+					tf[i] = *fmt;
+				tf[i]='\0';
+				vsnprintf(tmp, 256, tf, args); /* <== not sure if this is safe??? */
+				WRITE_STR(tmp);
+				break;
+			}
+		}
+	}
+max_len_hit:
+	*str = '\0';
+	return str-s;
+}
+
+int mad_snprintf(char *s, size_t n, uint8_t *buf, const char *format, ...)
+{
+	va_list args;
+	int i;
+
+	va_start(args, format);
+	i = mad_vsnprintf(s, n, buf, format, args);
+	va_end(args);
+	return (i);
+}
+
+int mad_fprintf(FILE *stream, uint8_t *buf, const char *format, ...)
+{
+	char str_buf[1024];
+	va_list args;
+	int i,j;
+
+	va_start(args, format);
+	i = mad_vsnprintf(str_buf, 1024, buf, format, args);
+	va_end(args);
+	j = fprintf(stream, "%s", str_buf);
+	if (i != j)
+		IBWARN("mad_vsnprintf and fprintf don't match???\n");
+	return (i);
+}
+
diff --git a/libibmad/src/libibmad.map b/libibmad/src/libibmad.map
index 4306dbc..f55027c 100644
--- a/libibmad/src/libibmad.map
+++ b/libibmad/src/libibmad.map
@@ -4,6 +4,8 @@ IBMAD_1.3 {
 		mad_dump_field;
 		mad_dump_val;
 		mad_print_field;
+		mad_snprintf;
+		mad_fprintf;
 		mad_dump_array;
 		mad_dump_bitfield;
 		mad_dump_hex;
-- 
1.5.4.5




More information about the general mailing list