ofp-util: New function ofputil_port_to_string().
authorBen Pfaff <blp@nicira.com>
Mon, 24 Jun 2013 20:18:46 +0000 (13:18 -0700)
committerBen Pfaff <blp@nicira.com>
Mon, 24 Jun 2013 20:21:30 +0000 (13:21 -0700)
This function is more convenient than ofputil_format_port() when a "struct
ds" is not already in use.  This commit converts one caller for which this
was already true, and the following commit will add another.

Signed-off-by: Ben Pfaff <blp@nicira.com>
lib/ofp-util.c
lib/ofp-util.h

index 90f4f35..de73eba 100644 (file)
@@ -4142,14 +4142,12 @@ ofputil_port_from_string(const char *s, ofp_port_t *portp)
                       "be translated to %u when talking to an OF1.1 or "
                       "later controller", port32, port32 + OFPP11_OFFSET);
         } else if (port32 <= ofp_to_u16(OFPP_LAST_RESV)) {
-            struct ds msg;
-
-            ds_init(&msg);
-            ofputil_format_port(u16_to_ofp(port32), &msg);
-            VLOG_WARN_ONCE("referring to port %s as %u is deprecated for "
-                           "compatibility with future versions of OpenFlow",
-                           ds_cstr(&msg), port32);
-            ds_destroy(&msg);
+            char name[OFP_MAX_PORT_NAME_LEN];
+
+            ofputil_port_to_string(u16_to_ofp(port32), name, sizeof name);
+            VLOG_WARN_ONCE("referring to port %s as %"PRIu32" is deprecated "
+                           "for compatibility with OpenFlow 1.1 and later",
+                           name, port32);
         } else if (port32 < ofp11_to_u32(OFPP11_MAX)) {
             VLOG_WARN("port %u is outside the supported range 0 through "
                       "%"PRIx16" or 0x%x through 0x%"PRIx32, port32,
@@ -4189,18 +4187,32 @@ ofputil_port_from_string(const char *s, ofp_port_t *portp)
 void
 ofputil_format_port(ofp_port_t port, struct ds *s)
 {
-    const char *name;
+    char name[OFP_MAX_PORT_NAME_LEN];
+
+    ofputil_port_to_string(port, name, sizeof name);
+    ds_put_cstr(s, name);
+}
 
+/* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
+ * representation of OpenFlow port number 'port'.  Most ports are represented
+ * as just the port number, but special ports, e.g. OFPP_LOCAL, are represented
+ * by name, e.g. "LOCAL". */
+void
+ofputil_port_to_string(ofp_port_t port,
+                       char namebuf[OFP_MAX_PORT_NAME_LEN], size_t bufsize)
+{
     switch (port) {
-#define OFPUTIL_NAMED_PORT(NAME) case OFPP_##NAME: name = #NAME; break;
+#define OFPUTIL_NAMED_PORT(NAME)                        \
+        case OFPP_##NAME:                               \
+            ovs_strlcpy(namebuf, #NAME, bufsize);       \
+            break;
         OFPUTIL_NAMED_PORTS
 #undef OFPUTIL_NAMED_PORT
 
     default:
-        ds_put_format(s, "%"PRIu16, port);
-        return;
+        snprintf(namebuf, bufsize, "%"PRIu16, port);
+        break;
     }
-    ds_put_cstr(s, name);
 }
 
 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
index f8705a2..eaa3af2 100644 (file)
@@ -40,6 +40,8 @@ enum ofperr ofputil_check_output_port(ofp_port_t ofp_port,
                                       ofp_port_t max_ports);
 bool ofputil_port_from_string(const char *, ofp_port_t *portp);
 void ofputil_format_port(ofp_port_t port, struct ds *);
+void ofputil_port_to_string(ofp_port_t, char namebuf[OFP_MAX_PORT_NAME_LEN],
+                            size_t bufsize);
 
 /* Converting OFPFW10_NW_SRC_MASK and OFPFW10_NW_DST_MASK wildcard bit counts
  * to and from IP bitmasks. */